John Harrison wrote:
You should normally use doubles. I can't see any reason not to in your
code. Obviously doubles have more precision and greater range, but
(apparently) on floating point hardware they are as fast as if, not
faster, than floats. So the only reason to use floats is to save space,
which isn't the case for your code.
I'm not even sure why I was using float there. Perhaps I just followed some
sample code. I've found a lot of C++ stuff uses floats where I would have
expected doubles. I will keep this issue in mind. My guess is the
performance impact could well be sensitive to the hardware, compiler, and
context. (ACK: I have read Sergey's response).
I usually write a simple round function, e.g.
int round(double x)
{
return static_cast<int>(d > 0 ? d + 0.5 : d - 0.5);
}
The 'automagic' conversion truncates towords zero, e.g.
1.9 ==> 1
-0.8 ==> 0
which doesn't sound like what you want.
I'm under the impression this is implementation dependent. I've only
glanced at the discussion, but it looks like the rounding style is part of
the descriptive datastructure required by the Standard. I'm looking at
18.2.1
namespace std {
template<class T> class numeric_limits {
public:
static const bool is_specialized = false;
static T min() throw();
static T max() throw();
static const int digits = 0;
static const int digits10 = 0;
static const bool is_signed = false;
static const bool is_integer = false;
static const bool is_exact = false;
static const int radix = 0;
static T epsilon() throw();
static T round_error() throw();
static const int min_exponent = 0;
static const int min_exponent10 = 0;
static const int max_exponent = 0;
static const int max_exponent10 = 0;
static const bool has_infinity = false;
static const bool has_quiet_NaN = false;
static const bool has_signaling_NaN = false;
static const float_denorm_style has_denorm = denorm_absent;
static const bool has_denorm_loss = false;
static T infinity() throw();
static T quiet_NaN() throw();
static T signaling_NaN() throw();
static T denorm_min() throw();
static const bool is_iec559 = false;
static const bool is_bounded = false;
static const bool is_modulo = false;
static const bool traps = false;
static const bool tinyness_before = false;
static const float_round_style round_style = round_toward_zero;
};
}
...
static const float_round_style round_style;
63 The rounding style for the type.206)
64 Meaningful for all floating point types. Specializations for integer
types shall return round_toward_zero.
18.2.1.3 Type float_round_style
namespace std {
enum float_round_style {
round_indeterminate = -1,
round_toward_zero = 0,
round_to_nearest = 1,
round_toward_infinity = 2,
round_toward_neg_infinity =3
};
}
So far as I know there is no explicit round() function native to C++ that
will round a float or double to an int or size_t. Is this correct?
Yes, presumably because there are several issues here. For instance which
algorithm to use for rounding (the above is only one possible), and how to
deal with overflow (which I've ignored above).
john
For now I think I'll buy the quick and dirty default behavior. If I can see
the consequence in the graphics, I'll consider a better solution.
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop:
http://www.kdevelop.org SuSE:
http://www.suse.com
Mozilla:
http://www.mozilla.org