Public Group

# Float subtraction error.

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

## Recommended Posts

Hello, this has really got me in a confused state of affairs and I was hoping that one of you helpful guys might be able to point me in the right direction. I am simply trying to subtract two floating point numbers, and all is going well until I am subtracting (0.101 - 0.001), instead of returning 0.1, it returns 0.0999998. I have this code in the main loop of my program and it works fine.
float testY = 0.101;
float testMove = 0.001;
cout << "testY: " << testY << endl;
cout << "testMove: " << testMove << endl;
cout << "testY - testMove : " << testY-testMove << endl;

Outputting.
testY: 0.101
testMove: 0.001
testY - testMove : 0.1

However, this piece of code in one of my classes breaks at that point.
cout << "\nCurY at start of move: " << curY << endl;
cout << "Moverate: " << MOVERATE << endl;
cout << "CurY - MOVERATE: " << curY-MOVERATE << endl;
curY = curY - MOVERATE;
cout << "CurY at end of move: " << curY << endl;

Which outputs: http://img484.imageshack.us/my.php?image=floaterrorey3.jpg .As you can see, it works fine up until 0.101-0.001 and then continues to work fine afterwards. I am hoping this is something stupid that I can't see, but it is evading me atm.

##### Share on other sites
Hehe, thanks for that. I have covered infinte numbers in finite space, and I understand that infinite numbers must be rounded for estimates when dealing with finite memory. I just can't get my head around all the maths and theorems on that page. I appreciate the link, but would it be ignorant of me to ask for a more...layman's, explaination?

##### Share on other sites
A shorter (and slightly less correct) explanation is that a float is only accurate to roughly six or seven decimal digits, so when it says "0.0999998" it actually means "0.0999998 +/- a bit in the last digit", which is equivalent to the expected answer (0.1).

In your screenshot example, there is also the issue that when it says "0.101" it actually means "0.101 +/- some rounding error after a few more digits" - the internal representation might be 0.1009996..., and it just gets rounded to 0.101 for display, and the fairly large error will propagate into any calculations you perform on that number.

##### Share on other sites
Moved to For Beginners.

##### Share on other sites
Hey, first of all, sorry for posting in the wrong section :oops:.

Secondly, thanks for all the help, I got the problem sorted this morning by rounding the numbers when appropriate. Basic collision detection is go!

Thanks again.

##### Share on other sites
Also, just to expand a little further, you should be aware of the implications of the above. You should not use direct comparisons (==) when comparing floats, and be aware that code like:

for (float f = 0.0f, int i = 0; f < 1.0f; f += 0.1f, ++i){    // I came across this sometime, but haven't tested it (and can't remember    // IEEE 754 in enough detail to know). Theoretically it iterates 11 times,    // not 10.    std::cout << i << std::endl;}

##### Share on other sites
Noted Unbeliever, I appreciate the advice.

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013612
×