Unexpected behaviour with float

This is a discussion on Unexpected behaviour with float within the C Programming forums, part of the General Programming Boards category; Please consider the following code Code: void main() { float a = 0.7; if(a <= 0.7) printf("Less or equal"); else ...

  1. #1
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10

    Question Unexpected behaviour with float

    Please consider the following code

    Code:
    void main()
    {
      float a = 0.7;
      if(a <= 0.7)
         printf("Less or equal");
      else
        printf("Greater");
    }
    Surprisingly (for me) it printed
    Code:
    Greater
    However, when I change the initialization expression to the following
    Code:
    float a = 0.8;
    and change the if condition to
    Code:
      if(a <= 0.8)
    it prints the expected output:
    Code:
    Less or equal
    Kindly explain. Thank you in advance.

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Nothing should suprise you when using floating point. Even more strange that on other machines the first example will print the right answer.
    Read this. Check question 14.5
    Last edited by andor; 09-14-2006 at 09:38 AM.

  3. #3
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by j.sreejit
    Please consider the following code

    Code:
      float a = 0.7;
      if(a <= 0.7)
         printf("Less or equal");
      else
        printf("Greater");
    Kindly explain. Thank you in advance.
    You must check that the floating point is accurate to within a certain tolerance, i.e. plus or minus a very small value away from the value you are comparing with must be considered acceptably 'equal'.

    Try this program. It takes a bunch of positive floating point numbers and then multiplies by 10 (or whatever you pass in as the first program argument), does the same operation with negative versions of the values and then shows the difference between the result and the value expected when assigned from a constant. When the multiplier is 10 the difference mathematically would be zero but some values will be slightly inaccurate. Different values will be inaccurate by different amounts on different platforms.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*
      Show how fractional decimal values cannot be stored in finite number
      of binary digits
    */
    int main(int argc, char ** argv)
    {
     double pos, neg, pdiff, ndiff;
     int f = 10;
     int i;
     double r;
     double cpos[10] = { /* constants representing expected results */
      11.0,
      22.0,
      33.0,
      44.0,
      55.0,
      66.0,
      77.0,
      88.0,
      99.0,
      110.0
     };
     double cneg[10] = { /* constants representing expected results */
      -11.0,
      -22.0,
      -33.0,
      -44.0,
      -55.0,
      -66.0,
      -77.0,
      -88.0,
      -99.0,
      -110.0
     };
     if (argc > 1)
     {
      f = atoi(argv[1]);
     }
     printf("\t%10s %10s %10s %10s %10s\n", "i", "pos", "diff", "neg", "diff");
     pos = 0.0;
     for (i = 0; i < 10; i++)
     {
      pos += 1.1;          /* a range of positive decimal fractions */
      r = pos * f;         /* simple multiplication by 10 */
      pdiff = cpos[i] - r; /* is result different from constant */
      neg = -pos;          /* a range of negative decimal fractions */
      r = neg * f;         /* simple multiplication by 10 */
      ndiff = cneg[i] - r; /* is result different from constant */
      printf("\t");
      printf("%10d ", i);
      printf("%10.2g ", pos);
      printf("%10.2g ", pdiff);
      printf("%10.2g ", neg);
      printf("%10.2g ", ndiff);
      printf("\n");
     }
     return 0;
    }
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Well that's what you get for using void main.

    It's also for mixing floats and doubles.

    You could try
    if ( a <= 0.7f )

    This question has appeared several times in the past.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Salem
    This question has appeared several times in the past.
    It has also appeared many times in the future.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Ah, that would be the whole "history repeating itself, because it has to" deal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10
    Thank you risby, andor and salem for replying. Andor, that link was really informative. Thanks.
    Last edited by j.sreejit; 09-14-2006 at 10:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21