On Saturday 08 December 2007 01:10:43 pm Brian Hurt wrote:
> > `./floats.pl | wc -l` = 101
> >
> > oops! Thats one too many.
>
> This is a fencepost error, not a floating point inaccuracy.
>
> Brian
>
It's a fencepost error caused by a floating point inaccuracy. In real world
math, you'd add .1 to 0 until it got to exactly 10, which would fail the loop
conditional. In a computer's floating point math rounding errors start to
creep in, and what should be your last iteration at 10 that stops the loop
ends up being slightly less than 10 and allows the loop to iterate one too
many times.
very end of the output....
9.79999999999998
9.89999999999998
9.99999999999998
10.1
In python it's even more amusing....
float = 0.0
interval = .1
max = 10.0
while float < max:
float += interval
print float
very end of input....
9.6
9.7
9.8
9.9
10.0 <--- 10.0 is less than 10.0!
10.1
Stick to your embedded systems. Leave the high-level languages to people that
can handle them. ;)
--
Thanks,
Josh Paetzel
PGP: 8A48 EF36 5E9F 4EDA 5A8C 11B4 26F9 01F1 27AF AECB
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: This is a digitally signed message part.
Url : http://mailman.mn-linux.org/pipermail/tclug-list/attachments/20071208/5452b378/attachment.pgp