XP is just a number PerlMonks

### expected 0 not result gotten

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

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.

Re: expected 0 not result gotten
by quester (Vicar) on Dec 22, 2011 at 06:40 UTC

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).

Create A New User
Node Status?
node history
Node Type: perlquestion [id://944681]
Approved by toolic
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-01-20 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (226 votes). Check out past polls.

Notices?