Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Bug? 1+1 != 2

by fglock (Vicar)
on Jun 19, 2003 at 06:45 UTC ( #267098=note: print w/replies, xml ) Need Help??

in reply to Bug? 1+1 != 2

They are not exactly the same. There is a very small rounding error:

#!/usr/bin/perl use strict; use warnings; my $base = 0.0425; while ( my $line = <DATA>){ chomp $line; my ($z,$c1,$c2,$c3,$c4) = split(/,/,$line); print join("|",$z,$c1,$c2,$c3,$c4),"\n"; my $total = $c1+$c2+$c3; print $c4 == ($total + $base) ? "True\n" : "False ". sprintf("%20.18f",$c4)." != ". sprintf("%20.18f",($total+$base))."\n"; } __DATA__ 00501,0.0000,0.0425,0.0025,0.0875 00544,0.0000,0.0425,0.0025,0.0875 06390,0.0000,0.0425,0.0025,0.0875


00501|0.0000|0.0425|0.0025|0.0875 False 0.087499999999999994 != 0.087500000000000008 00544|0.0000|0.0425|0.0025|0.0875 False 0.087499999999999994 != 0.087500000000000008 06390|0.0000|0.0425|0.0025|0.0875 False 0.087499999999999994 != 0.087500000000000008

Replies are listed 'Best First'.
Re: Re: Bug? 1+1 != 2
by shotgunefx (Parson) on Jun 19, 2003 at 10:52 UTC
    I figured as much, though I am still curious about the stringified representations. I've never thought too deeply about it. How does Perl determine the formatting for numbers when stringifying? $#?


    "To be civilized is to deny one's nature."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://267098]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2016-10-26 04:40 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (333 votes). Check out past polls.