I have a floating point overflow problem

Well, I'm a newbie, this is my year as a senior researcher. I am trying to do an exercise from my tutorial in which I use a structure called MovieData that has a constructor that allows me to initialize member variables when a MovieData struct is created. This is what my code looks like:

 #include <iostream> #include <iomanip> #include <string> using namespace std; // struct called MovieData struct MovieData { string title; string director; unsigned year; unsigned running_time; double production_cost; double first_year_revenue; MovieData() // default constructor { title = "Title"; director = "Director"; year = 2009; running_time = 90; production_cost = 1000000.00; first_year_revenue = 1000000.00; } // Constructor with arguments: MovieData(string t, string d, unsigned y, unsigned r, double p, double f) { title = t; director = d; year = y; running_time = r; } }; // function prototype: void displayMovieData(MovieData); // main: int main() { // declare variables: MovieData movie, terminator("Terminator", "James Cameron", 1984, 120, 5000000, 2000000); // calling displayMovieData function for movie and terminator // so it will display information about the movie: displayMovieData(movie); displayMovieData(terminator); return 0; } // displayMovieData function: // It receives struct MovieData variable as // an argument and displays that argument's // movie information to the user. void displayMovieData(MovieData m) { cout << m.title << endl; cout << m.director << endl; cout << m.year << endl; cout << m.running_time << endl; cout << fixed << showpoint << setprecision(2); cout << m.production_cost << endl; cout << m.first_year_revenue << endl << endl; } 

here is the result i got:

  Title
 Director
 2009
 90
 1,000,000.00
 1,000,000.00

 Terminator
 James cameron
 1984
 120
 -92559631349317830000000000000000000000000000000000000000000000.00.00
 -92559631349317830000000000000000000000000000000000000000000000.00.00

 Press any key to continue.  .  .

Compiled in Microsoft Visual C ++ 2008 Express Edition.

My question is: is this due to a double data type overflow? I even tried it with a long double, and the same thing happens. although I used 5mil as production_cost and 2mil as first_year_revenue both the output numbers are the same. Using my default constructor correctly prints 1,000,000. Am I using the correct data type in this case? I want it to be double, because it is a monetary number, dollars and a cent.

Thanks for any help that comes to my mind. Sorry for my long question. This is my first post on SO, so any feedback on the correct format of posting questions will be great, thanks!

+4
source share
6 answers

Thanks for posting the full code, now the problem is obvious. The problem is this:

 MovieData(string t, string d, unsigned y, unsigned r, double p, double f) { title = t; director = d; year = y; running_time = r; } 

You have omitted the following statements:

  production_cost = p; first_year_revenue = f; 

Without these statements, production_cost and first_year_revenue not initialized when using the above constructor.

This exercise emphasizes the need to publish the exact code that you use when posting questions about stack overflows. The first version of the code you posted is different and does not contain this error.

+8
source

Correction of errors that did not allow compiling the code (missing semicolons, upper case M instead of lowercase m) I get the following:

 #include <iostream> #include <iomanip> using namespace std; struct MovieData { string title; string director; unsigned year; unsigned running_time; double production_cost; double first_year_revenue; MovieData() // My default constructor { title = "Title"; director = "Director"; year = 2009; running_time = 90; production_cost = 1000000.00; // this one comes out ok. first_year_revenue = 1000000.00; // this one comes out ok as well. } // This is my constructor with arguments: MovieData(string t, string d, unsigned y, unsigned r, double p, double f) { title = t; director = d; year = y; running_time = r; production_cost = p; first_year_revenue = f; } }; void displayMovieData(MovieData m) { cout << m.title << endl; cout << m.director << endl; cout << m.year << endl; cout << m.running_time; cout << fixed << showpoint << setprecision(2); cout << m.production_cost << endl; cout << m.first_year_revenue << endl << endl; } int main() { MovieData terminator( "Terminator", "James Cameron", 1984, 120, 5000000, 2000000); displayMovieData(terminator); return 0; } 

When compiling and starting it does not reproduce your problem ...:

 $ g++ -Wall --pedantic z.cc $ ./a.out Terminator James Cameron 1984 1205000000.00 2000000.00 $ 

Copy and paste the code exactly as I gave it here, and let us know what happens (and with which compiler, platform, etc.) I use gcc 4.0.1 on MacOSX 10.5).

+3
source

No, you will not overwhelm the double range with the revenue or production costs of any movie.

I assume the problem is with your displayMovieData function. Can you post the code for this?
IIRC, you can get weird values ​​printed this way if you call something like printf and it gets confused between singles and doubles. Or if you pass it %d instead of %f ...

+2
source

Like the other answers here, I'm not sure what the problem might be, as the code looks fine. However, try replacing this ad:

 void displayMovieData(MovieData m) 

from

 void displayMovieData(const MovieData &m) 

and see if your situation improves. See a recent question. Why is it preferable to write func (const Class & value)? for more information on the difference between the two ads.

I must emphasize that this should not change the behavior of your program, but if something about your compiler and / or runtime has an error, the above code change can help isolate the problem.

+1
source

Your output does not match the code you display - you omitted '<<endl' after the time your output refers to. This makes our debugging more difficult.

Here is your code working correctly on MacOS X 10.5.8 (Leopard) with g ++ 4.0.1.

 #include <string> using namespace std; struct MovieData { string title; string director; unsigned year; unsigned running_time; double production_cost; double first_year_revenue; MovieData() // My default constructor { title = "Title"; director = "Director"; year = 2009; running_time = 90; production_cost = 1000000.00; // this one comes out ok. first_year_revenue = 1000000.00; // this one comes out ok as well. } // This is my constructor with arguments: MovieData(string t, string d, unsigned y, unsigned r, double p, double f) { title = t; director = d; year = y; running_time = r; production_cost = p; first_year_revenue = f; } }; #include <iostream> #include <iomanip> using namespace std; void displayMovieData(MovieData m) { cout << m.title << endl; cout << m.director << endl; cout << m.year << endl; cout << m.running_time << endl; cout << fixed << showpoint << setprecision(2); cout << m.production_cost << endl; cout << m.first_year_revenue << endl << endl; } int main() { MovieData def; MovieData terminator("Terminator", "James Cameron", 1984, 120, 5000000, 2000000); MovieData terminator2("Terminator 2", "James Cameron", 1984, 120, 5000000.0, 2000000.0); displayMovieData(def); displayMovieData(terminator); displayMovieData(terminator2); } 

The output I get is:

 Title Director 2009 90 1000000.00 1000000.00 Terminator James Cameron 1984 120 5000000.00 2000000.00 Terminator 2 James Cameron 1984 120 5000000.00 2000000.00 

My best thesis (unsupported by the data above) is that somehow you didn’t get the conversion from "int" to "double" that occurs in constructor calls, but I admit, I don’t understand how this could happen.

+1
source

I would try to add .0 to the number and make them double. This can be confusing when trying to convert from an integer to a double.

This will reflect what you have in the default working constructor.

0
source

All Articles