Re: round double to float

Discussion in 'C++' started by John H., Apr 27, 2010.

  1. John H.

    John H. Guest

    On Apr 27, 10:06 am, Victor Bazarov <> wrote:
    > Michael D. Berger wrote:
    > > Can someone tell me how to round a
    > > double to a float?

    >
    > By assigning, in most cases, which on some compilers may require a cast
    > ('static_cast' would do, or a "functional style cast"), just to shut up
    > a warning about "loss of precision".


    So you now know how to round a double to a float. Another question
    you might be interested in is: what is rounding a double to a float?
    The word "round" can be a little ambiguous. Concepts associated with
    rounding could include "round up", "round down", "round towards zero",
    "round away from zero", "round to nearest representable, except when
    equidistant, then round up", etc. <cmath> has some functions related
    to this, and you can set the rounding mode. Also related, converting
    a float to an int actually performs "round towards zero" (truncation).
    Of course none of these really get at the exact thing you want to do:
    round a double to a float. The standard's [conv.double] describes
    floating point conversions. If you are lucky that the number of your
    double can be exactly represented by a float, then you will get the
    same number. If however, the double is between two values
    representable by a float, then which of the two float values it
    chooses is implementation defined.
    John H., Apr 27, 2010
    #1
    1. Advertising

  2. John H.

    John H. Guest

    On Apr 27, 12:09 pm, "Michael D. Berger" <>
    wrote:
    > I would prefer: "round to nearest representable, except when
    > equidistant, then round away from zero", but it is the last
    > in your comment that concerns me.


    A bit you might find interesting:
    http://www.gnu.org/software/libc/manual/html_node/Rounding.html#Rounding

    There is probably a canned version somewhere that I am not thinking
    about, but you might try rolling your own with something like this
    (not tested!):

    #include <limits>
    #include <cmath>

    float doubleToFloat(double num)
    {
    return nearbyint(num/std::numeric_limits<float>::epsilon()) *
    std::numeric_limits<float>::epsilon();
    }
    John H., Apr 27, 2010
    #2
    1. Advertising

  3. John H.

    John H. Guest

    On Apr 27, 12:37 pm, "John H." <> wrote:
    > On Apr 27, 12:09 pm, "Michael D. Berger" <>
    > wrote:
    >
    > > I would prefer: "round to nearest representable, except when
    > > equidistant, then round away from zero", but it is the last
    > > in your comment that concerns me.

    >
    > There is probably a canned version somewhere that I am not thinking
    > about


    You might look at Boost Numeric Conversion library:
    http://www.boost.org/doc/libs/1_42_0/libs/numeric/conversion/index.html
    I am not positive it can do what you want, but it looks like it might.
    John H., Apr 27, 2010
    #3
  4. John H.

    John H. Guest

    Michael D. Berger wrote:
    > I think that if
    > I use:
    >
    > if (fesetround(FE_TONEAREST) != 0)
    > throw <an error>;
    > double do = <get my stuff>;
    > float fl = static_cast<float>(do);
    >
    > I will have what I need (albeit slightly different and perhaps
    > better than my above-stated preference). Right?


    I think you are on the right track.

    A couple things to think about is that this is documentation for the
    GNU standard library. It describes behavior when using functions from
    the standard library, particular GNU's implementation. Of interest
    here perhaps is:
    1 - You are not using the standard library, you are using the
    conversion behavior of the language itself. Verifying if its
    description of library behavior also applies to the general
    conversions might be worth looking into.
    2 - GNU implementation might not be portable, although they are
    usually good at stating when behavior is GNU specific.

    Assuming neither of these points are an issue, FE_TONEAREST is the
    default rounding method, so you might not need to explicitly set it to
    that. (With that in hand, you are back to Mr. Bazarov's original
    suggestion.)
    John H., Apr 27, 2010
    #4
    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. Sydex
    Replies:
    12
    Views:
    6,452
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    0
    Views:
    1,070
  3. bd
    Replies:
    0
    Views:
    605
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,505
    James Kanze
    Oct 8, 2009
  5. OliverMarchand

    Float.round - should it be round-to-even

    OliverMarchand, Apr 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    209
    OliverMarchand
    Apr 12, 2006
Loading...

Share This Page