Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit's .toString() method doesn't convert 1000 W to 1 kW #898

Closed
pvoznyuk opened this issue Jul 12, 2017 · 11 comments
Closed

Unit's .toString() method doesn't convert 1000 W to 1 kW #898

pvoznyuk opened this issue Jul 12, 2017 · 11 comments

Comments

@pvoznyuk
Copy link

pvoznyuk commented Jul 12, 2017

I expect 1000 W to be converted to 1 kW as well as 1000 kW is converted to 1 MW. However I see that:

math.unit(1000, 'W').toString() // otputs: 1000 W
math.unit(1000.01, 'W').toString() // otputs: 1 kW
math.unit(999, 'W').toString() // otputs: 999 W
math.unit(1000000, 'W').toString() // otputs: 1 MW

Is there a way to convert 1000 W to 1 kW ?

@pvoznyuk pvoznyuk changed the title Unit's .toString() method doesn't converts 1000 W to 1 kW Unit's .toString() method doesn't convert 1000 W to 1 kW Jul 12, 2017
@ericman314
Copy link
Collaborator

You can use the to method:

var c = math.unit('1000 W');        // 1000 W
var d = c.to('kW');                 // 1 kW

@pvoznyuk
Copy link
Author

pvoznyuk commented Jul 12, 2017

@ericman314
sure I can if I know the value, but usually it is a variable

math.unit(value, 'W').toString();

and I expect it will be converted to W / kW / MW / etc depending on value

@ericman314
Copy link
Collaborator

math.js tries to format the unit using an appropriate prefix. Currently we have the following behavior:

1000000 mW --> 1 kW
1000 W     --> 1000 W
1 kW       --> 1 kW
0.001 MW   --> 1 kW

The function chooses a prefix that results in a numeric value between approximately 0.501187... and 501.187.... For three of the four cases above, this results in 1 kW. However, a condition was added in response to a previous discussion (#737), so that when the input value is already close enough (between 0.1 and 1000) the prefix is not changed.

The details are largely a matter of preference, and we won't be able to please everybody, but if you have a suggestion to improve it, please let us know.

For reference, see the function that chooses the prefix here.

@ThomasBrierley
Copy link
Contributor

ThomasBrierley commented Jul 17, 2017

I thought with engineering notation the integer part should be less than one thousand (1-3 digits before the decimal)?

Seeing as these are all SI units I can see why one would expect engineering form. It looks like just the boundary condition is wrong, (considering 1000.01w => 1Kw, 1000w => 1000w).

[Edit]

The power is chosen so that the final number will be between 1 and 999

From Practical Problems in Mathematics for Information Technology, Page 67

(Except the extra prefixes between x10^-3 and 3 which should be between 1 and 9)

I guess my question is: should we be using engineering notation with SI units? I think it make sense as they are commonly paired, but perhaps I'm not thinking generally enough... I see in #737 the chosen boundaries are between 0.6 and 500, I'm wondering what the thinking behind this choice is?

@josdejong
Copy link
Owner

I see in #737 the chosen boundaries are between 0.6 and 500, I'm wondering what the thinking behind this choice is?

I'm not sure if this is a choice I've made some day or that this was the work of @ericman314 , but it mostly boils down to heuristics and trying to output a convenient, human friendly format. The boundary at 500 is halfway two prefixes, 600 mm looks "closer" to being a meter, 400 mm is closer to being millimeters. Some prefer an output like 0.83201 meter, others may prefer 832.01 millimeter.

I'm fine with further fine-tuning these heuristics if we can make it better overall, the "number between 1 and 999" rule is also nice and simple.

@ThomasBrierley
Copy link
Contributor

I guess as ericman says this is a matter of opinion, I see maybe some people prefer to say "half a KW" instead of "500 W".

Just to play devils advocate: Some people might also prefer scientific notation (exactly 1 non zero digit before the decimal), even with SI prefixes, just because of the domain they are working in...

In which case I though maybe these preferences are better solved with format which has engineering notation option, however it doesn't seem to be behaving either:

math.unit(1000, 'W').format({notation: 'engineering'}) // "1e+3 W"

math.unit(1000.01, 'W').format({notation: 'engineering'}) // "1.00001e+0 kW"

It also shouldn't really be using e+0 on that last one. I know this is a separate issue really - I should be able to have a dig at this soon because I think I need it to work properly.

@josdejong
Copy link
Owner

Ah, that's interesting indeed.

The mechanism of units to select the best prefix is separate from formatting numbers.

@ThomasBrierley would be great if you could dive into it and see if you can come up with some fixes or general improvements.

@ThomasBrierley
Copy link
Contributor

Yes will do, I need this feature eventually and i'd rather have it as part of mathjs. I cannot promise to implement it immediately though.

@pvoznyuk would this solve your issue if you were able to explicitly select engineering or scientific notation via the format method?

@pvoznyuk
Copy link
Author

@ThomasBrierley that format method improving is a useful thing, but i'm not sure if that can help with my problem.

@ThomasBrierley
Copy link
Contributor

ThomasBrierley commented Aug 1, 2017

My interpretation of the behaviour you want is for numbers with SI units to select the matching SI prefix for it's order of magnitude on output... in other words engineering notation.

It should be possible to first fix the engineering notation formater behaviour to output the correct exponent/integer-range, and then for numbers with SI units replace the exponent with the matching prefix. e.g

Using your examples this would preferably result in:

1000 W => 1 kW
1000.01 W => 1.00001 kW
999 W => 9.99 hW
1000000 W => 1 MW

also

10000 kW => 10 MW
0.01 kW => 1 daW

@josdejong
Copy link
Owner

Closing this discussion now because it has been inactive for a long time. Feel free to reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants