Identical float values comparing as inequal

VGupta

I was working on some 'determine the output' questions in C. I came across this question which looked simple on the face of it, but after running the code left me puzzled.

The output I expected was "True". However upon running, it was "False". And when I checked the value of f using printf(), it showed 0.1. Can someone please explain why f being assigned 0.1 doesn't return true for the IF statement?

There was no explanation for the answer from where I picked the question up, and I wasn't able to find an answer myself.

#include <stdio.h>

int main()
{
    float f = 0.1;
    if (f == 0.1)
        printf("True");
    else
        printf("False");
}
Vlad from Moscow

The problem is that variable f defined as having type float while float constant 0.1 has type double. Type double has more precision than type float. it has more binary digits to represent fraction. So in this statement

        float f = 0.1;

there is a truncation.

To get the expected result you should write at least

        if (f == 0.1f)

Also that to be sure that there is no rounding the code should look as

#include <stdio.h>

int main( void )
{
    float f = 0.1f;

    if ( f == 0.1f )
        printf("True\n");
    else
        printf("False\n");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why comparing float values is such difficult?

From Dev

Why does comparing two equal Float values with == returns false

From Dev

Comparing two images as being identical?

From Dev

Comparing 2 partitions on identical disks

From Dev

comparing identical strings returns false

From Dev

Comparing a float and its copy

From Dev

Comparing a float and its copy

From Dev

Java Comparing two identical objects gives false

From Dev

Comparing two Access tables identical in structure but not data

From Dev

Javascript: Comparing two identical object literals

From Dev

ruby comparing two identical strings as a condition for if else

From Dev

Python - comparing 2 identical objects returns False?

From Dev

assertEquals fails when comparing two identical lists

From Dev

Junit - Comparing that two CSV streams are identical

From Dev

Checking for identical hashtable values

From Dev

MySQL count identical values

From Dev

Why comparing two attributes type `float` and `int` with the same values get `False` in Python?

From Dev

Arrays are not identical after sorting even with identical values

From Dev

python comparing elements in dictionary to a float

From Dev

Laravel Crypt - Comparing Values

From Dev

Comparing two values in R

From Dev

Comparing EF timestamp values

From Dev

NetLogo: comparing neighbors' values

From Dev

Comparing values in any order

From Dev

Comparing values conditionally

From Dev

Comparing hexadecimal values with awk

From Dev

Comparing values between rows

From Dev

SQLite comparing time values

From Dev

Comparing if values exist in database