Velocity Reviews > C++ > Re: round double to float

# Re: round double to float

John H.
Guest
Posts: n/a

 04-27-2010
On Apr 27, 10:06*am, Victor Bazarov <(E-Mail Removed)> 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.
Guest
Posts: n/a

 04-27-2010
On Apr 27, 12:09*pm, "Michael D. Berger" <(E-Mail Removed)>
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/man....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.
Guest
Posts: n/a

 04-27-2010
On Apr 27, 12:37*pm, "John H." <(E-Mail Removed)> wrote:
> On Apr 27, 12:09*pm, "Michael D. Berger" <(E-Mail Removed)>
> 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

You might look at Boost Numeric Conversion library:
http://www.boost.org/doc/libs/1_42_0...ion/index.html
I am not positive it can do what you want, but it looks like it might.

John H.
Guest
Posts: n/a

 04-27-2010
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.)