Strange results with float subtraction

Hello! Recently, I was writing a small game in SFML when I ran into a strange problem involving the subtraction of floats. Here's a small snippet of the code I'm talking about:

 
std::cout << intersect.top << ", " << position_.y << ", " << intersect.top - position_.y << std::endl


Which produced the following output:


512.679, 511.679, 0.999969


Even stranger, subtracting 1 from this results in -3.05176e-005?
Stranger still, try as I may, I can't seem to be able to reproduce this result; if I load up a new console application and do this very subtraction the output is correct - 1.

I've been trying to debug this for upwards of a week, what in the world is going on here?

Last edited on
Quick explanation:
http://www.parashift.com/c++-faq/floating-pt-errs.html

Involved explanation:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

There's nothing to "debug" there.
If you want more precise floating point numbers make a "BigInt" class.
Topic archived. No new replies allowed.