[PATCH] stdlib: Make strtod/strtof set ERANGE consistently for underflow.

Joseph Myers joseph@codesourcery.com
Wed Jun 23 16:19:45 GMT 2021

On Tue, 22 Jun 2021, Keith Packard wrote:

> Thanks for this clarification; I didn't realize that underflow had this
> additional subtly. It sounds like this means that there are additional
> cases where errno should be set to ERANGE in the code. I'm afraid I
> don't understand the workings of this function well enough to know how
> to detect this particular case. Could we somehow detect this using
> exceptions? Or are there places in the code which could add some direct
> checks?

Detecting with exceptions is tricky, since (a) you'd need to save and 
clear any underflow exceptions raised before strtod is called, then merge 
them with the exceptions raised in strtod before returning and (b) the 
strtod code would also need to ensure that the correct exceptions are 
raised in all cases, which itself is tricky (and once you have logic to 
ensure the correct exceptions are raised in all cases, it's probably 
simpler to use that logic to drive setting errno as well, rather than 
deducing the errno setting from what exceptions were raised).  I'm not 
familiar enough with the newlib strtod code to know where you might need 
to add direct checks (including dealing with architecture-specific 
configuration of whether tininess is detected before and after rounding).

(The sort of case where before-rounding versus after-rounding tininess 
detection matters and a result rounding to the least normal value is still 
considered to underflow is: for round-to-nearest, if the mathematical 
result is between 1/2 (inclusive) and 3/4 (exclusive) of the way from the 
greatest subnormal value to the least normal value, then it rounds to the 
least normal value but still is considered to underflow for both 
before-rounding and after-rounding tininess detection; if it's at least 
3/4 of the way from the greatest subnormal value to the least normal 
value, it only underflows for before-rounding tininess detection but not 
for after-rounding tininess detection.)

Joseph S. Myers

More information about the Newlib mailing list