<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><br id="lineBreakAtBeginningOfMessage"><div><br><blockquote type="cite"><div>On Aug 21, 2024, at 7:25 PM, Fred J. Tydeman <tydeman@tybor.com> wrote:</div><br class="Apple-interchange-newline"><div><div>On Wed, 21 Aug 2024 17:42:17 -0700 Jim Thomas wrote:<br><blockquote type="cite"><br><blockquote type="cite">Suppose, this long string to nan() were treated as a range error (overflow).<br></blockquote><br>Does some implementation do this? An implementation isn't permitted to add a range errors unless the floating result overflows or underflows.<br></blockquote><br>glibc sets errno to ERANGE if there are lots of digits in the string.<br></div></div></blockquote><div><br></div>I think this is clearly a bug. Range errors (in C23 and in all previous versions of C) are about numeric results being too large or too small in magnitude for the format. The nan() functions return NaNs, which are not numeric and have no magnitude.</div><div><br></div><div>Back to Richard Smith’s questions:</div><div><br></div><div><blockquote type="cite">From: Richard Smith <<a href="mailto:richardsmith@google.com">richardsmith@google.com</a>><br>To: ISO C <<a href="mailto:sc22wg14@open-std.org">sc22wg14@open-std.org</a>><br>Date: Fri, 2 Aug 2024 16:32:14 -0700<br>Subject: [SC22WG14.26260] Can nan set errno?<br><br>Hi,<br><br>The documentation of errno is clear that it can be set to any non-zero<br>value by a function whose specification doesn't mention errno. The<br>specification of nan doesn't mention errno. Therefore one might conclude<br>that nan can choose to set errno to any non-zero value.</blockquote><br></div><div><div><blockquote type="cite">However, the specification of nan says that it's equivalent to a call to<br>strtod, and the specification of strtod *does* mention errno, which<br>presumably means that the strtod call that a call to nan is equivalent to<br>must leave errno unchanged (the condition for setting errno to ERANGE is<br>presumably not met since a NAN value should presumably not be considered to<br>overflow even if the n-char-sequence doesn't fit in the payload).<br><br>So which interpretation wins? Is a call to nan really equivalent to a call<br>to strtod, or is it allowed to affect errno? (There's a similar situation<br>for atof, but it explicitly mentions errno in its specification to say that<br>it may or may not have strtod's effect on errno, so the rule from 7.5/3<br>that it may be set to any non-zero value doesn't apply.)<br><br></blockquote></div><div><br></div><div>I suggest the following response:</div><div><br></div><div><div><div>As specified, the nan() function really is equivalent to the corresponding use of strtod(). The nan() function is not allowed to affect errno. </div><div><br></div><div>7.5 #3 says:</div><div><br></div></div><blockquote style="margin: 0px 0px 0px 40px; border: medium; padding: 0px;"><div><div><div>… The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this document.</div></div></div></blockquote><div><br></div>strtod() documents use of errno for overflow and underflow, which prohibits other use of errno, including for the case that is equivalent to the nan() function. <br><div><div><div><br></div></div>The specification of <math.h> functions includes the general specification in 7.12. Subclause 7.12.2 #2 says </div><div><br></div><blockquote style="margin: 0px 0px 0px 40px; border: medium; padding: 0px;"><div><div>For all functions, a domain error occurs if and only if an input argument is outside the domain over which the mathematical function is defined. The description of each function lists any required domain errors; an implementation may define additional domain errors, provided that such errors are consistent with the mathematical definition of the function.</div><div><br></div></div></blockquote>There are similar statements for range and pole errors. This specification would (in the absence of the equivalence to strtod) permit nan() to set errno but only in a way consistent with the meaning of the error. This qualification clearly excludes range errors (which pertain to numeric results being too large or small for the format) and pole errors. Since the meaning of the n-char-sequences is implementation-defined (see strtod 7.24.2.6 #5), one might be tempted to say some n-char-sequences are outside the domain of the function and define a domain error, but that is not an intended interpretation and the equivalence to strtod prohibits it.</div><div><br></div><div>- Jim Thomas</div><div><br><blockquote type="cite"><div><div><br><br><br>---<br>Fred J. Tydeman        Tydeman Consulting<br>tydeman@tybor.com      Testing, numerics, programming<br>+1 (702) 608-6093      Vice-chair of INCITS/C (ANSI "C")<br>Sample C17+FPCE tests: http://www.tybor.com<br>Savers sleep well, investors eat well, spenders work forever.<br></div></div></blockquote></div><br></div></body></html>