Casting integers to float.

Discussion in 'C Programming' started by Jonathan Fielder, Aug 12, 2003.

  1. 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?

    Many thanks,

    Jon.
     
    Jonathan Fielder, Aug 12, 2003
    #1
    1. Advertising

  2. 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;

    return f;
    }

    float int32_to_float_roundup (long i) {

    double d = (double) i;
    double e = d;
    float f;

    while ((f = (float) e)< d)
    e += 1.0;

    return f;
    }

    d and e must be double so that the conversions from 32 bit integer and
    from float are exact.
     
    Christian Bau, Aug 12, 2003
    #2
    1. Advertising

  3. Jonathan Fielder

    Kevin Easton Guest

    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.


    Will this work?

    float f = myint;

    if (f > (double)myint) {
    f -= FLT_EPSILON * myint;
    }

    - Kevin.
     
    Kevin Easton, Aug 13, 2003
    #3
  4. Jonathan Fielder

    Tim Prince Guest

    Jonathan Fielder wrote:


    >
    > 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.

    float f = myint -.25
    > 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.

    float f = myint +.25
    > I believe that if I simply cast to a float, it may be
    > assigned the next higher or lower representable value, depending on
    > implementation.

    Only for some of the values satisfying myint > 1/FLT_EPSILON, assuming a
    sane implementation, such as any IEEE compliant one.

    --
    Tim Prince
     
    Tim Prince, Aug 13, 2003
    #4
  5. In article <Adg_a.1316$>,
    Tim Prince <> wrote:

    > Jonathan Fielder wrote:
    >
    >
    > >
    > > 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.

    > float f = myint -.25


    Wrong result if myint = 1

    > > 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.

    > float f = myint +.25


    Wrong result if myint = 1

    > > I believe that if I simply cast to a float, it may be
    > > assigned the next higher or lower representable value, depending on
    > > implementation.

    > Only for some of the values satisfying myint > 1/FLT_EPSILON, assuming a
    > sane implementation, such as any IEEE compliant one.
     
    Christian Bau, Aug 13, 2003
    #5
  6. Jonathan Fielder

    Kevin Easton Guest

    Tim Prince <> wrote:
    > Jonathan Fielder wrote:
    >
    >
    >>
    >> 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.

    > float f = myint -.25


    If myint = 1, that gives 0.75 as f. There are many values representable
    in float that are closer to 1.0 than 0.75, whilst still being less than
    or equal to 1.0.

    - Kevin.
     
    Kevin Easton, Aug 13, 2003
    #6
  7. Jonathan Fielder

    Kevin Easton Guest

    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 ?

    - Kevin.
     
    Kevin Easton, Aug 13, 2003
    #7
  8. Jonathan Fielder

    Jirka Klaue Guest

    Kevin Easton wrote:
    > Christian Bau <> wrote:
    >>"Jonathan Fielder" <> wrote:

    ....
    >>>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 ?


    How about this?

    float f = i;
    double d = i;

    while (f > (double)i) {
    d -= 1;
    f = d;
    }

    while (f + FLT_EPSILON != f && f + FLT_EPSILON < (double)i)
    f += FLT_EPSILON;

    Jirka
     
    Jirka Klaue, Aug 13, 2003
    #8
  9. In article <newscache$3p6kjh$xg3$>,
    Kevin Easton <> 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).
     
    Christian Bau, Aug 13, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    631
  2. Replies:
    11
    Views:
    730
  3. techie

    casting unsigned integers

    techie, Oct 12, 2006, in forum: C++
    Replies:
    4
    Views:
    945
    Steve Pope
    Oct 13, 2006
  4. float and integers

    , Nov 15, 2008, in forum: C Programming
    Replies:
    2
    Views:
    220
    Keith Thompson
    Nov 15, 2008
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,549
    James Kanze
    Oct 8, 2009
Loading...

Share This Page