Floating Point Approximations.
- From: "David Cressey" <cressey73@xxxxxxxxxxx>
- Date: Wed, 28 Mar 2007 14:28:00 GMT
Here's another example of floating point errors, that's even simpler than
the 21200 plus 2.5% example.
int i;
float x = 0;
for (i = 1; i < 100; ++i)
x += 0.01;
x -= 1;
printf (x);
I apologize if I copied the code wrong. The idea is to start with zero,
add one hundredth a hundred times, and subtract one. The answer is not
quite zero.
Many environments will give a small error. This is such a trivial example
that it should make all programmers shudder about using floats, unless they
really know what they are doing.
.
- Follow-Ups:
- Re: Floating Point Approximations.
- From: David Cressey
- Re: Floating Point Approximations.
- From: Jonathan Leffler
- Re: Floating Point Approximations.
- From: Bob Badour
- Re: Floating Point Approximations.
- Prev by Date: Re: What is the logic of storing XML in a Database?
- Next by Date: Re: Modelling Disjoint Subtypes
- Previous by thread: DBPL 2007 Call for Papers
- Next by thread: Re: Floating Point Approximations.
- Index(es):
Relevant Pages
|
|