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

ScientificNotationNode #613

Closed
pixelzoom opened this issue Jul 21, 2020 · 8 comments
Closed

ScientificNotationNode #613

pixelzoom opened this issue Jul 21, 2020 · 8 comments
Assignees
Labels

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Jul 21, 2020

Discovered in phetsims/ph-scale#173.

Example:

> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 9.952541657737637e-8 )
{mantissa: "10.0", exponent: "-8"}

So 9.952541657737637e-8 is displayed as "10.0 x 10-8", when it should be "1.0 x 10-7".

This only occurs when the mantissa is exactly 10. And in that case, that power of 10 needs to be shifted to the exponent.

This will affect all sims that use ScientificNotationNode, which currently includes: blackbody-spectrum, coulombs-law, gravity-force-lab, gravity-force-lab-basics.

@pixelzoom
Copy link
Contributor Author

Fixed in the above commit. This problem occurs only when the mantissa is exactly 10, so the fix is specific to that value.

Some tests:

> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 9.952541657737637e-8 )
{mantissa: "1.0", exponent: "-7"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 9.952541657737637e-8, {mantissaDecimalPlaces: 2} )
{mantissa: "9.95", exponent: "-8"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 9.9e-8 )
{mantissa: "9.9", exponent: "-8"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 10e-8 )
{mantissa: "1.0", exponent: "-7"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 10.1e-8 )
{mantissa: "1.0", exponent: "-7"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 10.1e-8, { mantissaDecimalPlaces: 1 } )
{mantissa: "1.0", exponent: "-7"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 10.1e-8, { mantissaDecimalPlaces: 2 } )
{mantissa: "1.01", exponent: "-7"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 10.1e-8, { mantissaDecimalPlaces: 3 } )
{mantissa: "1.010", exponent: "-7"}
> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 10.5e-8 )
{mantissa: "1.1", exponent: "-7"}

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jul 21, 2020

Reopening.

After further digging, I discovered that this bug was introduced by @jessegreenberg in bfa1874 on 9/20/17. Reverting that commit yields the correct result:

> phet.sceneryPhet.ScientificNotationNode.toScientificNotation( 9.952541657737637e-8 )
{mantissa: "1.0", exponent: "-7"}

There's no GitHub issue referenced in the commit. @jessegreenberg do you recall why that change was made?

@pixelzoom pixelzoom reopened this Jul 21, 2020
@jessegreenberg
Copy link
Contributor

Commit bfa1874 actually does reference #334 which describes:

ScientificNotationNode.toScientificNotationNode returns an object like {mantissa:{number}, exponent:{number}}. Since mantissa is a number, we lose the zeroes to the right of the decimal and the precision specified by the option mantissaDecimalPlaces. To keep consistent precision when there are zeroes to the right of the decimal mantissa needs to be a string.

@pixelzoom
Copy link
Contributor Author

Thanks @jessegreenberg -- the issue number didn't show up as a link in WebStorm's "Show History", so I missed it.

The change I made in 65aa972 doesn't affect the goal of bfa1874. So I'll leave things as is and re-close.

@samreid
Copy link
Member

samreid commented Sep 8, 2022

As discovered in phetsims/chipper#946, there are TODOs in the code referring to this issue. Reopening.

@samreid samreid reopened this Sep 8, 2022
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Sep 8, 2022

Here's the remaining TODO:

      // Convert to a string in exponential notation, where the mantissa has 1 digit to the left of the decimal place.
      //TODO https://github.com/phetsims/scenery-phet/issues/613 toExponential uses Number.toFixed, which doesn't round the same on all platforms
      const exponentialString = value.toExponential( options.mantissaDecimalPlaces );

Per @kathy-phet in dev meeting, no plans to address this anytime soon, so unassigning.

@samreid
Copy link
Member

samreid commented Jan 3, 2024

In phetsims/dot#113, @zepumph and I changed the implementation of Utils.toFixed to use https://github.com/MikeMcl/big.js, which is described as "A small, fast JavaScript library for arbitrary-precision decimal arithmetic." @pixelzoom can you please check on the status of this TODO after that change? Please zoom consult with me and or @zepumph at your discretion.

pixelzoom added a commit that referenced this issue Jan 3, 2024
pixelzoom added a commit that referenced this issue Jan 3, 2024
pixelzoom added a commit that referenced this issue Jan 3, 2024
@pixelzoom
Copy link
Contributor Author

Running the tests in #613 (comment), this appears to be resolved. So I deleted the associated TODO comments, and will close this issue.

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

No branches or pull requests

3 participants