[Fortran, Patch] PR 34192 - Fix compile-time evaluation of NEAREST
Tobias Burnus
burnus@net-b.de
Fri Nov 23 20:53:00 GMT 2007
:ADDPATCH fortran:
We had several problems with the compile-time evaluation for NEAREST for
0.0 and subnormal numbers.
>From the standard:
-------------------- cut ---------------
"NEAREST (X, S)
Description. Returns the nearest different machine-representable number in a
given direction.
X shall be of type real.
S shall be of type real and not equal to zero.
Result Characteristics. Same as X.
Result Value. The result has a value equal to the machine-representable number
distinct from X and nearest to it in the direction of the infinity with the
same sign as S.
NOTE 13.16 Unlike other floating-point manipulation functions, NEAREST operates
on machine-representable numbers rather than model numbers. On many systems
there are machine-representable numbers that lie between adjacent model
numbers."
-------------------- cut ---------------
Problems:
a) gfortran gave an range error for the returned subnormal number
b) gfortran returned 0.0 as nearest number of 0.0, which is plainly wrong.
To (a): With the current patch, only for NaN an error is printed; one
might extend it to +/-INF if it is regarded as useful.
To (b): The fix was done in three steps:
1. Reverting to Steve's old patch
2. Fixing the returned result using Steve's suggestion
3. Fixing rounding of subnormal numbers
Thanks to Steve for the help! And to Paul Zimmermann, who send a similar
suggestion as Steve's for (2).
Build and regression tested on x86-64. OK for the trunk?
Tobias
PS: Any idea how to test for INF and NAN? If I do "0.0/0.0" I get a
compile-time error and -fno-range-check does not help. I cannot assign
to a variable first as otherwise NEAREST is not evaluated at compile time.
PPS: There seems to be a problem with the REAL(8) run-time version,
which returns wrong results for "NEAREST(subnormal, -1.0)". At least on
x86-64 Linux with glibc 2.6.1. See PR 34209.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: nearest.diff
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20071123/576c18fc/attachment.ksh>
More information about the Fortran
mailing list