CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2001
    Location
    California
    Posts
    1

    comparing floats

    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

  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: comparing floats

    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)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: comparing floats

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured