Re: 1.0f vs. 1.0
Victor Bazarov wrote:
vectorizor wrote:
I am writing a significant amount of floating-point computational
routines as template, so float or double values can be used depending
on the accuracy needed. There are many numerical constants defined in
these routines, and they keep generating a warning in VC++ 8.0:
"warning C4244: '=' : conversion from 'double' to 'float', possible
loss of data"
This warning is obviously generated when the template is compiled with
type=float. I could get around by appending a f at the end of the
constants, i.e. "1.0f" vs "1.0", but that would lose precision when
doing computation in double. So I am looking for a workaround.
I understand that macro are not well loved around here, but is there a
way to define a macro in order to get rid of this problem?
It's better to define a special contant template and specialise it for
both double and float. Something like:
template<class F> struct Constants {
static F One;
static F Pi;
static F e;
static F tenbillion;
};
template<> static Constants<float>::One = 1.0f;
template<> static Constants<double>::One = 1.0;
template<> static Constants<float>::Pi = 3.141593f;
template<> static Constants<double>::Pi = 3.14159265358979323846;
...
The right solution is to turn off warning C4244, as it is
counterproductive. Any warning that induces you to write all kinds of
bloated verbose workarounds is bad news.
A warning that says you lose precision by converting 1.0 to 1.0f is a
bug in the compiler, as no precision is lost.
The losing precision warning is problematical anyway, as floating point
literals are often not even representable exactly in *any* precision. An
example is 0.3. Pi, of course, is another.
--------------
Walter Bright
http://www.digitalmars.com C, C++, D programming language compilers
http://www.astoriaseminar.com Extraordinary C++