warning C4305: '=' : truncation from 'double' to 'float'
Hi guys ,
In Visual Studio 2005 I compiled the following code
int main()
{
float a;
a=0.1;
if(a<0.1)
{
printf("C\n");
}
else
{
printf("C++\n");
}
}
I was very surprised when I saw the output C++ .
I got the warning C4305 :" '=' : truncation from 'double' to 'float' " , I searched the web but couldn't find some answers .
I don't understand what's wrong with assigning the value 0.1 to a float variable .
Thank you
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
Skill Endorsements: 0
>I don't understand what's wrong with assigning the value 0.1 to a float variable .
There's nothing wrong with it, the compiler is warning you that you are trying to assign a double to a float.
A literal 0.1 is a double in your compiler.
Do a test. Add these printf(s) to your snippet.
printf( "sizeof of 0.1 = %d bytes\n", sizeof( 0.1 ) );
printf( "sizeof of float = %d bytes\n", sizeof( float ) );
The result will be clear to you.
[Edit]: Some extra.
In fact even
if( a <= 0.1 )
will evaluate to FALSE because float != double. However
if( a <= ( float ) 0.1 )
will evaluate to TRUE
Aia
Nearly a Posting Maven
2,394 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 220
Skill Endorsements: 14
This helped a lot , thanks Aia . You're great .
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 5 Years Ago by
Aia