Re: Rounding a REAL
- From: Tim Prince <timothyprince@xxxxxxxxxxxxx>
- Date: Sat, 15 Sep 2007 07:22:58 -0700
Steve Lionel wrote:
On Sep 14, 10:43 pm, t...@xxxxxxxxxxxxxxx wrote:As ttw took care to promote everything to double precision, the
I would suggest:This works as long as X is of a type that has a smaller dynamic range
ANINT(DBLE(X)*4.0D0) / 4.0D0
This is standard-conforming and takes advantage of the definition of
ANINT rounding away from zero.
than double precision. Else the problem of overflow arises. It's a
good practical solution though.
It is also limited by the range of the integer type, though if you
have a larger integer type available that can help. I'm guessing that
for the purpose intended, this solution will do. I agree that one
needs to be careful about the range of X - single precision is good to
about 6 or 7 decimal digits, so double would be better (and then would
not need the DBLE call.)
properties of integer types don't enter in here. Actually, the only
point in promoting to higher range and precision, when no integer type
is used, would be to avoid overflow when multiplying by 4.
Maybe you are hinting at the possible alternative
nint(x*4,selected_int_kind(precision(1d0)+1))*.25
on the assumption that you have such an integer type available. Any
integer type conversion would fail if x*4 would overflow.
As OP said a range of 1400 would be sufficient, this seems overkill.
I don't read into OP statement any requirement about rounding away from
zero (presumably meaning the cases of odd multiples of 0.125, where
Fortran rounding differs from IEEE, thus requiring lengthier code).
If anyone is looking for more choices, when using a compile mode which
avoids extra precision, there is
((x*4d0 + sign(1/epsilon(4d0),x)) - sign(1/epsilon(4d0),x))*.25
(assuming no use of ieee_set_rounding_mode). This "suffers" from IEEE
"bankers" rounding. It would work just as well in the data type of x,
except for that possibility of overflow.
.
- Follow-Ups:
- Re: Rounding a REAL
- From: Steve Lionel
- Re: Rounding a REAL
- References:
- Rounding a REAL
- From: Luka Djigas
- Re: Rounding a REAL
- From: Steve Lionel
- Re: Rounding a REAL
- From: ttw
- Re: Rounding a REAL
- From: Steve Lionel
- Rounding a REAL
- Prev by Date: Re: g77 on Cygwin
- Next by Date: Re: g77 on Cygwin
- Previous by thread: Re: Rounding a REAL
- Next by thread: Re: Rounding a REAL
- Index(es):
Relevant Pages
|