Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

expected 0 not result gotten

by mikeraz (Friar)
on Dec 21, 2011 at 19:28 UTC ( #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

Comment on expected 0 not result gotten
Select or Download Code
Re: expected 0 not result gotten
by toolic (Chancellor) 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 (Monsignor) 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
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? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-09-15 04:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls