javascript rounding bug
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: AriB, Assigned: norrisboyd)
References
()
Details
Updated•25 years ago
|
Comment 1•25 years ago
|
||
Updated•25 years ago
|
Comment 3•25 years ago
|
||
Comment 4•20 years ago
|
||
Comment 5•20 years ago
|
||
Comment 6•20 years ago
|
||
Comment 9•20 years ago
|
||
Comment 10•19 years ago
|
||
Comment 11•19 years ago
|
||
Comment 12•18 years ago
|
||
Updated•17 years ago
|
Comment 39•11 years ago
|
||
Comment 41•7 years ago
|
||
Comment 42•7 years ago
|
||
Comment 43•7 years ago
|
||
Comment 44•7 years ago
|
||
Comment 45•7 years ago
|
||
Comment 46•6 years ago
|
||
11 mar 2019 4:56 pm edt:
(1)to Waldemar Horwat's comment:
'Every other language that uses standard IEEE double-precision arithmetic will
also give this result. This includes Java and most implementations of C and C++.'
now c produce better result:
//math_test.c
//compile-command:gcc -o math_test math_test.c
#include <stdio.h>
int main ()
{
/* javascript-eval-result,javascript-standard-arithmetic-result:
(1)defect:2.9+2.8=5.699999999999999
(2)defect:2.9-2.8=0.10000000000000009
(3)defect:2.9-2=0.8999999999999999
(4)not defect:2.9X2=5.8
(5)defect:2.9X2.3=6.669999999999999
(6)not defect:2.8/1.4=2
(7)not defect:2.8/4=0.7
(7)not defect:2.8/3=0.9333333333333332
/
printf("2.9+2.8=%f",2.9+2.8); // 2.9+2.8=5.700000
printf("\n2.9-2.8=%f",2.9-2.8); // 2.9-2.8=0.100000
printf("\n2.9-2=%f",2.9-2); // 2.9-2=0.900000
printf("\n2.9x2=%f",2.92); // 2.9x2=5.800000
printf("\n2.9x2.3=%f",2.9*2.3); // 2.9x2.3=6.670000
printf("\n2.8/1.4=%f",2.8/1.4); // 2.8/1.4=2.000000
printf("\n2.8/4=%f",2.8/4); // 2.8/4=0.700000
printf("\n2.8/3=%f",2.8/3); // 2.8/3=0.933333
printf("\n");
return 1;
}
(2)to brendan eich's comment 'Note that ECMA-262 Edition 3 added Number.prototype.toFixed, which takes a
precision argument telling how many digits after the decimal point to show. Use
this method well':
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
from google(Number.prototype.toFixed)result 1
will also round-up result for 2.8/3=0.9333333333333332=0.9.
if that result remain 0.9333333333333332 then that's better because:
linuxmint-18.2->gnu-calculator says:
0.9333333333333332 x 3 = 2.8
0.9 x 3 = 2.7
0.93 x 3 = 2.79.
a javascript-htm-test-file:
<!--- defect_eval.htm:
(1)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval from google(javascript evalulate math)
should contain warning about this eval-defect:
(2)mozilla should write warning about simple-arithmetic-defect
like in page
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
should contain warning saying 'tips:did you know that
in javascript 2.9-2.8 = 0.10000000000000009.please make your own
subtraction-function,addition-function,multiplication-function'
-->
<script language="javascript">
//simple-arithmetic-defect
document.writeln('simple-arithmetic-defect');
document.writeln('<br>(1)defect:2.9+2.8='+(2.9+2.8)); // 5.699999999999999 <-- defect
document.writeln('<br>(2)defect:2.9-2.8='+(2.9-2.8)); // 0.10000000000000009 <-- defect
document.writeln('<br>(3)defect:2.9-2='+(2.9-2)); // 0.8999999999999999 <-- defect
document.writeln('<br>(4)not defect:2.9X2='+(2.92)); // 5.8 <-- not defect
document.writeln('<br>(5)defect:2.9X2.3='+(2.92.3)); // 6.669999999999999 <-- defect
document.writeln('<br>(6)not defect:2.8/1.4='+(2.8/1.4)); // 2 <-- not defect
document.writeln('<br>(7)not defect:2.8/4='+(2.8/4)); // 0.7 <-- not defect
document.writeln('<br>(7)not defect:2.8/3='+(2.8/3)); // 0.9333333333333332 <-- not defect
//eval-defect
document.writeln('<br><br>eval-defect');
document.writeln('<br>(1)defect:2.9+2.8='+eval(2.9+2.8)); // 5.699999999999999 <-- defect
document.writeln('<br>(2)defect:2.9-2.8='+eval(2.9-2.8)); // 0.10000000000000009 <-- defect
document.writeln('<br>(3)defect:2.9-2='+eval(2.9-2)); // 0.8999999999999999 <-- defect
document.writeln('<br>(4)not defect:2.9X2='+eval(2.92)); // 5.8 <-- not defect
document.writeln('<br>(5)defect:2.9X2.3='+eval(2.92.3)); // 6.669999999999999 <-- defect
document.writeln('<br>(6)not defect:2.8/1.4='+eval(2.8/1.4)); // 2 <-- not defect
document.writeln('<br>(7)not defect:2.8/4='+eval(2.8/4)); // 0.7 <-- not defect
document.writeln('<br>(7)not defect:2.8/3='+eval(2.8/3)); // 0.9333333333333332 <-- not defect
</script>
Comment 47•6 years ago
|
||
(In reply to arnon81 from comment #46)
11 mar 2019 4:56 pm edt:
(1)to Waldemar Horwat's comment:
'Every other language that uses standard IEEE double-precision arithmetic will
also give this result. This includes Java and most implementations of C and C++.'now c produce better result:
//math_test.c
//compile-command:gcc -o math_test math_test.c
#include <stdio.h>
int main ()
{
/* javascript-eval-result,javascript-simple-arithmetic-result:
(1)defect:2.9+2.8=5.699999999999999
(2)defect:2.9-2.8=0.10000000000000009
(3)defect:2.9-2=0.8999999999999999
(4)not defect:2.9X2=5.8
(5)defect:2.9X2.3=6.669999999999999
(6)not defect:2.8/1.4=2
(7)not defect:2.8/4=0.7
(7)not defect:2.8/3=0.9333333333333332
/
printf("2.9+2.8=%f",2.9+2.8); // 2.9+2.8=5.700000
printf("\n2.9-2.8=%f",2.9-2.8); // 2.9-2.8=0.100000
printf("\n2.9-2=%f",2.9-2); // 2.9-2=0.900000
printf("\n2.9x2=%f",2.92); // 2.9x2=5.800000
printf("\n2.9x2.3=%f",2.9*2.3); // 2.9x2.3=6.670000
printf("\n2.8/1.4=%f",2.8/1.4); // 2.8/1.4=2.000000
printf("\n2.8/4=%f",2.8/4); // 2.8/4=0.700000
printf("\n2.8/3=%f",2.8/3); // 2.8/3=0.933333
printf("\n");return 1;
}(2)to brendan eich's comment 'Note that ECMA-262 Edition 3 added Number.prototype.toFixed, which takes a
precision argument telling how many digits after the decimal point to show. Use
this method well':
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
from google(Number.prototype.toFixed)result 1
will also round-up result for 2.8/3=0.9333333333333332=0.9.
if that result remain 0.9333333333333332 then that's better because:
linuxmint-18.2->gnu-calculator says:
0.9333333333333332 x 3 = 2.8
0.9 x 3 = 2.7
0.93 x 3 = 2.79.a javascript-htm-test-file:
<!--- defect_eval.htm:
(1)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval from google(javascript evalulate math)
should contain warning about this eval-defect:
(2)mozilla should write warning about simple-arithmetic-defect
like in page
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
should contain warning saying 'tips:did you know that
in javascript 2.9-2.8 = 0.10000000000000009.please make your own
subtraction-function,addition-function,multiplication-function'
--><script language="javascript">
//simple-arithmetic-defect
document.writeln('simple-arithmetic-defect');
document.writeln('<br>(1)defect:2.9+2.8='+(2.9+2.8)); // 5.699999999999999 <-- defect
document.writeln('<br>(2)defect:2.9-2.8='+(2.9-2.8)); // 0.10000000000000009 <-- defect
document.writeln('<br>(3)defect:2.9-2='+(2.9-2)); // 0.8999999999999999 <-- defect
document.writeln('<br>(4)not defect:2.9X2='+(2.92)); // 5.8 <-- not defect
document.writeln('<br>(5)defect:2.9X2.3='+(2.92.3)); // 6.669999999999999 <-- defect
document.writeln('<br>(6)not defect:2.8/1.4='+(2.8/1.4)); // 2 <-- not defect
document.writeln('<br>(7)not defect:2.8/4='+(2.8/4)); // 0.7 <-- not defect
document.writeln('<br>(7)not defect:2.8/3='+(2.8/3)); // 0.9333333333333332 <-- not defect//eval-defect
document.writeln('<br><br>eval-defect');
document.writeln('<br>(1)defect:2.9+2.8='+eval(2.9+2.8)); // 5.699999999999999 <-- defect
document.writeln('<br>(2)defect:2.9-2.8='+eval(2.9-2.8)); // 0.10000000000000009 <-- defect
document.writeln('<br>(3)defect:2.9-2='+eval(2.9-2)); // 0.8999999999999999 <-- defect
document.writeln('<br>(4)not defect:2.9X2='+eval(2.92)); // 5.8 <-- not defect
document.writeln('<br>(5)defect:2.9X2.3='+eval(2.92.3)); // 6.669999999999999 <-- defect
document.writeln('<br>(6)not defect:2.8/1.4='+eval(2.8/1.4)); // 2 <-- not defect
document.writeln('<br>(7)not defect:2.8/4='+eval(2.8/4)); // 0.7 <-- not defect
document.writeln('<br>(7)not defect:2.8/3='+eval(2.8/3)); // 0.9333333333333332 <-- not defect
</script>
addition 11 mar 2019 5:33 pm edt--------------
#reword# /* javascript-eval-result,javascript-standard-arithmetic-result:
#to# /* javascript-eval-result,javascript-simple-arithmetic-result:
i write this addition by clicking 'reply to this comment' on an icon located right-side of my comment.
Comment 48•5 years ago
|
||
http://wortel.ucoz.com/calculator.htm#17_apr_2019_6_29_pm_edt
maybe has correct-arithmetic,function division may produce many decimal-digit if those decimal-digit are all different not repetitive.
arithmetic-code is located in
find '//start math-arithmetic-function','//end math-arithmetic-function'
Description
•