Boost logo

Boost :

Subject: Re: [boost] [review][constrained_value] Review of Constrained Value Library begins today
From: Kim Barrett (kab.conundrums_at_[hidden])
Date: 2008-12-08 15:30:47

At 2:23 PM +0100 12/6/08, Robert Kawulak wrote:
>... maybe the problem
>could be somehow solved if we have a function float exact(float) that, given a
>floating point value (that may have greater precision because of caching in a
>register), returns a value that is truncated (has exactly the precision of
>float, not greater).

I think that something along the lines of the following will likely work:

inline double exact(double x) {
     struct { volatile double x; } xx = { x };
     return xx.x;

The idea is to force the value to make a round trip through a memory
location of the "correct" size. The use of volatile should prevent
the compiler from optimizing away the trip through memory.

If worried about whether a compiler will really do what's needed here,
then I think the only other option is inline assembler to produce the
equivalent behavior. Looking through the x86 instruction set, I don't
see any other way to cause the desired rounding.

Boost list run by bdawes at, gregod at, cpdaniel at, john at