Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

expected 0 not result gotten

by mikeraz (Friar)
on Dec 21, 2011 at 19:28 UTC ( [id://944681]=perlquestion: print w/replies, xml ) Need Help??

mikeraz has asked for the wisdom of the Perl Monks concerning the following question:

mikeraz@hive:~> perl -e 'my $x = (.0425 * 2) + .0025; print $x - (.042 +5 * 2) - .0025, $/;' 2.16840434497101e-18 mikeraz@hive:~>

Silly me would expect something like 0 to be printed. Why the 2.16840434497101e-18? (OK, round it off and you have 0, but ....)

Digging into this I tried something "simplier" and got more confused:

mikeraz@hive:~> perl -e 'print ((.0425*2) + .0025 ) - ((.0425*2) + .00 +25 ); print $/;' 0.0875 mikeraz@hive:~>


Be Appropriate && Follow Your Curiosity

Replies are listed 'Best First'.
Re: expected 0 not result gotten
by toolic (Bishop) on Dec 21, 2011 at 19:34 UTC

      Oh F me. Thank you.


      Be Appropriate && Follow Your Curiosity
Re: expected 0 not result gotten
by quester (Vicar) on Dec 22, 2011 at 06:40 UTC

    To answer the second part of your question:

    Digging into this I tried something "simplier" and got more confused:

    perl -e 'print ((.0425*2) + .0025 ) - ((.0425*2) + .0025 ); print $/;'

    just bring the perlrun option -w (warnings) to the rescue:

    $ perl -we 'print ((.0425*2) + .0025 ) - ((.0425*2) + .0025 ); print $ +/;' print (...) interpreted as function at -e line 1. Useless use of subtraction (-) in void context at -e line 1. 0.0875

    That can be fixed with either extra parens or a plus sign:

    $ perl -we 'print (((.0425*2) + .0025 ) - ((.0425*2) + .0025 )); print + $/;' 0 $ perl -we 'print +((.0425*2) + .0025 ) - ((.0425*2) + .0025 ); print +$/;' 0

    There is a explanation of this in the perldoc for print:

    "Be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all arguments (or interpose a + , but that doesn't look as good)."

      surprising but true, i get something like, $ perl -we 'my $x = (.0425 * 2) + .0025; print $x - (.042+5 * 2) - .0025, $/;' -9.957 i am using perl 5.8.8

        Do you mean that it surprises you that

        .0425*2
        

        is different than

        .042+5*2
        

        Um... wow... really?

Re: expected 0 not result gotten
by sundialsvc4 (Abbot) on Dec 22, 2011 at 10:03 UTC

    Wise Man Say:   “Floating point numbers like little piles of sand.   Each time you pick one up and move it around, you lose a little sand and pick up a little dirt.”

      Except that's not quite true. Each time you pick one up and move it around, you may lose a little sand. "Sand" is only lost when the number has a huge (>53) number of digits in binary.

      This usually happens because of the use of numbers that are periodic in binary; they have an infinite number of digits. The OP used 425/10000 and 25/10000 which are both periodic in binary (like 1/3 is in decimal).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://944681]
Approved by toolic
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found