Hello, I have written a C++ code to implement the Box Muller random number generator. However, I keep getting the error stated above. I have attached the code below. Any help will be highly appreciated.
#include %26lt;cstdio%26gt;
#include %26lt;cstdlib%26gt;
#include %26lt;cmath%26gt;
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
double result;
double u;
double v;
double s;
do
{
u = 2.0*rand() - 1.0;
v = 2.0*rand() - 1.0;
s = u*u + v*v;
}
while
(s %26gt;= 1.0);
result = u*sqrt(-2*log(s)/s);
return result;
}
How do I fix C++ Error; 'Warning converting to int from double' ?
NOOOOOOO! Don't change your main program's return type to double! (But also don't "return result;" either.)
Your problem is not an error. It is only a warning. You can ignore it if you wish. However, it points to a real problem:
------------------
return result;
------------------
This statement returns an exit code from the main program that most operating system interpret as an exit status. As is your result will be truncated to an int and that int will be your exit status. If you change your return type to double, the truncation does not happen and the raw double is passed back to the OS as an exit status, which, while not "unsafe", will certainly result in some weird exit statuses.
Instead, I would end the program as follows:
------------------
cout %26lt;%26lt; "Result: " %26lt;%26lt; result %26lt;%26lt; endl;
return 0; //%26lt;------ indicates normal program exit
------------------
=============
Aside to Jonny: IIRC, C++ does not encode information about the return type into the "mangled name" that is sent to the loader. So you could make your return type anything you want and at runtime the system would take whatever stream of bits is returned and interpret it as an int.
In fact, I know this is so, since I have seen production C++ programs with the main program signature of:
------------------
void main()
------------------
Reply:First of all, that's not really an error, but a warning. It's just informing you that the compiler is implicitly converting a double to an int. Stuff like this is good to know about, as conversions from floating point numbers to integer numbers, if used a lot, can slow a program down.
The easiest way to get rid of this warning is to just explicitly cast the double to an int. Instead of simply writing "return result", you should write "return (int)result". That tells the compiler that you actually want to convert the double to an int, so it doesn't need to throw a warning.
You could, as the first person said, make it so main returns a double rather than an int, but since main is the entry point to the program I'm not sure if that's allowed. And whether or not it is allowed, it's the C standard that main returns an int, so I personally wouldn't mess with it, as the results might be unpredictable.
Reply:The return type for main is set to int and you are returning a double value...
int main %26lt;==
double result
return result
try setting your return type of main to double and that should clear up.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment