Quote:>Hi,
>Sounds like silly question, but I don't know why I got " 'initializing' :
>truncation from 'const double' to 'float' " warning from the following
>simple code.
>#include <stdio.h>
>void main()
>{
> float tmp = 3.57;
> printf("tmp: [%f]\n", tmp);
>}
By default, when you put a floating-point constant in your code (like
"3.57" here), it's treated as double-precision. So when you assign that
double-precision constant to a single-precision "float" variable, it loses
some precision. If you want to avoid this warning, you can either change
the tmp variable to a double, or change "3.57" to "3.57f" to make it a
single-precision constant.
Quote:>If I assign only 3.5 to tmp then I don't have the warning. I am using VC5 in
>Win95.
That's because 3.5 happens to be storable exactly in a single-precision
variable; 3.57 isn't--you can store something really really really _close_
to 3.57, but not 3.57 exactly.
If you assign a float variable to be equal to a double, you'll always
get the "possible loss of precision" warning, unless you say
"myFloat = (float)myDouble;", where the cast tells the compiler "Yes, I know
I'm losing precision, and I don't care." With constants like "3.5" and "3.57",
the compiler can see whether or not the constant will lose precision, and it
only gives the warning for constants that will actually lose precision.
--
\o\ If you're interested in books and stories with transformation themes, \o\
/o/ please have a look at <URL:http://www.halcyon.com/phaedrus>. Thanks! /o/
\o\ FC1.21:FC(W/C)p6arw A- C->++ D>++ H+ M>+ P R T++++ W** Z+ Sm RLCT \o\
/o/ a cmn++++$ d e++ f+++ h- i++wf p-- sm# /o/