Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

declaring a constant

Status
Not open for further replies.

dexy99

Programmer
Jun 2, 2003
1
GB
I'm new to programming so please forgive me is this is a daft question!
On working my way thru a book on C++ i tried an exercise as follows:

"declare a constant for pi as 3.14159 then declare a float variable and initialize it using your pi constant."

this was my solution:

const float pi = 3.14159;
float testPi = pi;

I included a print statement to test and compiled (i'm using MS Visual C++ 6) the compiler gave me no errors but the following warning:

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

Can anybody out there tell me what this means?




 
hi dexy99,
a floating point constant like "3.1415" is assumed to be of double precision in C++ by default. Your compiler just warns you that you initialize a single-precision constant using a double-precision value - if you use many digits after the point they may actually be lost in the convertion (not in your example, however).

You can avoid this warning in two ways: 1) use double instead of float for all your variables (if you really need the precision), or 2) append an "f" to the value:
Code:
const float pi = 3.14159f;
This way the value is already of type float.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top