comparing floating point numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hendrixx
    New Member
    • Jun 2007
    • 3

    comparing floating point numbers

    i am posting codes and they give striking results can someone please explain why ???

    [code=c]main()
    { float a=0.7 ;
    if (a<0.7)
    { printf("c") ;
    }
    else
    printf("c++") ;
    }
    [/code]OUTPUT is c
    [code=c]main()
    { float a=0.7 ;
    if (a<0.7f)
    { printf("c") ;
    }
    else
    printf("c++") ;
    } [/code]
    OUTPUT : c++

    WHY I know in the first case 0.7 is a double and second it is taken as a float ....so wht ???
    Last edited by AdrianH; Jun 3 '07, 04:41 PM. Reason: Please use [code=c][/code] tags to improve readability
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You can't compare floating point numbers using the common <,>, ==,etc, operators. Due to rounding floating point numbers that are close in value may trigger the wrong result from these operators.

    Usually, you have to work withe th absolute value and do a comparsion within a sigma deviation that you specify.

    Your can Google "floating point arithmetic" and get a ton of advice.

    Also, a float is 4 bytes whereas a double is 8. Assigning a double to a float will produce truncation and loss of data. Do not mix these types.

    I recommend that unless you have a real technical reason that you can write down on paper, you should stick with doubles everywhere. float is an obsolete type from the days of computers with small memories. float is in C++ only so relic C code willl still compile.

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by weaknessforcats
      You can't compare floating point numbers using the common <,>, ==,etc, operators. Due to rounding floating point numbers that are close in value may trigger the wrong result from these operators.

      Usually, you have to work withe th absolute value and do a comparsion within a sigma deviation that you specify.

      Your can Google "floating point arithmetic" and get a ton of advice.

      Also, a float is 4 bytes whereas a double is 8. Assigning a double to a float will produce truncation and loss of data. Do not mix these types.

      I recommend that unless you have a real technical reason that you can write down on paper, you should stick with doubles everywhere. float is an obsolete type from the days of computers with small memories. float is in C++ only so relic C code willl still compile.
      float may still be used in real-time embedded systems with little memory, but as wfc said, it is mostly not very useful to use.


      Adrian

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by AdrianH
        float may still be used in real-time embedded systems with little memory, but as wfc said, it is mostly not very useful to use.
        I was going to make the same argument, however in most of the real-time embedded systems I have worked on we have avoided both float and double like the plague (not good for performance or memory usage as it tends to drag in a large maths library).

        I have to say that has not been a particular problem there are several tricks you can use in order to avoid there use. Integer storing units of 1 hundredth, or to ints 1 numerator 1 demoninator are both schemes I have used.

        I agree though, if you don't have a good reason use double.

        Comment

        • dumparun
          New Member
          • Feb 2007
          • 26

          #5
          Originally posted by Banfa
          I have to say that has not been a particular problem there are several tricks you can use in order to avoid there use. Integer storing units of 1 hundredth, or to ints 1 numerator 1 demoninator are both schemes I have used.
          Off topic though, could you please explain this

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by dumparun
            Off topic though, could you please explain this
            Basicly, he is using integer arithmetic in a way as to bypass the need to use a floating point number.

            [code=c]
            int x = 123; // really means x=12.3 if you say that x is actually 1/10th of what is stored.

            // this requires supporting functions.
            struct fraction {
            int n = 5; // numerator
            int d = 3; // denomonator
            };
            [/code]


            Adrian

            Comment

            • dumparun
              New Member
              • Feb 2007
              • 26

              #7
              Originally posted by AdrianH
              Basicly, he is using integer arithmetic in a way as to bypass the need to use a floating point number.

              [code=c]
              int x = 123; // really means x=12.3 if you say that x is actually 1/10th of what is stored.

              // this requires supporting functions.
              struct fraction {
              int n = 5; // numerator
              int d = 3; // denomonator
              };
              [/code]


              Adrian
              Ok... Got it right :)

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                What every computer scientist should know about floating-point arithmetic.

                kind regards,

                Jos

                Comment

                • hendrixx
                  New Member
                  • Jun 2007
                  • 3

                  #9
                  well i found the ans wer nd its not wht u all r saying .....floats or doubles can be compared using >= etc the ans lies in the internal representation of floats and doubles
                  float is represented using IEEE 754 single precision
                  double by IEEE 754 double precision so .7>.7f and so on.....thnx newayz

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by hendrixx
                    well i found the ans wer nd its not wht u all r saying .....floats or doubles can be compared using >= etc the ans lies in the internal representation of floats and doubles
                    float is represented using IEEE 754 single precision
                    double by IEEE 754 double precision so .7>.7f and so on.....thnx newayz
                    Can't was probably the wrong word to use you can use all the comparison operators on doubles and floats

                    ==
                    !=
                    >
                    <
                    >=
                    <=

                    However using == and != is virtually pointless as after any calculations have been performed it is unlikely that any 2 given floats will that approximate the same value will actually have the same value so typically == will always return false and != will always return true making them fairly useless.

                    Using > < >= and <= is OK as long as you when you do so you are fully aware that if you test <0.7 if you variable has the value 0.7000000000000 01 than the test will return false.


                    I hope you realise that "IEEE 754 single precision double by IEEE 754 double precision so .7>.7f " does not mean that for every approximation of a given number in single and double precision then the double precision representation is bigger than the single precision representation, that is

                    x > xf

                    is not true for all x

                    Comment

                    Working...