Nitin Tripathi
Nitin Tripathi

Reputation: 1254

behaviour of float in C

#include<stdio.h>
int main()
{
   float f = 0.1;
   double d = 0.1;
   printf("%lu %lu %lu %lu\n", sizeof(f), sizeof(0.1f), sizeof(0.1), sizeof(d));
   return 0;
}

Output

$ ./a.out 
4 4 8 8

As per above code, we can see sizeof(0.1) and sizeof(0.1f) are not same. sizeof(0.1) is 8 bytes, while sizeof(0.1f) is 4 bytes. but while assigning the value to float variable f, it automatically truncates its size to 4 bytes.

While in below code, while comparing it with float x it is not truncating and 4 bytes of float are compared with 8 bytes of 0.1, value of float x matches with 0.1f as both are of 4 bytes.

#include<stdio.h>
int main()
{
    float x = 0.1;
    if (x == 0.1)
        printf("IF");
    else if (x == 0.1f)
        printf("ELSE IF");
    else
        printf("ELSE");
}

Output

$ ./a.out 
ELSE IF

why and how it is truncating while assigning and not while comparing?

Upvotes: 3

Views: 333

Answers (5)

ameyCU
ameyCU

Reputation: 16607

0.1 is a double value whereas 0.1f is a float value. The reason we can write float x=0.1 as well as double x=0.1 is due to implicit conversions .

But by using suffix f you make it a float type .

In this -

if(x == 0.1)

is flase because 0.1 is not exactly 0.1 at some places after decimal .There is also conversion in this to higher type i.e double.

Converting to float and then to double , there is loss of information as also double as higher precession than float so it differs .

Upvotes: 1

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22234

a floating point constant like 0.1 is a double unless specified as a float like 0.1f. The line

float f = 0.1;

means create a double with value 0.1 and cast it to float and lose precision in the process. The lines

float x = 0.1;
if (x == 0.1)

will cause x to be implicitly converted to double but it will have a slightly different value than for e.g. double x = 0.1;

Upvotes: 2

Nishant Kumar
Nishant Kumar

Reputation: 2169

when you write 0.1 , it is considered by default as double. suffix f explicitly make it float.

In second question float are stored as ieee standard so it it's going in else if because equivalent conversion of 0.1f to double is not same.

https://en.wikipedia.org/wiki/Floating_point

Upvotes: 1

fuz
fuz

Reputation: 93014

A floating point literal without a suffix is of type double. Suffixing it with an f makes a literal of type float.

When assigning to a variable, the right operand to = is converted to the type of the left operand, thus you observe truncation.

When comparing, the operands to == are converted to the larger of the two operands, so x == 0.1 is like (double)x == 0.1, which is false since (double)(float)0.1 is not equal to 0.1 due to rounding issues. In x == 0.1f, both operands have type float, which results in equality on your machine.

Floating point math is tricky, read the standard for more details.

Upvotes: 7

Ori Yampolsky
Ori Yampolsky

Reputation: 125

0.1f (the "f" after the number) is for the computer as float , that how your compailer know that he need to store it as float and not as double. so float 0.1 not equal to 0.1 , its equal to 0.1f

Upvotes: 1

Related Questions