Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Is this odd behavior a floating point problem?

by Anonymous Monk
on Mar 24, 2012 at 00:47 UTC ( #961339=note: print w/ replies, xml ) Need Help??


in reply to Re: Is this odd behavior a floating point problem?
in thread Is this odd behavior a floating point problem?

:D Looks like two separate off-by-one error (OBOE) errors to me :)

First you start with non-zero and add 40 times (one too many), then you start with non-zero and add 41 times (one too many twice).

If you start with non-zero you need to add only 39 times, or start with zero and add 40 times :)

In short

perl -MData::Dump -e " @f = map { 0.001 } 1 .. 40; dd\@f; $o = 0; for( +@f){ dd $o+=$_; } dd int @f; dd $o/int(@f); " perl -MData::Dump -e " $o = 0; for(1 .. 40){ dd $o+= 0.001; } dd $o/4 +0; "

It didn't dawn on me to check wickedjesters (or your) math until ww raised the quesiton


Comment on Re^2: Is this odd behavior a floating point problem?
Download Code
Re^3: Is this odd behavior a floating point problem?
by Eliya (Vicar) on Mar 24, 2012 at 01:09 UTC
    ... you start with non-zero ...

    Not sure what you're talking about.

    n times adding x to zero is mathematically (but not necessarily numerically) the same as n * x.

    $ perl -le '$sum += 1 for 1..40; print $sum' 40

    So where is the problem?  I think you overlooked that $sum is initially undef/zero.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961339]
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 2015-07-03 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls