Share via


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

Question

Wednesday, February 13, 2013 5:21 PM

//so this is my program

// ******************************************************************

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

const float HEIGHT = 30.0; // Height of the cone
const float DIAMETER = 8.0; // Diameter of the cone
const float RED_PRICE = 0.10; // Price per square foot of red
const float BLUE_PRICE = 0.150; // Price per square foot of blue
const float GREEN_PRICE = 0.180; // Price per square foot of green
const float PI = 3.14159265; // Ratio of circumference to diameter
const float INCHES_PER_FT = 12.0; // Inches per foot

int main()

{
float heightInFt; // Height of the cone in feet
float diamInFt; // Diameter of the cone in feet
float radius; // Radius of the cone in feet
float surfaceArea; // Surface area of the cone in feet
float redCost; // Cost of red cone
float blueCost; // Cost of blue cone
float greenCost; // Cost of green cone

heightInFt = HEIGHT/INCHES_PER_FT;
diamInFt = DIAMETER/INCHES_PER_FT;
radius = diamInFt/2;
surfaceArea= PI*radius* sqrt((radius*radius)+(heightInFt*heightInFt) );
redCost = surfaceArea* RED_PRICE;
blueCost = surfaceArea * BLUE_PRICE;
greenCost = surfaceArea * GREEN_PRICE;

cout << "The Surface Area is " << surfaceArea << " Sq.Ft. " << endl;

cout << "The painting cost for " << endl;
cout << setw(3) << "Red Is " << redCost << endl;
cout << setw(3) << "Blue is " << blueCost << endl;
cout << setw(3) << "Green Is " << greenCost << endl;

return 0;

}

ERROR

Warning 1 warning C4305: 'initializing' : truncation from 'double' to 'const float' c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp 12 1 program 1 ERNEST OSHOKOYA 1
Warning 2 warning C4305: 'initializing' : truncation from 'double' to 'const float' c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp 13 1 program 1 ERNEST OSHOKOYA 1
Warning 3 warning C4305: 'initializing' : truncation from 'double' to 'const float' c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp 14 1 program 1 ERNEST OSHOKOYA 1
Warning 4 warning C4305: 'initializing' : truncation from 'double' to 'const float' c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp 15 1 program 1 ERNEST OSHOKOYA 1

'program 1 USER1.exe' (Win32): Loaded 'C:\Users\USER\Documents\Visual Studio 2012\Projects\program 1 USER 1\Debug\program 1 USER.exe'. Symbols loaded.
'program 1 USER.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'program 1 USER.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'program 1 USER.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'program 1 USER .exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp110d.dll'. Symbols loaded.
'program 1 USER.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110d.dll'. Symbols loaded.
The program '[4212] program USER.exe' has exited with code 0 (0x0).

1> Build started: Project: program 1 ERNEST OSHOKOYA 1, Configuration: Debug Win32
1>  ERNEST OSHOKOYA.cpp
1>c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp(12): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp(13): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp(14): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>c:\users\ernesto\documents\visual studio 2012\projects\program 1 ernest oshokoya 1\program 1 ernest oshokoya 1\ernest oshokoya.cpp(15): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>  program 1 ERNEST OSHOKOYA 1.vcxproj -> C:\Users\ERNESTO\Documents\Visual Studio 2012\Projects\program 1 ERNEST OSHOKOYA 1\Debug\program 1 ERNEST OSHOKOYA 1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

All replies (4)

Wednesday, February 13, 2013 5:40 PM ✅Answered | 2 votes

The problem is you're using a double literal to initialize a constant float.  You can force it to use float literals to avoid this warning:

const float HEIGHT = 30.0f; // Height of the cone
const float DIAMETER = 8.0f; // Diameter of the cone
const float RED_PRICE = 0.10f; // Price per square foot of red
const float BLUE_PRICE = 0.150f; // Price per square foot of blue
const float GREEN_PRICE = 0.180f; // Price per square foot of green
const float PI = 3.14159265f; // Ratio of circumference to diameter
const float INCHES_PER_FT = 12.0f; // Inches per foot

Note the "f" at the end of the value.  This specifies that the value is a float, not a double.

Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


Wednesday, February 13, 2013 7:44 PM ✅Answered | 1 vote

You need to use setprecision:

cout << setprecision(3) << "Red Is " << redCost << endl;

Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


Wednesday, February 13, 2013 7:08 PM

The problem is you're using a double literal to initialize a constant float.  You can force it to use float literals to avoid this warning:

const float HEIGHT = 30.0f; // Height of the cone
const float DIAMETER = 8.0f; // Diameter of the cone
const float RED_PRICE = 0.10f; // Price per square foot of red
const float BLUE_PRICE = 0.150f; // Price per square foot of blue
const float GREEN_PRICE = 0.180f; // Price per square foot of green
const float PI = 3.14159265f; // Ratio of circumference to diameter
const float INCHES_PER_FT = 12.0f; // Inches per foot

Note the "f" at the end of the value.  This specifies that the value is a float, not a double.

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

thanks for your help with that.... it resolved the error.

one more problem after debugging, I got this error. I don't know if this is a problem.

the debugger flashes on my screen & exits immediately.

Also, i don't know how to make this come out to 3 decimal places

cout << "The painting cost for " << endl;
cout << setw(3) << "Red Is " << redCost << endl;
cout << setw(3) << "Blue is " << blueCost << endl;
cout << setw(3) << "Green Is " << greenCost << endl;

'program 1 USER 1.exe' (Win32): Loaded 'C:\Users\USER\Documents\Visual Studio 2012\Projects\program 1 USER 1\Debug\program 1 USER 1.exe'. Symbols loaded.
'program 1 USER 1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'program 1 USER 1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'program 1 USER 1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'program 1 USER 1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp110d.dll'. Symbols loaded.
'program 1 USER 1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110d.dll'. Symbols loaded.
The program '[11440] program 1 USER 1.exe' has exited with code 0 (0x0).

 


Wednesday, February 13, 2013 7:52 PM

I figured it out already lol! the program is running good with no errors now thanks to you.