3

Just do not understand why the following two have different values. The first one has value 0, while the other has value 1

if(1/10);
if(0.1);
user3506287
  • 39
  • 1
  • 3

2 Answers2

12

By default the type of 1 is int, thus 1/10 will be rounded down to 0 which is equivalent to false. While 0.1 has some bits set and is not 0.

On the other hand 1.0/10 is equivalent to 0.1.

Sergey L.
  • 20,104
  • 4
  • 42
  • 67
  • 4
    I think it's good to note, that even `1.0/10` and `0.1` are not necessarely equal, because of floating point numbers representation features. – FreeNickname Apr 07 '14 at 10:48
0
if(condition)

Here the condition is executed and checked with 0. If that is 0 that means false else that means true. 1/10 gives value 0 which is equal to 0 ( as integer/integer gives integer result) so condition failes, where as 0.1 is not equal to 0, so it is treated as true and currespoinding statements in if block will be executed.

Vinay
  • 188
  • 8