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" } # < 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" } # <