[clang] [C23] Add INFINITY and NAN macros to <float.h> (PR #96659)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 25 10:28:45 PDT 2024


AaronBallman wrote:

> I'm not sure about tying this to `__FINITE_MATH_ONLY__`; -ffinite-math-only doesn't mean infinity doesn't exist, it just means you're promising that you won't use floating-point arithmetic/comparison ops on infinity. Which is weird, but that's fast-math. Also, other implementations don't do this.

The requirement that you only define the macro if inf/nan "is supported" is new in C23, so I don't expect anyone to have implemented this yet. As for `__FINITE_MATH_ONLY__`, our [documentation](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-ffinite-math-only) mention "results" and that implies (at least to me) that an expression cannot form an infinity to begin with, so the act of trying to expand `INFINITY` is nonsensical in that case, right?

> In C99, INFINITY and NAN are macros in math.h, not float.h. Is it correct to check __STDC_VERSION__ >= 199901L?

In C99, C11, and C17 `INFINITY` and `NAN` are only defined in `math.h`. In C23, the definition is now in both `math.h` and `float.h`. The `float.h` requirement comes from 5.2.5.3.3 `Characteristics of floating types <float.h>` (p29 and p3), and the `math.h` requirement comes from 7.12 `Mathematics <math.h>` (p7 and p9). I'm not 100% certain of why they're in both, but I think it's for freestanding conformance reasons (math.h isn't [always] in freestanding but float.h [always] is).

https://github.com/llvm/llvm-project/pull/96659


More information about the cfe-commits mailing list