#### Archived

This topic is now archived and is closed to further replies.

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

This topic is 5587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
It works. But can you give a little brief at background.?

##### Share on other sites
and what is this epsilon

##### Share on other sites
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]

##### Share on other sites
*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.

##### Share on other sites
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

##### Share on other sites
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
______________________________________________________________

##### Share on other sites
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.

##### Share on other sites
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
______________________________________________________________

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632960
• Total Posts
3009476
• ### Who's Online (See full list)

There are no registered users currently online

×