The stupid question is the question not asked PerlMonks

### comment on

 Need Help??
I've done some testing with large numbers and floats in Perl and JavaScript. Here are the results:

```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;
}

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2023-03-26 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (63 votes). Check out past polls.

Notices?