'initializing' : truncation from 'const double' to 'float' warning

'initializing' : truncation from 'const double' to 'float' warning

Post by Joey Juyun Ch » Sat, 17 Apr 1999 04:00:00



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);

Quote:}

If I assign only 3.5 to tmp then I don't have the warning. I am using VC5 in
Win95.
Thanks,  Joey
 
 
 

'initializing' : truncation from 'const double' to 'float' warning

Post by Chris Marriot » Sat, 17 Apr 1999 04:00:00



>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);
>}

Because "3.57" is a double precision constant and is losing precision when
you assign it to a "float" variable. To get rid of the warning, use a single
precision constant:

    float tmp = 3.57f;

Quote:>If I assign only 3.5 to tmp then I don't have the warning.

That's because "3.5" is rather unusual in that it has an exact floating
point binary representation, so not precision is being lost by the
assignment.

Chris
-----------------------------------------------------------------------

Visit our web site at http://www.skymap.com
Astronomy software written by astronomers, for astronomers

 
 
 

'initializing' : truncation from 'const double' to 'float' warning

Post by Phaedr » Mon, 19 Apr 1999 04:00:00




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/
 
 
 

1. {'a','b','\0'} not equivalent to "ab"??

Given a function like
    void f(char *);
we all know we can make the call
    f("ab");
Now, since "ab" is equivalent to the static
structure {'a','b','\0'}, I claim we would
also be able to call the function like
    f({'a','b','\0'});
My compiler (MSVC++4.2, btw) does not agree.
What do you say?
--
Martin Fabian
-----------------------------------------------------------

     tel: +46 (0)31 772 37 16        | Chalmers University of Technology
     fax: +46 (0)31 772 37 30        |       S-412 96 Gothenburg
                                     |              Sweden
Homepage: http://www.control.chalmers.se/~fabian
------------------------------------------------------------------------
Everyone is talking about real-time, but how real is time, really?
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

2. problems with lpt1: and dos

3. Q:'default' before 'case' in 'switch'

4. Shells (was re: backslashes--> was re: shells)

5. invalid conversion from 'const void*' to 'void*' : works fine on Windows but

6. Using WSE Security

7. why no implicit cast from 'int *' to 'const int *'

8. Wanted: DOS Screen Saver

9. Passing 'const' as 'this' discards qualifiers

10. 'operator==' & 'const'

11. TreeView: what's the difference btw 'child' and 'sibling'?

12. File format for '.dll's' and '.lib'

13. 'const_iterator' : is not a member of '`global namespace''