Skip to main content
GameDev.net gamedev.net
🔒 Locked

VERY simple c++ question

Started by GamesProxy Jan 16, 2007 at 7:38 AM 5 replies 950+ views
Original Post
GamesProxy
GamesProxy
Hey, I have the following code in just a learning c++ program. float b; b = 6.12; This gives me the error: (21) : warning C4305: '=' : truncation from 'double' to 'float' Why? I dont get an error if i make b a double instead of a float.
_goat
_goat
Use "6.12f" for a float.

The difference is, a float is 32 bits of accuracy, and a double is 64. Hence you lose information going from double to float. It's warning you, in case you made a mistake.
Motorherp
Motorherp
When assigning a float you must put an 'f' after the number else the compiler will assume its a double. Therefore your code would look like:

float b;
b = 6.12f;

Hehe .... everyone jumped on that one ;p
[size="1"] [size="4"]:: SHMUP-DEV ::
mikeman
mikeman
Actually, this is not an error, just a warning. The literal "6.12" is considered a double, so the compiler tells you that if you store it in the float "b", you will lose precision. To construct a float literal, just put an "f" in the end, like "6.12f"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.