mysterious numbers in a float

This is a discussion on mysterious numbers in a float within the C Programming forums, part of the General Programming Boards category; Hi everyone, I just began to learn c, and here's the first problem I encountered, the text is about the ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    mysterious numbers in a float

    Hi everyone,
    I just began to learn c, and here's the first problem I encountered,
    the text is about the 3 data types (int, float, char). Here particularly, it talks about precision in floats. Just to test it, I set up a simple code, Here:


    Code:
    adrian@Adriansys:~/ccode$ cat floating.c
    #include <stdio.h>
    
    main()
    {
    
      float num1 = 91.273;
    
      printf("\n%.1f\n", num1);
      printf("%.2f\n", num1);
      printf("%.3f\n", num1);
      printf("%.6f\n", num1);
      printf("%f\n", num1);
      printf("%.14f\n", num1);
    
    }
    adrian@Adriansys:~/ccode$ ./floating
    
    91.3
    91.27
    91.273
    91.273003
    91.273003
    91.27300262451172
    adrian@Adriansys:~/ccode$

    And the question is, if num1 was made to be equal to 91.273, where are these numbers after .273 coming from? (The 0's are obvious, but the 26245........)

    Thanks for all the help,

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Floating point numbers are not exact when dealing with binary just like the decimal system cannot handle all possible decimal numbers in that representations (such as 10/3 ).

    Depending upon the system used, floating point numbers may be represented in many different fashions. One such fashion is the IEEE representation of binary numbers.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    147
    This is a classic issue - every programmer encounters it.

    It's been covered so many times, you're likely to get links to some reference on the material everywhere you ask.

    They key summary is that floats and doubles are approximations. Occasionally they return an exact value (like 3.0), and on other occasions they seem to refuse to accept an exact value (like 2.99). While there are means of dealing with the issue, there is no means of "correcting" a double or float (in a way that's consistent - it can be maddening).

    Two caveats in dealing with this fact. First, if you require precision, you'll need to use other means (large integers, such as storing values in pennies instead of dollars with decimals), or you'll have to accept the minor error. This means, two, that when comparing values, you often must consider a small range instead of a simple equality.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Thanks for the help guys,
    Now everything seems to be clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rounding numbers
    By maple23 in forum C Programming
    Replies: 3
    Last Post: 05-26-2008, 12:33 AM
  2. 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
  3. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 10:47 PM
  4. menu problems with functions
    By Spectrum48k in forum C Programming
    Replies: 3
    Last Post: 07-20-2002, 04:53 PM
  5. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 09:03 PM

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