Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Is this odd behavior a floating point problem?

by Eliya (Vicar)
on Mar 23, 2012 at 17:14 UTC ( #961266=note: print w/replies, xml ) Need Help??

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

For reasons described in excruciating detail in the document already cited, there will be errors, but according to a quick test on my system, they are nowhere near as large as you claim:

$ perl -e '$x=0.001; $sum += $x for 1..40; printf "%.20f", $sum/40' 0.00100000000000000067

Looks more like a "one off" error to me (i.e. summing over one more than you divide by):

$ perl -e '$x=0.001; $sum += $x for 0..40; printf "%.20f", $sum/40' 0.00102500000000000074

Replies are listed 'Best First'.
Re^2: Is this odd behavior a floating point problem?
by Anonymous Monk on Mar 24, 2012 at 00:47 UTC

    :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

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

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961266]
[LanX]: lanx wonders ... how likely is it to talk >95% BS without intention?
[erix]: "gigantic amounts of data" is also not SQLite (imho)
talexb wonders why sqlite2 was deprecated in favour of sqlite3.
[erix]: looks like a fork, rather, no?
LanX /me /me
[erix]: /hehehe
[marto]: LanX yesterday I found out about Gish gallop tactic
[marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
[marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2017-07-28 15:38 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.