in reply to 4253.95 + 0.9 = 4254.84999999999 (need help to interpret internals)

Thanks everyone for your replies!

I think that what confused me the most with this is that seemingly identical values don't behave the same way. I.e. the result of

perl -e "print 4253.95 + 0.9"

is 4254.85. But the result of my other values, which are 4253.95 and 0.9, is 4254.84999999999.

/J

  • Comment on Re: 4253.95 + 0.9 = 4254.84999999999 (need help to interpret internals)
  • Download Code

Replies are listed 'Best First'.
Re^2: 4253.95 + 0.9 = 4254.84999999999 (need help to interpret internals)
by hv (Prior) on Feb 26, 2005 at 13:40 UTC

    Your diagnostics are still not telling you the whole of the story, because Devel::Peek is performing the same sort of rounding on the NV value it shows you that perl normally does.

    If you want to look at these values in more detail, best to ask for it explicitly with sprintf:

    my $a = 4253.95; my $b = 0.9; my $c = $a + $b; print "$a + $b = $c\n"; printf "%.30f + %.30f = %.30f\n", $a, $b, $c;

    If you use this with your real code, it is more likely to show you the actual difference between the numbers you get when you enter them explicitly as above and those derived in your code. My guess would be accumulated rounding errors; another possibility would be something modifying $# (see perlvar), but trying to create a testcase to illustrate that I can't get it to do anything at all.

    Hugo