On 11/13/15 3:47 AM,
robin....@gmail.com wrote:
>> Another slightly more verbose would be
>> >
>> >f = x - aint(x)
> By subtracting the whole number part from x, you get the
> fractional part.
>
> Thus, if x = 9.876, you have aint(x) = 9.0, and you get 0.876.
>
> FRACTION delivers something entirely different.
I think this is probably the best approach in fortran. Yes, FRACTION()
returns the binary fraction from the internal floating point
representation, not the decimal fraction which is desired here.
A possible alternative is to use FLOOR(x) rather than AINT(x). The
former always rounds down (toward negative infinity), while the latter
rounds toward zero, so the correct choice depends on what you want to do
with negative values of x.
If you happen to want to always round up (toward positive infinity),
then you would need the CEILING() intrinsic, and if you want to round
toward the nearest integer then you would need the ANINT() intrinsic. I
just point out these options to show that fortran does indeed cover all
possibilities for rounding.
I just did a quick search on these intrinsics while writing this reply,
and I noticed that the gfortran documentation incorrectly uses the term
"whole number" to describe the function result of AINT() and ANINT().
That term usually means a nonnegative integer (0 or positive), which is
not correct for either of these intrinsics. I have not looked at the
standard to see what term is used there. In any case, what is returned
is a floating point number (REAL) that approximates the appropriate
integer value, and there is an optional KIND argument that allows
different KIND values to be returned. That argument may be important for
INTEGER values that are too large to be represented in the default real
kind.
$.02 -Ron Shepard