Thread: floating point issues

  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 07:39 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,656
    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.

  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,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    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.

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, 04: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, 11:34 PM

Tags for this Thread