Strange issue in floating point addition

This is a discussion on Strange issue in floating point addition within the C Programming forums, part of the General Programming Boards category; Hi All, I got a program which declares a float variable as f. and then iterates a for loop for ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Strange issue in floating point addition

    Hi All,
    I got a program which declares a float variable as f.
    and then iterates a for loop for 10 times adding a 0.1f to this variable
    well u assume the value is 1.0f but it is not true the value is something like 1.0000001
    why is this so. I tried this on windows using visual studio

    Regards,
    Stephen

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,327
    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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    I ran this code in linux/debian

    Code:
    #include<stdio.h>
    int main()
    {
            double a=0.0;
            int i ;
            for  (i = 1 ; i<=10 ; i++)
            {
                    a+=0.1f;
                    printf("value : %f\n",a);
            }
    }
    This works fine.

    Output is :
    Code:
    value : 0.100000
    value : 0.200000
    value : 0.300000
    value : 0.400000
    value : 0.500000
    value : 0.600000
    value : 0.700000
    value : 0.800000
    value : 0.900000
    value : 1.000000
    Last edited by Alexander jack; 02-18-2010 at 01:56 AM. Reason: Add the output

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Alexander jack View Post
    This works fine.
    Try some more output precision
    Code:
    #include <stdio.h>
    int main()
    {
    	double a=0.0;
    	int i ;
    	for  (i = 1 ; i<=10 ; i++)
    	{
    		a+=0.1f;
    		printf("value : %f  %0.9f\n",a,a);
    	}
    }
    Output:
    Code:
    value : 0.100000  0.100000001
    value : 0.200000  0.200000003
    value : 0.300000  0.300000004
    value : 0.400000  0.400000006
    value : 0.500000  0.500000007
    value : 0.600000  0.600000009
    value : 0.700000  0.700000010
    value : 0.800000  0.800000012
    value : 0.900000  0.900000013
    value : 1.000000  1.000000015
    Kurt

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Any idea why the answer is so

    Hi ZUK,
    Any idea why this sort of thing is happening , is it something to do with floating point addition algorithm used by the processor.

    Regards,
    Stephen

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The idea behind floating point is that it will NOT equal any particular decimal number, exactly. What it will do, if you work it right, is give you the answer beyond any reasonable resolution your program requires.

    It's a bit like Pi. We know that Pi approximately equals 3.1, but that isn't enough resolution for a lot of calculations. OK, how about 3.14159265358979?

    It's still NOT the value of Pi, but it's close enough to get you within one 1/100th of a millimeter, in a circle extending out to Jupiter.

    So that's probably good enough.

    There are ways of "normalizing" floats to an exact value, but it doesn't alter the fact that they have limits to their resolution, whether normalized or not.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,020
    Floating point numbers are stored in the form of 1.Nx2^M. This is much like scientific notation, except with base 2 instead of 10. As a result numbers such as 1/10 cannot be expressed accurately, because it would require infinate precision to represent it. This is similar to how 1/7 or 1/9 cannot be represented with a finite number of digits in base 10.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 04:29 PM
  4. Head Banging Floating Point Conversions
    By Davros in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 08:23 AM
  5. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM

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