Skip to main content
GameDev.net gamedev.net
🔒 Locked

Float comparsion

Started by zel Apr 18, 2001 at 2:01 PM 2 replies 500+ views
Original Post
zel
zel
Would this be a good way to compare two floats ? Just figured it out so I haven''t check it yet myself. I will but have to go and shop me some food before the stores close. bool equal(float a, float b) { int m; m = mantissa(a); m += 128; m &= 0xFFFF00; mantissa(a) = m; exp(a) += mantissa_carry(m); m = mantissa(b); m += 128; m &= 0xFFFF00; mantissa(b) = m; exp(b) += mantissa_carry(m); return a == b; }
Martee
Martee
Or you could just do it the inaccurate way...
float TOLERANCE = 0.00001
if(fabs(a - b) < TOLERANCE) return true;

~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
SteveC
SteveC
C++ Report had an article on using template meta programming for float comparisons with defining a variable tolerance.

You can find some info on it at

http://www.creport.com/html/from_pages/view_recent_articles_c.cfm?ArticleID=396



zel
zel
quote:
Original post by Martee

Or you could just do it the inaccurate way...
float TOLERANCE = 0.00001
if(fabs(a - b) < TOLERANCE) return true;

~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill


I almost forgot about this post.

Well, the above code is kinda of what I''m trying to avoid since it only works for numbers in a limited range. Not acceptable for me. I need to use both very large and small numbers.

Besides I think my approach would be faster and pretty easy and efficient to extend to the other cases. (<=, >...)

Another quiz. Is it possible to compare floating points values with integer comparison. I mean, the component of floating points values (x86) are stored in order (sign, exp, mantissa).
So isn''t it possible to threat a float number as an int and use the integer comparison ? That would further speed up my code.
(I will already have the modified values in a register)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.