Click to See Complete Forum and Search --> : Warning in circle circumference problem


Dave Christman
03-12-2000, 09:15 PM
The following warning message is produced when I compile the program after
the warning message:

C:\Cmis 140\experiments\circumf\circumf.cpp(4) : warning C4305: 'initializing'
: truncation from 'const double' to 'const float'

The program is as follows:

#include <iostream>
using namespace std;

const float PI = 3.14159;
float circmf(float);

int main()
{
float circumference;
circumference = 0;
circmf(circumference);
cout<<"The circumference of a circle is: "<<circmf(circumference)<<endl;
return 0;
}

float circmf(float circumference)
{
float radius = 20;
return PI*(2*radius);
}

Can anyone tell me why this warning is being generated? Any help would be
appreciated. Thanks.

Dave Christman

Danny Kalev
03-12-2000, 11:09 PM
Dave,
You should replace all your float variables with double. The problem is
that a hard coded floating point number is treated as double, and you
try to store it in a float. The compiler warns you that you might lose
precision in this case.

Danny Kalev
http://www.amazon.com/exec/obidos/ASIN/0789720221
Please reply to the newsgroup.

Dave Christman wrote:
>
> The following warning message is produced when I compile the program after
> the warning message:
>
> C:\Cmis 140\experiments\circumf\circumf.cpp(4) : warning C4305: 'initializing'
> : truncation from 'const double' to 'const float'
>
> The program is as follows:
>
> #include <iostream>
> using namespace std;
>
> const float PI = 3.14159;
> float circmf(float);
>
> int main()
> {
> float circumference;
> circumference = 0;
> circmf(circumference);
> cout<<"The circumference of a circle is: "<<circmf(circumference)<<endl;
> return 0;
> }
>
> float circmf(float circumference)
> {
> float radius = 20;
> return PI*(2*radius);
> }
>
> Can anyone tell me why this warning is being generated? Any help would be
> appreciated. Thanks.
>
> Dave Christman

David Christman
03-17-2000, 10:14 PM
Danny Kalev <dannykk@inter.net.il> wrote:
>Dave,
>You should replace all your float variables with double. The problem is
>that a hard coded floating point number is treated as double, and you
>try to store it in a float. The compiler warns you that you might lose
>precision in this case.
>
>Danny Kalev
>http://www.amazon.com/exec/obidos/ASIN/0789720221
>Please reply to the newsgroup.
>
>Dave Christman wrote:
>> Danny;
Thanks for your help. Unfortunately the problem calls for the function receiving
a floating point number and returning the fractional part of that number.
Any help is appreciated on this matter. Thanks.

Dave Christman
>> The following warning message is produced when I compile the program after
>> the warning message:
>>
>> C:\Cmis 140\experiments\circumf\circumf.cpp(4) : warning C4305: 'initializing'
>> : truncation from 'const double' to 'const float'
>>
>> The program is as follows:
>>
>> #include <iostream>
>> using namespace std;
>>
>> const float PI = 3.14159;
>> float circmf(float);
>>
>> int main()
>> {
>> float circumference;
>> circumference = 0;
>> circmf(circumference);
>> cout<<"The circumference of a circle is: "<<circmf(circumference)<<endl;
>> return 0;
>> }
>>
>> float circmf(float circumference)
>> {
>> float radius = 20;
>> return PI*(2*radius);
>> }
>>
>> Can anyone tell me why this warning is being generated? Any help would
be
>> appreciated. Thanks.
>>
>> Dave Christman

Bill Heffner
03-18-2000, 12:22 AM
>>Dave Christman wrote:
>>> Danny;
>Thanks for your help. Unfortunately the problem calls for the function receiving
>a floating point number and returning the fractional part of that number.
>
Well, a double is a floating point number, and you can return the fractional
part with fmod(double-number, 1.0000);