Is the equal?

Started by
3 comments, last by DvDmanDT 18 years, 8 months ago
float f = 1.7e38f; float ff = f + 1.3e22f; if (f == ff) cout<<"ok!";//output ok!
Advertisement
floating point types are not absolutely precise. They can only how so many significant figures. So in your cast is is like adding 10,000,000 + 1 = 10,000,001, except that since floats only have so many significant figures it rounds it back to 10,000,000. If you want something twice as precise use the double type instead. You will probably get better results. If that doesn't work, come back for fancier solutions.
floats are very inaccurate.. :p You shouldn't trust them.. :p
floats have around 24-bits of precision, doubles have around 53. f is about 10^16 times greater than ff. 10^16 is about 2^53... so even using a double is pushing it.
You could try a long double.. Probably 64+ bits of precision (80 bits in total).. Still, never trust them to be accurate..

This topic is closed to new replies.

Advertisement