In article <newscache$3p6kjh$xg3$>,
Kevin Easton <kevin@-nospam-pcug.org.au> wrote:
> Christian Bau <> wrote:
> > In article <bhb5u3$4he$>,
> > "Jonathan Fielder" <> wrote:
> >
> >> Hi,
> >>
> >> I have a 32 bit integer value and I wish to find the single precision
> >> floating point value that is closest to but less than or equal to the
> >> integer. I also have a similar case where I need to find the single
> >> precision floating point value that is closest to but greater than or
> >> equal
> >> to the integer. I believe that if I simply cast to a float, it may be
> >> assigned the next higher or lower representable value, depending on
> >> implementation.
> >>
> >> I am aware that if I use double precision floating point values then I
> >> shouldn't have a problem because 32 bit integers can be represented
> >> exactly,
> >> but I really need to use float.
> >>
> >> Is there a simple method using standard C to achieve my goal?
> >
> > Interesting problem. I think this should give the required result on
> > most or all correct C implementations.
> >
> > float int32_to_float_rounddown (long i) {
> >
> > double d = (double) i;
> > double e = d;
> > float f;
> >
> > while ((f = (float) e) > d)
> > e -= 1.0;
>
> Why do you think that 1.0 is the smallest amount you will have to
> subtract from e to make it less than i ?
This makes three assumptions: 1. All integers that fit almost into 32
bit can be stored exactly in a "double" variable. 2. Adding or
subtracting 1 to/from such a variable produces the correct result. 3.
The type float has the following property: There are two numbers fmin
and fmax such that all integers x, fmin <= x <= fmax can be represented
in a variable of type float, and no non-integer value less than fmin or
greater than fmax can be represented.
That would be the case for any simple floating point representation that
I have ever seen, and it wouldn't matter if it is binary, base 10, base
sixteen or whatever. (I know there are implementations of long double
that work differently).
|