-1

Possible Duplicate:
Floating point issue in C

#include<stdio.h>
main()
{
        int a,b;
        float f;
        scanf("%2d%3d%4f",&a,&b,&f);
        printf("%d %d %f",a,b,f);
}

When i run this program and input 2 4 56.8 ,it gives output 2 4 56.799999.....but I would expect 2 4 56.8....why is it so???

Community
  • 1
  • 1

4 Answers4

6

That is correct. Floating point numbers are approximations. Just as 0.33333333 is an approximation to 1/3, 56.7999999 is an approximation for 56.8. There is no exact floating point representation for 0.1.

See some of what has been written:

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

56.8 is not exactly-representable in the floating-point format used by your C compiler. See What Every Computer Scientist Should Know About Floating Point Arithmetic (1991) by David Goldberg.

mlp
  • 809
  • 7
  • 21
0

It might have to do with floating point inaccuracy.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
cody
  • 61
  • 1
  • 4
0

All floating point numbers cannot be stored as its exact value, as the continuous space of the floating point number domain is mapped into a discrete levels. For example the floating point number can be stored in the IEEE754 single precision format. It stores the floating number in three parts in 32 bits of memory. Assume that [a, b] are the two closest floating point numbers that can be represented exactly with IEEE754 format, but we will have infinite number of floating point numbers in between a and b, in that case these numbers will be approximated with either a or b. This induces the imprecision you are experiencing.

The number is stored in normalized format, although some very small number can be stored in unnormalized format. Just have a look at the documents.

For example in your case 56.8 IEEE754 single point precision representation will be as follows:

  • sign: 0 (1 bit)
  • biased exponent: 10000100 (8 bits)
  • mantissa: 11000110011001100110011 (23 bits)

Now, if you convert back this number into decimal then you get

  • sign is 0 ie the value is positive
  • 10000100 = 132 bias amount 127 therefore exponent = 132 - 127 = 5
  • mantissa with hidden bit 1.11000110011001100110011
  • after adjusting decimal point with exponent (move 5 places right): 111000.110011001100110011
    • The integer part : 111000 = 2^5 + 2^4 + 2^3 = 56
    • The fraction part : .110011001100110011 = 2^-1 + 2^-2 + 2^-5 + 2^-6 + 2^-9 + 2^-10 + 2^-13 + 2^-14 + 2^-17 + 2^-18 = 0.799999237

Therefore when you retrieve the value from the IEEE754 single precision format it becomes: 56.799999237 where you have stored 56.8

You can play with IEEE754 representation here: http://www.h-schmidt.net/FloatConverter/IEEE754.html

phoxis
  • 60,131
  • 14
  • 81
  • 117