admin管理员组

文章数量:1026989

MSVC does not accept 0.0 / 0.0 in some contexts, but it does accept them in others. Is this standards compliant? Which part of the standard is relevant?

This triggers error C2124: divide or mod by zero:

double f(void) {
    double a = 0.0 / 0.0;
    return a;
}

This does compile without errors:

double a = 0.0 / 0.0;

double f(void) {
    return a;
}

Clang and GCC compile both without errors.

This came up while trying to work around a (probably buggy) non-constant NAN in recent MSVC, see Recent MSVC versions don't treat NAN as constant, workaround? I am looking to see if 0.0 / 0.0 is a valid replacement in some contexts.

EDIT: Needless to say, I am looking for answers that consider extensions to the standard that deal with practical floating point arithmetic, including support for NaN values. Wikipedia refers to this as "Annex F IEC 60559 floating-point arithmetic". I would like to know if MSVC is justified in rejecting 0.0 / 0.0 given that it claims IEEE 754 support.

MSVC does not accept 0.0 / 0.0 in some contexts, but it does accept them in others. Is this standards compliant? Which part of the standard is relevant?

This triggers error C2124: divide or mod by zero:

double f(void) {
    double a = 0.0 / 0.0;
    return a;
}

This does compile without errors:

double a = 0.0 / 0.0;

double f(void) {
    return a;
}

Clang and GCC compile both without errors.

This came up while trying to work around a (probably buggy) non-constant NAN in recent MSVC, see Recent MSVC versions don't treat NAN as constant, workaround? I am looking to see if 0.0 / 0.0 is a valid replacement in some contexts.

EDIT: Needless to say, I am looking for answers that consider extensions to the standard that deal with practical floating point arithmetic, including support for NaN values. Wikipedia refers to this as "Annex F IEC 60559 floating-point arithmetic". I would like to know if MSVC is justified in rejecting 0.0 / 0.0 given that it claims IEEE 754 support.

Share Improve this question edited Nov 16, 2024 at 13:14 Szabolcs asked Nov 16, 2024 at 12:46 SzabolcsSzabolcs 25.7k11 gold badges88 silver badges187 bronze badges 1
  • It does not claim the support – 0___________ Commented Nov 16, 2024 at 21:04
Add a comment  | 

1 Answer 1

Reset to default 3

Is writing 0.0 / 0.0 valid in C99?

C language

No, it is not valid C and results in undefined behaviour according to the C standard.

However, if the macro __STDC_IEC_559__ is defined, then the operation is well-defined, as it would follow the IEEE 754 standard for floating-point arithmetic. Under IEEE 754, 0.0 / 0.0 should result in a quiet NaN.

MSVC

In the case of MSVC, the compiler does not define __STDC_IEC_559__ (https://godbolt./z/dKh45b778), which means it does not claim compliance with IEEE 754. Microsoft states that their floating-point implementation is "consistent with IEEE 754," but this is not the same as full compliance.

Given that MSVC does not conform to IEEE 754, it is correct for the compiler to treat 0.0 / 0.0 as undefined behaviour, which is why it emits an error in some contexts.

As for why MSVC emits an error in certain cases but not others, the reasoning is unclear. I have a theory, but it is difficult to confirm: MSVC likely applies different rules for constant expression evaluation depending on the context (e.g., global scope versus local scope), leading to this inconsistent behaviour.

MSVC does not accept 0.0 / 0.0 in some contexts, but it does accept them in others. Is this standards compliant? Which part of the standard is relevant?

This triggers error C2124: divide or mod by zero:

double f(void) {
    double a = 0.0 / 0.0;
    return a;
}

This does compile without errors:

double a = 0.0 / 0.0;

double f(void) {
    return a;
}

Clang and GCC compile both without errors.

This came up while trying to work around a (probably buggy) non-constant NAN in recent MSVC, see Recent MSVC versions don't treat NAN as constant, workaround? I am looking to see if 0.0 / 0.0 is a valid replacement in some contexts.

EDIT: Needless to say, I am looking for answers that consider extensions to the standard that deal with practical floating point arithmetic, including support for NaN values. Wikipedia refers to this as "Annex F IEC 60559 floating-point arithmetic". I would like to know if MSVC is justified in rejecting 0.0 / 0.0 given that it claims IEEE 754 support.

MSVC does not accept 0.0 / 0.0 in some contexts, but it does accept them in others. Is this standards compliant? Which part of the standard is relevant?

This triggers error C2124: divide or mod by zero:

double f(void) {
    double a = 0.0 / 0.0;
    return a;
}

This does compile without errors:

double a = 0.0 / 0.0;

double f(void) {
    return a;
}

Clang and GCC compile both without errors.

This came up while trying to work around a (probably buggy) non-constant NAN in recent MSVC, see Recent MSVC versions don't treat NAN as constant, workaround? I am looking to see if 0.0 / 0.0 is a valid replacement in some contexts.

EDIT: Needless to say, I am looking for answers that consider extensions to the standard that deal with practical floating point arithmetic, including support for NaN values. Wikipedia refers to this as "Annex F IEC 60559 floating-point arithmetic". I would like to know if MSVC is justified in rejecting 0.0 / 0.0 given that it claims IEEE 754 support.

Share Improve this question edited Nov 16, 2024 at 13:14 Szabolcs asked Nov 16, 2024 at 12:46 SzabolcsSzabolcs 25.7k11 gold badges88 silver badges187 bronze badges 1
  • It does not claim the support – 0___________ Commented Nov 16, 2024 at 21:04
Add a comment  | 

1 Answer 1

Reset to default 3

Is writing 0.0 / 0.0 valid in C99?

C language

No, it is not valid C and results in undefined behaviour according to the C standard.

However, if the macro __STDC_IEC_559__ is defined, then the operation is well-defined, as it would follow the IEEE 754 standard for floating-point arithmetic. Under IEEE 754, 0.0 / 0.0 should result in a quiet NaN.

MSVC

In the case of MSVC, the compiler does not define __STDC_IEC_559__ (https://godbolt./z/dKh45b778), which means it does not claim compliance with IEEE 754. Microsoft states that their floating-point implementation is "consistent with IEEE 754," but this is not the same as full compliance.

Given that MSVC does not conform to IEEE 754, it is correct for the compiler to treat 0.0 / 0.0 as undefined behaviour, which is why it emits an error in some contexts.

As for why MSVC emits an error in certain cases but not others, the reasoning is unclear. I have a theory, but it is difficult to confirm: MSVC likely applies different rules for constant expression evaluation depending on the context (e.g., global scope versus local scope), leading to this inconsistent behaviour.

本文标签: cIs writing 0000 valid in C99Stack Overflow