Thread: precision - making sure I understand this correctly.

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    precision - making sure I understand this correctly.

    Hi all,

    Just thought I'd discuss some basics on a nice sunny morning.

    When we are dealing with precision - in this case float - when it is said that float has 6 decimal digits precision - does that include the digits before the decimal point? I had assume d wrongly that it referred only to the digits after the decimal point. I am not using double here - examining float just right now.

    eg. this circle example highlights this. In the printf control strings I display to 6 places.

    Code:
      float radius = 0.0f;                   /* radius of table */
      float diameter = 0.0f;
      float circumference = 0.0f;
      float area = 0.0f;
      float pi = 3.14159265f;
      
      printf("\nInput the diameter of the table: ");
      scanf("%f", &diameter);
      
      radius = diameter/2.0f;
      circumference = 2.0f*pi*radius;        /* circumference = 2πr */
      area=pi*radius*radius;                 /* area = π^r2         */
      
      printf("\nThe circumference is %.6f", circumference);
      printf("\nThe radius is %.6f", radius);
      printf("\nThe area is %.6f\n", area);
      return 0;
    run1
    Input the diameter of the table: 1.1

    The circumference is 3.455752
    The radius is 0.550000
    The area is 0.950332 /* on my calculator it is 0.9503317 */
    hamster@ganymede:~/it/c/lessons$ ./2.8_circle

    run2
    Input the diameter of the table: 11

    The circumference is 34.557522
    The radius is 5.500000
    The area is 95.033180 /* on my calculator it is 0.95033176 */
    hamster@ganymede:~/it/c/lessons$

    In run1, the area is only accurate to 5 places after the decimal - so 0 before the decimal is part of 6 digits of precision.
    In run2, the area is only accurate to 4 places after the decimal - so 95 before the decimal is part of 6 digits of precision.

    I am basing these precisions on my compiler/machine version. So, having a large number before the decimal (eg 1024.4567) would drastically reduce the accuracy. Using format %.6f only would be good for floats like 0.xxxxx for example and 1024.xxyyyy would be inaccurate after 2 places beyond the decimal.
    Last edited by hamsteroid; 03-25-2007 at 04:28 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, it refers to the number of digits.

    Like so
    123456.0
    123.456
    0.123456

    Floats have a fixed number of bits (23 usually) to represent the mantissa (the number itself), and a further (8 usually) bits to represent the exponent (roughly where the decimal point goes).
    Since you need just over 3 binary bits to represent each decimal digit, this gives you the 6 digit precision.
    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.

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by Salem
    No, it refers to the number of digits.

    Like so
    123456.0
    123.456
    0.123456

    Floats have a fixed number of bits (23 usually) to represent the mantissa (the number itself), and a further (8 usually) bits to represent the exponent (roughly where the decimal point goes).
    Since you need just over 3 binary bits to represent each decimal digit, this gives you the 6 digit precision.
    Thanks Salem, just to add to that, you are saying that 0.123456 is counted as 6 digits - ie, the zero is not counted as long as it's zero before the decimal? (similarly 123456.0). Sorry for the confusion I was counting the digits.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Imagine all floats as
    000000000000123456xxxxxxxxxxxxxxxxx
    Somewhere in there is the decimal point, whether it's 1.23 or 1.23E10 or 1.23E-10

    Anything to the left of the most significant digit is always 0 (and not stored anywhere).
    Anything to the right of the 6th digit is "decreasingly inaccurate" (more inaccurate, but less significant).

    So the 7th digit is probably right (but might not be), and the rest have a 1:10 chance of being right (just chaotic noise in other words).
    To be exact, you need log(10)/log(2) bits for each decimal digit. Given 23 bits (in a float mantissa), you get 6.9236899 decimal digits of precision.
    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.

  5. #5
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Yes I got it! That's a pretty good answer. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Did I Install the Platform SDK Correctly?
    By bengreenwood in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2008, 09:33 AM
  2. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  3. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  4. Why does this not read in correctly?
    By bluebob in forum C Programming
    Replies: 9
    Last Post: 04-19-2002, 09:17 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM