To: J3 J3/14-183
From: David Muxworthy
Subject: UK proposal UK-21 Numeric conversion check
Date: 2014 June 09
Status: For Consideration
Basic Functionality: Numeric conversion check
Comment: This proposal is a development of UK-07 in N1975.
Rationale: It is tedious (and mistake-prone) to check if values of one
intrinsic type can be converted to another without error.
Specification:
Add a new intrinsic function or functions which can check whether a REAL or
INTEGER value can be converted to a different type (or kind) without error,
where "without error" means no integer or real overflow will occur.
The function(s) should be able to handle rounded conversions (NINT) when
checking for conversion to INTEGER, as well as truncating ones (INT).
For conversions between different REAL kinds, an IEEE infinity is not
considered to be an error unless it is not representable in the target
kind.
The conversions to be checked shall be those of:
REAL
INT
NINT
The functions AINT and ANINT (with a KIND= argument) need not be handled by
the feature, since their use for conversions is not as frequent, and it is
not too onerous to check using AINT/ANINT without KIND=.
The requirement to be able to handle NINT directly is because this is a
common conversion.
Syntax:
Add new logical intrinsic function:
OUT_OF_RANGE(X, MOLD, [, ROUND])
The function determines whether the value of X (integer or real) is
representable as an integer or real variable with the characteristics
of MOLD.
Edits to J3/14-007r1(= N2014):
[324] Following the line NUM_IMAGES in Table 13.1 add new line(s):
OUT_OF_RANGE (X, MOLD, [, ROUND]) E Whether a value can be
converted without error.
[378:23+] Insert new intrinsic function:
<<13.7.126a OUT_OF_RANGE (X, MOLD, [, ROUND])>>
<> Whether a value can be converted without error.
<> Elemental function.
<>
X shall be of type integer or real.
MOLD shall be an integer or real scalar. If it is a variable,
it need not be defined.
ROUND (optional) shall be a logical scalar.
ROUND shall be present only if X is of type real and
MOLD is of type integer.
<> Default logical scalar.
<>
Case (i): If MOLD is of type integer, and ROUND is absent or present with
the value false, the result is true if and only the value of X is
an IEEE infinity or NaN, or if the integer with largest magnitude
that lies between zero and X inclusive is not representable by
objects with the type and kind of MOLD.
Case (ii): If MOLD is of type integer, and ROUND is present with the value
true, the result is true if and only if the value of X is an
IEEE infinity or NaN, or if the integer nearest X, or the
integer of greater magnitude if two integers are equally near to
X, is not representable by objects with the type and kind of
MOLD.
Case (iii): Otherwise, the result is true if and only if the value of X is
an IEEE infinity or NaN that is not supported by objects of the
type and kind of MOLD, or if X is a finite number and the
result of rounding the value of X (according to the IEEE
rounding mode if appropriate) to the extended model for the
kind of MOLD has magnitude larger than that of the largest
finite number with the same sign as X that is representable by
objects with the type and kind of MOLD.
ALTERNATIVE WORDING FOR CASE (III):
Case (iii): Otherwise, if the value of X is an IEEE infinity or NaN, the
result is true if and only if that exceptional value is not
supported by objects of the type and kind of MOLD. Otherwise,
let X' be the result of rounding the value of X (according to
the IEEE rounding mode if appropriate) to the extended model
for the kind of MOLD; the result is true if and only if X' has
magnitude larger than that of the largest finite number with
the same sign as X that is representable by objects with the
type and kind of MOLD.
<>If INT8 is the kind value for an 8-bit binary integer type,
OUT_OF_RANGE(-128.5,0_INT8) will have the value false and
OUT_OF_RANGE(-128.5,0_INT8,.TRUE.) will have the value true.