Re: Rounding a REAL



Steve Lionel wrote:
On Sep 14, 10:43 pm, t...@xxxxxxxxxxxxxxx wrote:

I would suggest:
ANINT(DBLE(X)*4.0D0) / 4.0D0
This is standard-conforming and takes advantage of the definition of
ANINT rounding away from zero.
This works as long as X is of a type that has a smaller dynamic range
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.)

As ttw took care to promote everything to double precision, the
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.
.



Relevant Pages

  • Re: Why (0.09+0.01-0.1) is not equal to 0.09+0.01-0.1 ?
    ... > gives full accuracy by default, but also includes rounding algorithms ... > partitioned mantissa by without risk of overflow ... > Function D2BAs String ... the work involved in avoiding lost precision due to type ...
    (microsoft.public.excel)
  • Re: Integers and standard
    ... then INTwill overflow ... with regard to BOZ when a BOZ is used with an INTEGER type in a ... DATA statement or used as an argument to INT. ... gfortran has at least a 64-bit integer type, ...
    (comp.lang.fortran)
  • Re: How to measure code execution time...?
    ... > functions in an embedded environment and handles all the hardware for ... If clock_t is an integer type, this will be equivalent because of ... = seconds, the first method will lose precision by casting, while the ... second method carries full precision. ...
    (comp.arch.embedded)
  • Re: double is integer?
    ... decimal digits of precision, and every implementation I've seen is exact ... integer type in question) or fractions that it becomes imprecise. ... range of int/long while relieving you of overflow worries. ...
    (microsoft.public.vc.mfc)
  • Re: Precision of one-bit bit field
    ... integer type is the same but including any sign bit; ... the width is one greater than the precision. ... expression would always yield zero on systems with symmetric representations ... 1's complement: the sign bit has a value of 1-2^N ...
    (comp.std.c)