Home > Mobile >  Why does 0.0 == 0 means true in C language and when condition is 0.0 means false in branching statem
Why does 0.0 == 0 means true in C language and when condition is 0.0 means false in branching statem

Time:10-12

// first
    bool flag = 0.0 == 0;
    printf("the flag of 0.0 == 0 is %d\n", flag);

it turns out:

    the flag of 0.0 == 0 is 1

run this program tell me 0.0 equals to 0, but the 0.0 is storged like 0.0e0, which is different from the int 0 storged.

// second
    if (!0.0) {
        printf("0.0 is false\n");
    }

the second program turns out

    0.0 is false

in branching statement, why 0.0 means the false, since my condition didn't do any operations which will convert its type to int 0

CodePudding user response:

In 0.0 == 0 the int value it converted to double before the comparison is done (same as if you had written 0.0 == (double)0).

      0.0 == 0
// double    int

// the int is converted to double by the usual arithmetic conversions
      0.0 == (double)0

See C11 6.5.9: "Any two values of arithmetic types from different type domains are equal if and only if the results of their conversions to the result type determined by the usual arithmetic conversions are equal"

CodePudding user response:

run this program tell me 0.0 equals to 0 means false in branching statement.

No. The part of the program that reports on the result of 0.0 == 0 is this:

    printf("%d", flag);

and it prints "1", which corresponds to true. But the overall program output is deceptive, because you do not output anything to separate that "1" from the output of the next printf(). The overall output is

10.0 is false

, where the first "1" digit comes from the first printf and reports on the comparison of 0 with 0.0, and the rest, "0.0 is false", comes from the other printf() (because 0.0 indeed does evaluate to false in boolean context).

This would be clearer:

    bool flag = (0.0 == 0);

    printf("%d\n", flag);

    if (!0.0) {
        printf("0.0 is false\n");
    }

The output is:

1
0.0 is false

You go on to say,

but 0.0 is double type, while 0 is int type, i just want to know why C treat the different type mean the same

For most operations on arithmetic types, instead of rejecting operations on operands of mixed types or performing any kind of analysis based directly on the presence of mixed types, the operands are converted to a common type and the operation applied to the converted arguments. This even has a name and its own section in the standard: the "usual arithmetic conversions", C17 section 6.3.1.8. The idea is that when such type mixtures occur, the expected behavior is normally to operate on the numeric values of the operands, not to make type-based distinctions.

In your example of 0.0 == 0, the usual arithmetic conversions call for the int 0 on the right-hand side to be converted to type double to match the left-hand operand. The result is exactly 0.0, and the comparison evaluates to true (represented by the int value 1).

CodePudding user response:

Zero resolves to false, any other value resolves to true. This is specified by the standard of the language, see here:

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.

Once the standard is defined, it is followed.

CodePudding user response:

It is because '0.0' can hide something like '0.0001' and 0 is equals to 0, nothing more than 0.

  •  Tags:  
  • c
  • Related