Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr] It is underspecified when evaluating an operation whose operands have values designated NaN #5407

Closed
xmh0511 opened this issue Apr 22, 2022 · 7 comments

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Apr 22, 2022

Consider this example

auto sNaN = std::numeric_limits<float>::signaling_NaN();
auto r0 = sNaN  +  0;
bool r1 = sNaN  == 0;
bool r2 = sNaN  != sNaN;
auto r3 = sNaN  * 1;
auto r4 = sNaN  / 1;
int n = sNaN ;
unsigned int un = sNaN ;

I only list parts of operators that are unclear to NaN, in every subclause that corresponds to the operator or conversion used the above, we do not have a wording that can define what the result is. The only possible relevant wording is in [expr.pre] p4

If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

It is unclear whether NaN is mathematically defined. If it were not, it would mean that even the following example also is UB

auto sNaN = std::numeric_limits<float>::signaling_NaN();
int c = (sNaN , 10);
@jensmaurer
Copy link
Member

jensmaurer commented Apr 22, 2022

Yes. C++ is silent on the properties of floating-point operations, and that includes the treatment of infinities or NaN. Those are undefined behavior by virtue of not being expressly defined to anything in particular.

C has an Annex that maps floating-point operations to IEEE semantics, including proper NaN treatment. Nobody has proposed something like that for C++, yet.

This needs a full-blown paper targeted at EWG, with rationale etc.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Apr 23, 2022

C++ is silent on the properties of floating-point operations, and that includes the treatment of infinities or NaN.

Those are undefined behavior by virtue of not being expressly defined to anything in particular.

I don't understand it. Since the operations defined for float-point types have defaultly covered infinite and 'NaN', why do we say these values are not explicitly expressed and thus UB?

@jwakely
Copy link
Member

jwakely commented Apr 23, 2022

It's implementation-defined whether any infinity and NaN values exist at all.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Apr 23, 2022

It's implementation-defined whether any infinity and NaN values exist at all.

That means, all the operations associated with infinity and NaN should be implementation-defined rather than undefined behavior. However, this point is not clear in the current draft. At least, it can be exposed according to the above comment that we all have different opinions regarding floating-point values.

In most subclauses of operations defined in [expr], we merely define rules for arithmetic types without having the special rules for floating-point types. We just have a hazy in floating-point types. Anyway, we have a standard way to acquire infinity and NaN

auto infinity = std::numeric_limits<float>::infinity();
auto QNaN = std::numeric_limits<float>::quiet_NaN();
std::cout<< infinity  << QNaN ; // #1

#1 is UB or implementation-defined? Moreover, when we make these acquired values(by standard ways) participate in the operations defined in [expr], what are the results? UB or implementation-defined, or partial well-defined?

@jensmaurer
Copy link
Member

At best, it's implementation-defined.

Note that std::numeric_limits does not necessarily deliver the values you seek, depending on the details of the platform's floating-point format.

There is lots of room for improvement here. One avenue (as I already said) would be to actually specify the relationship to IEC 60559 for those platforms whose floating-point is conforming to that.

Quoting my earlier comment:

This needs a full-blown paper targeted at EWG, with rationale etc.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Apr 23, 2022

There is lots of room for improvement here.

Agree. I also don't think we can clarify all of them in an editorial fix. I just want to make this issue be recorded or marked with a CWG tag if possible? Such that we can fix them in the future.

@jwakely
Copy link
Member

jwakely commented Apr 23, 2022

But it's not a CWG issue. There is no defect here. If you want changes, write a paper for EWG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants