Note that there are some explanatory texts on larger screens.

plurals
  1. POI have a floating-point overflow problem
    text
    copied!<p>Well, I'm a beginner, it's my year as a computer science major. I'm trying to do an exercise from my textbook that has me use a struct called <code>MovieData</code> that has a constructor that allows me to initialize the member variables when the <code>MovieData</code> struct is created. Here's what my code looks like:</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;string&gt; 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 &lt;&lt; m.title &lt;&lt; endl; cout &lt;&lt; m.director &lt;&lt; endl; cout &lt;&lt; m.year &lt;&lt; endl; cout &lt;&lt; m.running_time &lt;&lt; endl; cout &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(2); cout &lt;&lt; m.production_cost &lt;&lt; endl; cout &lt;&lt; m.first_year_revenue &lt;&lt; endl &lt;&lt; endl; } </code></pre> <p>here is the output I received:</p> <pre> Title Director 2009 90 1000000.00 1000000.00 Terminator James Cameron 1984 120 -92559631349317830000000000000000000000000000000000000000000000.00 -92559631349317830000000000000000000000000000000000000000000000.00 Press any key to continue . . . </pre> <p>Compiled on Microsoft Visual C++ 2008 Express Edition.</p> <p>My question is, is this happening due to an overflow of the double data-type? I even tried it using long double and the same thing occurs. even though i used 5mil as <code>production_cost</code> and 2mil as <code>first_year_revenue</code> both numbers output are the same. Using my default constructor properly prints out 1000000. Am I using the correct data-type in this case? I want it to be double because it's a monetary number, dollars and cents. </p> <p>Thanks for whatever help comes my way. Sorry about my long question. It's my first post on SO, so any feedback on correct format of posting questions will be great, Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload