simplify is overly aggressive for composed units
I have an issue with the simplify conversion of certain complex units, which seems overly aggressive to me.
The following is an example using two simple units: energy and energy by square meter (very common in many sectors).
// Simple energy
e = unit(123456, 'Wh')
console.log(e.simplify().toString()) // 123.456 kWh
// Energy by square meter
e = unit(123456, 'Wh / m^2')
console.log(e.simplify().toString()) // 444441600 kg / s^2
Clearly the second simplification does not make much sense, even if it is absolutely correct. How can this be prevented?
So it seems like you are expecting it to return 123.456 kWh / m^2? Unfortunately, I think the only to get simplify to do this currently would be to add "kWh/m^2" to the systems.si definition, so that simplify considers it as an option when it is simplifying the unit:
unit = unit.config({ definitions: { systems: { si: ['kWh/m^2'] } } })
unit(123456, 'Wh/m^2').simplify().toString() // 123.456 kWh / m^2
But this is not satisfactory at all, as you still have to tell it what units you want, which defeats the purpose of using simplify.
There is an internal function which auto chooses a prefix. If we made that function public, would that work for you? It could work something like this:
unit(123456, 'Wh/m^2').autoPrefix().toString() // 123.456 kWh / m^2
Prior to v1.0.0, units would only be simplified if the "complexity" of the new unit was reduced by a certain threshold. That behavior was sometimes very confusing, though. So in v1.0.0 it was changed to be more predictable, so if you call simplify, it would always try to simplify. But there is still room for adding the "complexity threshold" and other simplification options back in if there is a need for it.
Thanks for the reply and explanation!
Yes, I think that definitely having a function that is able to rescale units using the SI, but without changing the units themselves, would be very handy.
Examples:
unit(123456, 'Wh/m^2').autoPrefix().toString() // 123.456 kWh / m^2
unit(1, 'GWh/km^2').autoPrefix().toString() // 1 MWh / m^2
// Here numerator and denominator prefixes are simplified, and units stay the same
Other libraries seem to do something very similar, for example unitmath automatically selects the best prefix when formatting (could be an option here too, as it is for simplify iirc).
autoPrefix should also have prefixMin and prefixMax thresholds.
A word of caution though: imho there should be some way to define which SI prefixes to ignore. For example, in the case of energy or power, nobody would ever use cW or hWh; however at the same time, for other units (e.g. lengths), they are more commonly used.
So there should be a config way to select which prefixes to accept when using autoPrefix (or simplify, for that matter).
IMHO a sane default would be to use only prefixes with exponents multiple of 3 (..., p, u, m, '', k, M, G, T, ...).
I've been playing with this a bit more, and in general something .autoPrefix() would be extremely handy to have, as well as the ability to separate prefixes from units
Sorry, do you think this is going to be implemented? I'm in the position in which I have to choose between this library and another, and knowing if this is going to be available shortly or not makes is relatively important for the decision.
thanks in advance!
I believe we can definitely add autoPrefix(). Unfortunately, I can't really give you an estimate on when I'll be able to get to it, though. Hopefully this weekend if I'm lucky.
there should be some way to define which SI prefixes to ignore. For example, in the case of energy or power, nobody would ever use cW or hWh; however at the same time, for other units (e.g. lengths), they are more commonly used.
This should already be possible using formatPrefixes. Some pretty standard values are already built-in. For example, for m (meter) the default prefixes that it will use for formatting are n, u, m, c, '', and k. This is all configurable, too. I believe the autoPrefix method would use the same rules.
I believe we can definitely add
autoPrefix(). Unfortunately, I can't really give you an estimate on when I'll be able to get to it, though. Hopefully this weekend if I'm lucky.there should be some way to define which SI prefixes to ignore. For example, in the case of energy or power, nobody would ever use cW or hWh; however at the same time, for other units (e.g. lengths), they are more commonly used.
This should already be possible using
formatPrefixes. Some pretty standard values are already built-in. For example, form(meter) the default prefixes that it will use for formatting aren,u,m,c,'', andk. This is all configurable, too. I believe theautoPrefixmethod would use the same rules.
That's awesome news! Thanks!
Hi, sorry to push on this, is there any news? Would you eventually accept a PR?
So sorry, I haven't gotten to it yet. Work has been very busy lately.
I would gladly accept a PR!
So sorry, I haven't gotten to it yet. Work has been very busy lately.
I would gladly accept a PR!
I've done so with #60, but I'm sure there's more to it?
Very sorry for the delay. I've added the new function as applyBestPrefix in version 1.1.0. Let me know if that meets your need!