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.
- It does not claim the support – 0___________ Commented Nov 16, 2024 at 21:04
1 Answer
Reset to default 3Is 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.
- It does not claim the support – 0___________ Commented Nov 16, 2024 at 21:04
1 Answer
Reset to default 3Is 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
版权声明:本文标题:c - Is writing 0.00.0 valid in C99? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745658175a2161722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论