jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
517 stars 82 forks source link

Add setting for scientific notation #528

Closed AlienKevin closed 3 years ago

AlienKevin commented 4 years ago

Currently, all number outputs are converted to scientific notation only when exceeding 10^20.

10^20 = 100000000000000000000
10^21 = 1e+21

To me, this is a lot of zeros to count. Can we add a new setting like "MIN_SCIENTIFIC_NOTATION" so users can set their desired number of zeros to display before converting to scientific notation?

Happypig375 commented 4 years ago

An alternative would be to decrease the default boundary of scientific notation. For instance, my calculator can only display up to 10 digits at a time before needing to use scientific notation.

jiggzson commented 4 years ago

@AlienKevin, can you give me an example of this? I didn't even realize that this happened since I haven't yet complete the scientific notation option. The code is there but just not implemented.

jiggzson commented 4 years ago

@AlienKevin, I've added this option. Feel free to test it out and provide feedback. In a nutshell:

var x = nerdamer('7/(11*x-24*x^2)+cos(13/44)^(300/21)+3333/1000');
console.log(x.text('scientific'));
//3.333+7*(-2.4e1*x^2+1.1e1*x)^(-1)+cos(2.95454545454545e-1)^1.42857142857143e1

The digits can be set with SCIENTIFIC_MAX_DECIMAL_PLACES is set to 14 by default. You can force zero exponents to be displayed as scientific numbers by setting SCIENTIFIC_IGNORE_ZERO_EXPONENTS to false.

jiggzson commented 4 years ago

Added. Missing documentation.

jiggzson commented 3 years ago

This issue has been fixed. It's documentation still needs to be added so moved to #141.

itmeson commented 2 years ago

Is there a way to make the .toTeX() function also use the SCIENTIFIC_MAX_DECIMAL_PLACES setting? I can convert to text, back to a nerdamer value, then back to TeX but that can mess up the rest of the expression and reverts the number of decimal places shown to the default of 20 -- while it rounds to the number of places set in SCIENTIFIC_MAX_DECIMAL_PLACES, it still doesn't switch to scientific notation until the number is more than 20 digits long.

Here's the difference in behavior with toTeX:

nerdamer.set("SCIENTIFIC_MAX_DECIMAL_PLACES", 5)

nerdamer("123456789x^2").text('scientific')
\\"1.23457e8*x^2"

nerdamer("123456789x^2").toTeX('scientific')
\\"123456789 \\cdot x^{2}" 

nerdamer(nerdamer("123456789x^2").text('scientific')).toTeX('scientific')
\\"123457000 \\cdot x^{2}"

​

It would also be nice if display precision could be overridden individually for each output, like:

console.log(a.toTeX('scientific', {precision: 5}))