unexpected float t=0.0f to t=1.0f FOR LOOP

Started by
11 comments, last by DirectXXX 20 years, 9 months ago
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.??
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
Advertisement
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.
It works. But can you give a little brief at background.?
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
and what is this epsilon
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
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]
*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.
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
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
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
quote:Original post by Thunder_Hawk
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...
Bad idea. Floating point error can go both ways; it''s entirely possible to end up with 1.09999999997 making the loop do another iteration.

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
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement