use strict; use warnings; my $NUM_A = 3335.999999999995; my $NUM_B = 3335.99999999995; # Normally, a 64-bit float can have 15 significant digits, # which is the mantissa of a number. If you try to write # 16 significant digits, you will lose precision!! And # that's what happens with $NUM_A. It has 16 significant # digit, and that's too long! # In JavaScript, you can store 3335.9999999999995 which # has 17 significant digits, but the numbers turns into 3336 # once you squeeze in another '9' in that list! # # In JavaScript, the largest mantissa you can have is: # 9007199254740992. If you try to add 1 to this number, # then the addition won't register, because it gets rounded # back to the original number. lol # # In JavaScript, if you try to add # 3335.9999999999995 + 0.0000000000001, it is equal to: # 3335.9999999999995 # So the addition won't even register, because it considers # that addition insignificantly small. The result won't change. # # In perl, it turns into 3336. lol # print " ADDITION: ", 3335.9999999999995 + 0.0000000000001, "\n"; print " SUBTRACT: ", 3335.9999999999995 - 0.0000000000001, "\n"; # Let's see what perl will do with this large number: print " BIGGEST JAVASCRIPT NUMBER: ", 9007199254740992, "\n"; print " BIGGEST JAVASCRIPT NUMBER: ", 900719925474099, "\n"; print " BIGGEST JAVASCRIPT NUMBER: ", 90071992547409, "\n"; print " BIGGEST JAVASCRIPT NUMBER: ", 9007199254740, "\n"; print " BIGGEST JAVASCRIPT NUMBER: ", 900719925474, "\n"; # "print" won't display the last significan digit. # so let's try this instead: if (9007199254740988 > 9007199254740987) { print "1 BIGGER OK.\n" } # + prints fine if (9007199254740989 > 9007199254740988) { print "2 BIGGER OK.\n" } # + prints fine if (9007199254740990 > 9007199254740989) { print "3 BIGGER OK.\n" } # + prints fine if (9007199254740991 > 9007199254740990) { print "4 BIGGER OK.\n" } # + prints fine if (9007199254740992 > 9007199254740991) { print "5 BIGGER OK.\n" } # + prints fine if (9007199254740993 > 9007199254740992) { print "6 BIGGER OK.\n" } # + <<this won't print! if (9007199254740994 > 9007199254740993) { print "7 BIGGER OK.\n" } # + prints fine if (9007199254740995 > 9007199254740994) { print "8 BIGGER OK.\n" } # + prints fine if (9007199254740996 > 9007199254740995) { print "9 BIGGER OK.\n" } # + <<this won't print! # See, as we start heading above that number, # we have problems in perl as well. # You can't tell whether a number is bigger or smaller, # because the last digit get rounded when it is stored in memory. # Okay, let's talk about divisions and remainders for a sec... print " NUM_A = $NUM_A \n NUM_B = $NUM_B\n"; my $R = $NUM_B % 3330; print " NUM_B % 3330 = $R (This should be 5.99999999995, but due to t +he strange way 64-bit floats are stored, it gets \"corrupted.\" So, n +ormally you'd get 5.9999999999499778. But in perl you don't even get +that result. You just get 5.)\n\n"; # So, in order to get the same result you would get # in JavaScript, you would call FMOD() function (see below) # instead of using the % (mod) operator: print " FMOD(NUM_B, 3330) = ", FMOD($NUM_B, 3330), "\n\n"; # # This function produces the same result as the % operator # in JavaScript: C = A % B; # # Usage: C = FMOD(A, B) # sub FMOD { my ($A, $B) = @_; return $A - int($A / $B) * $B; }
In reply to Re^3: Variables are automatically rounded off in perl
by harangzsolt33
in thread Variables are automatically rounded off in perl
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |