nerdamer
nerdamer copied to clipboard
Proposal: round for scientific numbers
This is no mathematical issue but would be of great practical use for physics:
given a result like 1.23423534e-12 normally is rounded in physics with a reasonable number of valid figures, e. g. in this case 1.23e-12.
It would be nice to let round(1.23423534e-12,2) give back this result instead of first converting the number to a decimal number and then giving back zero.
This is a "nice to have" issue.
@menzelths the biggest problem I see is that after the conversion to the decimal number, there's really no way of know how the number was input. All number are treated equally once they reach the parser. The only solutions I can think of is to either allow for a third parameter or to signal to the library that you want the decimal treated as a scientific number by making the second parameter negative.
@menzelths This has been implemented on dev. To round only the coefficient, the second argument has to be negative so for instance
var x = nerdamer('round(1.23423534e-12,-2)');
console.log(x.text());
Is that what you had in mind?
Thoughts: 2nd parameter (-n) means (n+1) significant figures Should change so that 2nd parameter (-n) means (n) significant figures?
@Happypig375 The negative sign is just to signal to round that the coefficient should be rounded. Not the whole number.
2nd parameter (-n) means (n+1) significant figures
I don't understand what you mean here.
Finally I had the possibility to check the enhancement:
for numbers smaller than 10 it is working as expected but not for numbers greater than 10.
I would like it as Happypig375 suggested, the rounding parameter determing the number of significant figures, which means:
125324142 with 2 significant figures is 1.3e8 125324142 with 4 significant figures is 1.243e8 0.0001234 with 2 significant figures is 1.2e-4 0.1 with 2 significant figures is 1.0e-1
and so on. This is common in natural sciences to limit the number of figures. Preceding zeros are not counted but all the figures behind it. The scientific notation always starts with a figure unequal to zero.
It would really be very convenient to make it work as Happypig375 suggested: let n be the number of significant figures (or n+1, I don't mind). It can be negative, but in a way that -2 means 2 significant figures.
BTW: for toTeX() it would be great to convert "1.243e8" to "1.243\cdot 10^{8}". I suppose that's how it is written in the States, too, isn't it?
I am sorry that I am not able to make a pull request for this asI am not so much into it to understand exactly how to determine if a number is a decimal number or not and how to get the exponent.
I have found a nice description here: http://web.lemoyne.edu/giunta/chm151L/scinot.html
@menzelths, There currently is no way to tell the library to return the result in scientific notation. I guess I can create a function called scientific and do something like:
nerdamer('scientific(0.125)');
Finally being back on github :-)
I think implementing something like "scientific(1.3234,4)" would perfectly fit the needs of physicians with the second argument being the number of valid figures.
Examples would be:
scientific(12342152351,3) gets 1.23e10 scientific(0.1231231313,4) gets 1.231e-1
The results should translate to the following tex representations: 1.23 \cdot 10^10 and 1.231 \cdot 10^{-1}