1

PYTHON PROGRAM:

a = 0.2
if a == 0.2:
    print('*')

OUTPUT:

*

C PROGRAM:

#include <stdio.h>

int main(void) 
{
    float a = 0.2;
    if(a == 0.2)
    {
        puts("*");
    }
}

OUTPUT:


Why is the output different in both cases? Is there a difference between the working of == operator?

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 6
    In the `a == 0.2` in C, 0.2 is a _double_ while a is a _float_. Change it to `if(a == 0.2f) { .. }`. If that works / changes the behavior, search for "Is floating point math broken?" – user2864740 Jun 15 '20 at 17:59
  • 3
    [Very related question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). Short summary: Don't ever do "exact comparison" using floating point values. Always use an *epsilon*. – Some programmer dude Jun 15 '20 at 18:01
  • 2
    Also, it's often hard to do direct comparison between different languages with different semantics. Even for small limited areas where syntax might seem the same, the behavior might be vastly different. – Some programmer dude Jun 15 '20 at 18:02
  • Alse [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane Jun 15 '20 at 18:03
  • @Some programmer dude Not even in Python? –  Jun 15 '20 at 18:06
  • 1
    @NSR No not even in Python can it be reliable. Lets say you have made two different functions that take different input and do some calculation on the input. Using a specific set of inputs should mathematically produce an exact equal result, but due to inaccuracies in representation and possibly compounded rounding errors, the two results are not *exactly* equal, just very close. – Some programmer dude Jun 15 '20 at 18:12
  • @Some programmer dude Yeah, but while printing Python just approximates, right? –  Jun 15 '20 at 18:22
  • @Someprogrammerdude "Don't ever do "exact comparison" using floating point values. Always use an epsilon." is an overstatement. Comment space too small for a thorough explanation. – chux - Reinstate Monica Jun 15 '20 at 19:30

1 Answers1

3

It is because the types float and double have different width reserved for the mantissa. The type double can represent a floating number more precisely. In this case that matters as 0.2 can't be represented exactly and has a very slightly different representation when stored as a double vs. a float.

In the condition

if(a == 0.2)

the left operand has the type float while the right operand has the type double, as the default type of a number literal with a "." in C is a double.

So change the declaration the following way

double a = 0.2;

Or alternatively change the condition like

if(a == 0.2f)

Here is a demonstrative program

#include <stdio.h>

int main(void) 
{
    float a1 = 0.2;

    if ( a1 == 0.2f )
    {
        puts( "*" );
    }

    double a2 = 0.2;

    if ( a2 == 0.2 )
    {
        puts( "*" );
    }
}

Its output is

*
*
user2864740
  • 60,010
  • 15
  • 145
  • 220
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335