3

I came across this piece of code :

int x=3;
float y=3.0;
if(x==y)
  printf("x and y are equal");
else
  printf("x and y are not equal");

Why does this code print "x and y are equal"?? Here if y=3.1(say), then the code prints "x and y are not equal". Someone please explain how is this happening.

kasif
  • 57
  • 1
  • 8
  • when the values are equal, the expression `x==y` produces `true`, and the `if`-`else` executed the first `printf` statement. when the values are not equal, this does not happen. – Cheers and hth. - Alf Jun 05 '14 at 18:28
  • 3
    Are you asking why an `int` value of `3` is equal to a `float` value of `3.0` ? (and just fyi, comparing floating-point with *anything* for *equality* is a [**terrible idea**](http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples)). – WhozCraig Jun 05 '14 at 18:29
  • Also see [this related question](http://stackoverflow.com/questions/22143032/about-comparing-an-integer-and-a-float-double-in-c-c?rq=1) – Chris O Jun 05 '14 at 18:31
  • My question is basically directed towards the behaviour of '==' operator. I can predict it from the result of my code but I want a clear understanding of the same. – kasif Jun 05 '14 at 18:33
  • 1
    @WhozCraig: Comparing floating-point numbers for equality is a fine enough idea if you know what you're doing. – tmyklebu Jun 05 '14 at 19:22
  • @tmyklebu does this look like a question that would be asked by someone that meets your qualification? – WhozCraig Jun 05 '14 at 19:28
  • 1
    @WhozCraig: No. However, filling people's heads with superstitions is a good way to make sure people *don't* figure out what's going on. – tmyklebu Jun 05 '14 at 19:55
  • @tmyklebu I concur. I provided one of several links to the subject discussed on this forum in detail. Hopefully it is investigated (was somewhat the point of providing it in the first place). – WhozCraig Jun 05 '14 at 19:57
  • @WhozCraig: The link you posted is irrelevant. Note that 3 is exactly representable as a `float` and that no arithmetic is taking place here. All your comment does is reinforce the unfortunately widely-held superstition that floating-point numbers are not to be trusted. – tmyklebu Jun 05 '14 at 20:02
  • 2
    Note that because each of 32-bit int and 32-bit float contain values that do not exit in the other type, telling whether a 32-bit int and a 32-bit float represent the same value can be tricky. See for instance http://gynvael.coldwind.pl/?id=535 and https://twitter.com/spun_off/status/467929922259144704 – Pascal Cuoq Jun 05 '14 at 20:07
  • @tmyklebu If that was what you read from that so be it. I never claimed, nor intended to claim, that floating point numbers are cannot be "trusted". Had that been my intention, I would have said so. I referred specifically to *equality* comparison. That the OP's number of choice happened to be one that can withstand exact representation doesn't change that fact *at all*. – WhozCraig Jun 05 '14 at 20:14
  • possible duplicate of [Is relational comparison between int and float directly possible in C?](http://stackoverflow.com/questions/1161199/is-relational-comparison-between-int-and-float-directly-possible-in-c) – Ciro Santilli OurBigBook.com Jun 15 '15 at 16:00

3 Answers3

16

Comparisons between arithmetic types are subject to the so-called usual arithmetic conversions (§5/9, §5.9/2, §5.10/1). Emphasis mine.

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

— If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.

— If either operand is of type long double, the other shall be converted to long double.

— Otherwise, if either operand is double, the other shall be converted to double.

Otherwise, if either operand is float, the other shall be converted to float.

— Otherwise, the integral promotions (4.5) shall be performed on both operands. Then the following rules shall be applied to the promoted operands:

— If both operands have the same type, no further conversion is needed.

— Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank.

— Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.

— Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type.

— Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
4

When you try to compare an int with a float, the int gets converted to float first.

So 3 == 3.0f actually tests float(3) == 3.0f, which is true.

On the other hand, 3 == 3.1f tests float(3) == 3.1f, which is false.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
-2

In this question x is an integer value whereas y is a float value... an integer value and a float value cannot be equated...so inorder to check equality we have to typecast either float to integer or vice versa... Hope this may help to clear your concept

Shivani
  • 1
  • 1