-
Notifications
You must be signed in to change notification settings - Fork 6
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 rounds numbers incorrectly #747
Comments
I reproduced the problem using the above patch to simula-rasa. To clarify... In the above example, 424.8 should be 4.2 x 10^2, not 4.3 x 10^2. The above patch is displaying 4.3. x 10^2 in SimulaRasaScreenView. Repos/sims that use ScientificNotationNode include:
|
The first thing I checked was whether this problem was introduced during TypeScript conversion. So I revert to e0d0199, which is when the file was renamed from .js to .ts, but had not otherwise been changed. The problem was still present in the SimulaRasaScreenView patch, so it would seem that this problem has been around for awhile. I then isolated the problem to the static In the brower console:
|
In the above commits, I improved some documentation. In 8048792, I did a bit of TypeScript/options cleanup. |
Going through the cases in The first case is when the value is if ( value === 0 ) {
mantissa = 0;
exponent = 1;
} That's incorrect, as shown in these examples:
Fixed in the above commit, so that the above examples are:
|
The next case in else if ( options.exponent !== null && options.exponent === 0 ) {
mantissa = Utils.toFixedNumber( value, options.mantissaDecimalPlaces );
exponent = 0;
} The Fixed in the above commit. |
The next case is where the problem that was reported occurs: else {
// Convert to a string in exponential notation (eg 2e+2).
// Request an additional decimal place, because toExponential uses toFixed, which doesn't round the same on all platforms.
const exponentialString = value.toExponential( options.mantissaDecimalPlaces + 1 ); We're requesting one extra decimal place, to do our own rounding later, due to problems with built-in
Then later in the code, 4.25 will be rounded to 1 decimal place, resulting in 4.3. So yes, we're rounding twice. This is going to require some serious reimplementation, with requirements:
|
In the above commits, I added unit tests for |
In the above commit, I added an implementation and unit tests for the case where a non-zero exponent is supplied via options: else if ( options.exponent !== null ) {
// M x 10^E, where E is options.exponent
mantissa = Utils.toFixedNumber( value / Math.pow( 10, options.exponent ), options.mantissaDecimalPlaces );
exponent = options.exponent;
} In the process, I discovered a problem with
35.855 rounded to 2 decimal places should be '35.86', but |
In phetsims/dot#113, I wrote |
…led by the general case; delete workaround for toFixed; adding unit tests; #747
Summary of above commits:
|
In 6/30 dev meeting:
|
Summary:
Back to @Luisav1 to review. If this meets your needs for build-a-nucleaus, please assign back to me. |
@Luisav1 reminder that this issue is assigned to you for review. Let me know if you have questions. |
Thanks @pixelzoom! @chrisklus, @marlitas, and I first ran the QUnit tests which all passed. We reviewed the cases set for each test which were working as expected. This should be good, assigning it back to @pixelzoom. |
👍🏻 closing. |
The ScientificNotationNode rounds first to the number of decimals specified plus one seen below.
This causes the number to be rounded up twice in some cases, resulting in incorrect rounding as in the example in the patch below. The number 424.8 rounded to 2 significant digits should be 4.2 x 10^2. Other numbers like this are in this issue phetsims/build-a-nucleus#24.
@pixelzoom can you take a look at this and see if there's a way to fix it? Thanks!
The text was updated successfully, but these errors were encountered: