2

I exactly don't know how why output is coming

    float f=1.4, t;
    int d,s;

    d=(int)f;
    printf("d=%d\n",d);
    t=f-d;
    printf("t=%f\n",t);
    t=t*10;
    printf("t=%f\n",t);
    s=(int)t;
    printf("s=%d\n",s);

the output is

d=1
t=0.400000
t=4.000000
s=3

and similarly when f=1.1

the output is

d=1
t=0.100000
t=1.000000
s=1

Is this related to the way the integer and float is stored in the memory or something else ?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Deepu
  • 131
  • 6
  • Nooooo, not again! You guys can't google "floating-point arithmetic unexpected result", can you... >. –  Feb 01 '14 at 21:44
  • 1
    @H2CO3 Please do the honors to google and then Post your answer ... thanks for being so polite :) – Deepu Feb 02 '14 at 11:58

3 Answers3

4

You have initialized f=1.4, not when you do

    d=(int)f;

You are converting float to integer, and when float is converted to integer, all the number after period "." are truncated. Now d has 1 so

    t=f-d;

will be 1.4 - 1 = 0.4

    t=t*10;

t=0.4*10=4 and as t is float so it outputs 4.0000

Float represent trailing zeros at the end

    s=(int)t;

Here again you are converting float to integer, Now here is tricky part, All the values above are rounded off, here t has value 3.99999976 so when converted to integer it shows 3 as a result

This is all because when you initialize t=1.4, in actual it is initialized to 1.39999998

Hamza
  • 1,593
  • 2
  • 19
  • 31
1

During the first assignment

float f=1.4;

there is an approximation because 1.4 is intended as double (not float). Something like 1.39999999 is assigned to f.

Try to use

float f=1.4f;

and it should work as you expect.

HAL9000
  • 3,562
  • 3
  • 25
  • 47
  • It should also be mentioned that printf first converts to double (since it is a variadic function) and then rounds what it prints, which is why it shows 1.400000 – ooga Feb 01 '14 at 21:32
  • @ooga printf in this case does nothing. s contains 3 because of the approximation I described. – HAL9000 Feb 01 '14 at 21:34
  • but when he prints t (a float) it converts to double before the call and printf rounds. – ooga Feb 01 '14 at 21:35
  • @ooga you're right, now I understood what you meant. – HAL9000 Feb 01 '14 at 21:37
1

Let's take it step-by-step and see how floating point and int interact.

Assume a typical platform where
float is IEEE 754 single-precision binary floating-point format and
double is IEEE 754 double-precision binary floating-point format

float f=1.4, t;
// 1.4 isn't exactly representable in FP & takes on the closest `double` value of
// 1.399999999999999911182158029987476766109466552734375
// which when assigned to a float becomes 
// 1.39999997615814208984375

int d,s;
d=(int)f;
// d gets the truncated value of 1 and prints 1, no surprise.
printf("d=%d\n",d);

t=f-d;
// t gets the value 0.39999997615814208984375
// A (double) version of t, with the same value is passed to printf()
// This is printed out, rounded to 6 (default) decimal places after the '.' as 
// 0.400000
printf("t=%f\n",t);

t=t*10;
// t is multiplied by exactly 10 and gets the value
// 3.9999997615814208984375
// A (double) version of t, with the same value is passed to printf()
// which prints out, rounded to 6 decimal places after the '.' as 
// 4.00000.
printf("t=%f\n",t);

s=(int)t;
// s gets the truncated value of 3.9999997615814208984375
// which is 3 and prints out 3. - A bit of a surprise.
printf("s=%d\n",s);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256