On 03/19/2012 02:40 PM, Sian Mountbatten wrote:
> Has anybody used the functions such as fegetround and fesetround. What
> do these functions return?
7.6p8, describing <fenv.h> says:
"Each of the macros
FE_DOWNWARD
FE_TONEAREST
FE_TOWARDZERO
FE_UPWARD
is defined if and only if the implementation supports getting and
setting the represented rounding direction by means of the fegetround
and fesetround functions. Additional implementation-defined rounding
directions, with macro definitions beginning with FE_ and an uppercase
letter,210) may also be specified by the implementation. The defined
macros expand to integer constant expressions whose values are distinct
nonnegative values."
7.6.3.1p3: "Description
The fegetround function returns the value of the rounding direction
macro representing the current rounding direction or a negative value if
there is no such rounding direction macro or the current rounding
direction is not determinable."
7.6.3.2p2: "Description
The fesetround function establishes the rounding direction represented
by its argument round. If the argument is not equal to the value of a
rounding direction macro, the rounding direction is not changed.
Returns
The fesetround function returns zero if and only if the requested
rounding direction was established."
> I'm trying to find a function which will round a double to
> (1) the nearest integer, with 0.5 rounded to 0.0 and 1.5 to 2.0
That is a popular rounding mode for certain kinds of analysis, but not
one defined by the standard. Your implementation might support it: look
at <fenv.h> to find out.
> (2) the nearest integer near zero, with 1.5 rounded to 1.0 and -1.5 to -1.0
For this kind of rounding, you don't need to worry about the floating
point environment, because that is precisely the defined behavior for
conversion of a floating point value to a signed integer type, which is
by far the simplest way to perform the conversion, if the value is small
enough to fit in some integer type.
If you're looking at floating point values too large to be represented
by any signed integer type, it's unlikely that the floating point value
is represented with sufficient precision to make rounding necessary.
However, in the unlikely case that it does have sufficient precision,
trunc(), or more likely truncl(), has the behavior you desire,
regardless of the current rounding mode.