in reply to Re^2: [OT: JavaScript] JS remainder operation ('%')
in thread [OT: JavaScript] JS remainder operation ('%')
Try this in JavaScript:
var A = 900719066409336.9;
alert(A); // This will display: 900719066409336.9
Try this in Perl:
my $A = 900719066409336.9;
print $A; # This will print: 900719066409337
This is QBASIC 1.1 code:
LET A# = 900719066409336.9#
PRINT A# ' This will print: 900719066409336.9
So, there's the problem. As you can see, at one point, the FMOD() function that we have divides the numbers that you picked in your example, and then it takes the integer part of that. Everything works okay so far. We get the same result in all three languages. But then we multiply this number by 2147483647.83, and that's when things go bad.
In JavaScript and even in ancient QBASIC 1.1, we get the correct result. But in Perl, the double variable loses precision for some reason.
It gets more interesting, because if you use printf instead of print in Perl, you further get different results:
my $A = 900719066409336.9;
print $A; # This will print: 900719066409337
printf('%.1f', $A); # This will print 900719066409336.9
printf('%.2f', $A); # This will print 900719066409336.87
printf('%.3f', $A); # This will print 900719066409336.870
Man, this is weird! I have no idea what's going on.
|
|---|