Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By Salem

floating point issues

This is a discussion on floating point issues within the C Programming forums, part of the General Programming Boards category; The following piece of code outputs 0.1000000000001 Code: float a=0.1; printf("%lf",a); But if I add a 10 times as Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    floating point issues

    The following piece of code outputs 0.1000000000001


    Code:
    float a=0.1;
        printf("%lf",a);
    But if I add a 10 times as


    Code:
    long float b=0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1;
        printf("lf",b);
    It outputs as 0.999999999999989
    But if a=0.1 is stored as 0.1000000000001 then the answer should be greater than 10 due to the 1 in last column.Then why is the output less then 10
    Last edited by Saurabh Mehta; 11-28-2012 at 06:39 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    0.1 does NOT have an exact FP representation (it's an approximation).
    So adding 10 of them together will always result in an approximation for 1.0, rather than 1.0 exactly.

    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Thankyou for your answer actually I read the link that you gave before I posted this article..But my doubt was that since the value of 0.1 shown above is slightly greater than 0.1 then why does the addition yield a value less than 1.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    There is no rule that says the floating point approximation of 0.1 exceeds 0.1. It can be less - whether it is or not is a property of the floating point representation supported by your compiler and host system.

    And 0.1000000000001 cannot be represented exactly using floating point either.
    Saurabh Mehta likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Saurabh Mehta
    But my doubt was that since the value of 0.1 shown above is slightly greater than 0.1 then why does the addition yield a value less than 1.
    Maybe the intermediate computations (which might have been performed at compile time) resulted in rounding to a value very slightly less than 0.2, 0.3, etc. Maybe the conversion from float to long double (long float?! What?!) resulted in a slight change of value when the approximation was made.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    float a=0.1;
    printf("%lf",a);

    To start, 0.1 by itself is a double constant, so line 1 involves a 'double-to-float' conversion.
    Then on line 2, there is a 'float-to-double' conversion to call printf.

    > The following piece of code outputs 0.1000000000001
    A float has approximately 6 decimal digits of precision. So anything after the 6th digit is just random rounding noise.
    For a double, it's about 15 decimal digits.

    But since the lowest precision used was a float, you can't really trust past 6 digits anyway.
    Saurabh Mehta likes this.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating point
    By anirban in forum C Programming
    Replies: 4
    Last Post: 08-16-2007, 07:11 AM
  2. Floating point Hex.
    By cstubbs50 in forum C Programming
    Replies: 4
    Last Post: 11-16-2005, 03:29 PM
  3. Floating-Point (BCD)
    By zx-1 in forum C Programming
    Replies: 1
    Last Post: 10-15-2004, 01:11 AM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 10:34 PM

Tags for this Thread


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