Click to See Complete Forum and Search --> : comparing floats


bpc007
November 6th, 2001, 12:58 AM
I am writing a program in C and I need to directly compare 2 floats. I can not use the == operator, so what can i do to compare them?

Brian, http://bpc007.cjb.net

Igor Soukhov
November 6th, 2001, 01:38 AM
It's aint easy to answer a question without complete understanding of it ... But I try:
Why you can't use == operator ? If it's kinda test you always can substruct one float variable from other and check the sign of the result.

Please - rate answer if it helped you
It gives me inspiration when I see myself in the top list =)

Best regards,

-----------
Igor Soukhov (Brainbench/Tekmetrics ID:50759)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com

Russian Software Developer Network http://rsdn.ru

Paul McKenzie
November 6th, 2001, 03:39 AM
Subtract the two values and see if the absolute value of the difference falls within a small tolerance level. If it does, then you can assume that the values are equal.

However, if you are writing a financial application using 'C', you should *never* use floats or doubles. Instead, you have to use integers, or get yourself a library that handles money values in exact amounts (a BCD library or some other type of large integer / fixed point library).

Regards,

Paul McKenzie