最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c - Is writing 0.00.0 valid in C99? - Stack Overflow

matteradmin4PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far