unexpected float t=0.0f to t=1.0f FOR LOOP
I just recently noted it.
for (float t=0.0f; t<=1.0f; t+=0.1f)
this was supposed to end at 1.0f but it doesnt. it ends at 0.9f
Also it works when im using double instead of float. What is the reason.??
because you should never be checking for equality between floating point numbers, because of precision errors.
Try this:
for (float t=0.0f; t<=1.0f + epsilon; t+=0.1f)
where epsilon is 1e-5 or so.
How appropriate. You fight like a cow.
Try this:
for (float t=0.0f; t<=1.0f + epsilon; t+=0.1f)
where epsilon is 1e-5 or so.
How appropriate. You fight like a cow.
The FPU is not accurate.
For example 123456789 is converted to 1234567936 with 32 bit floats. Epsilon is a value that defines a range in which two numbers are treated as equal.
[edited by - novum on July 3, 2003 5:34:20 PM]
For example 123456789 is converted to 1234567936 with 32 bit floats. Epsilon is a value that defines a range in which two numbers are treated as equal.
[edited by - novum on July 3, 2003 5:34:20 PM]
*shrug* okay.
It helps to know a little bit about how floating point numbers are stored. Basically, they store an exponent and a mantissa (and a sign-bit), like scientific notation, except with base-2. So numbers that are not powers of two are only represented approximately. When those numbers are printed, they usually come out close enough to be rounded to the intended number, but compounded error (such as would be produced by successively adding to a number) can result in numbers like 1.000000000123, which is something like what the last value in your for-loop was.
How appropriate. You fight like a cow.
It helps to know a little bit about how floating point numbers are stored. Basically, they store an exponent and a mantissa (and a sign-bit), like scientific notation, except with base-2. So numbers that are not powers of two are only represented approximately. When those numbers are printed, they usually come out close enough to be rounded to the intended number, but compounded error (such as would be produced by successively adding to a number) can result in numbers like 1.000000000123, which is something like what the last value in your for-loop was.
How appropriate. You fight like a cow.
loop on ints!
#define DELTA_T 0.1
float t=0.0f;
for(int i=0; i<10; i++)
{
do stuff with t;
t += DELTA_T;
}
At least this is the "right" solution
#define DELTA_T 0.1
float t=0.0f;
for(int i=0; i<10; i++)
{
do stuff with t;
t += DELTA_T;
}
At least this is the "right" solution
Or you can do:
for (float t = 0.0f; t < 1.1f; t+=0.1f)
Treating the float the way you probably would an int...
______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
for (float t = 0.0f; t < 1.1f; t+=0.1f)
Treating the float the way you probably would an int...
______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
quote:Original post by Thunder_HawkBad idea. Floating point error can go both ways; it''s entirely possible to end up with 1.09999999997 making the loop do another iteration.
Or you can do:
for (float t = 0.0f; t < 1.1f; t+=0.1f)
Treating the float the way you probably would an int...
How appropriate. You fight like a cow.
quote:Original post by Fredrik Dahlberg
loop on ints!
#define DELTA_T 0.1
float t=0.0f;
for(int i=0; i<10; i++)
{
do stuff with t;
t += DELTA_T;
}
At least this is the "right" solution
Why not just go:
for (int i = 0; i < 11; i++) {
float t = 0.1f*i;
}
and do away with cumulative error all together
______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement