English 中文(简体)
我有一个浮点超支问题。
原标题:I have a floating-point overflow problem
  • 时间:2009-11-02 02:19:54
  •  标签:

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 MovieData that has a constructor that allows me to initialize the member variables when the MovieData struct is created. Here s 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;
}

我收到的产出如下:

Title
Director
2009
90
1000000.00
1000000.00

Terminator
James Cameron
1984
120
-92559631349317830000000000000000000000000000000000000000000000.00
-92559631349317830000000000000000000000000000000000000000000000.00

Press any key to continue . . .

载于MicrosoftView C++ 2008快版。

我的问题是,这是否由于双重数据类型的过度流入而发生? 我甚至用了很长的两倍,也发生了同样的事。 即便使用5mil作为<代码>生产——成本和2mil作为<代码>第一年_年_revenue两种数字产出相同。 利用我的违约建筑商适当印制100 000份。 我在这种情况下使用正确的数据类型? 我想,这是双重的,因为它有货币数目、美元和百分数。

我感谢一切帮助。 回答我的长期问题。 这是我第一个关于SO的职位,因此,任何关于正确张贴问题的反馈都将是巨大的, 感谢!

最佳回答

由于你提出了完整的法典,这个问题现在很明显。 问题如下:

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;

如无这些说明,则在使用上述构件时,不首先采用<代码>生产_cost和

这项工作突出表明,必须张贴<>exact。 在Stack Overflow上张贴问题时,你重新使用。 你所贴出的法典第一版是不同的,并不包含这种ug。

问题回答

mistake清阻止法典编纂的错误(将半殖民地、上门M而不是下小米),我举出如下:

#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;
}

......

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

$ 

请复制并贴出这部守则,因为我在这里做了介绍,让我们知道发生什么(以及编辑、平台等——我用4.0.1的格丽斯X10.5。

无,你们的头巾会超过任何电影的收入或生产成本的两倍。

My guess would be that the problem lies in your displayMovieData function. Can you post the code to that?
IIRC you can get weird values printed like that if you call something like printf and it gets confused between singles and doubles. Or if you pass it %d instead of %f...

你的产出与你所展示的法典不相符合,你略去了“斜线”;在您的产出要求包括的操作时间之后,末。 这总是使我们更加难以想象。

在这里,你关于MacOS X 10.5.8(Leopard)的守则是正确的,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);
}

我获得的产出是:

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

我的最好论点(得到上述数据的支持)是,你在给建筑商的电话中,没有把电线从直线转换为两倍,但我不理解会如何做到这一点。

同这里的其他答案一样,我不肯定问题可能是什么,因为《守则》似乎有点。 然而,试图取代这一声明:

void displayMovieData(MovieData m)

iii

void displayMovieData(const MovieData &m)

并且看你的情况是否有所改善。 See the recent question 为什么要撰写有关这两个声明之间的差别的更多信息,最好写一下func(组合式班子;价值)?

我应强调,这应该而不是<>,改变你方案的行为,但如果涉及你的汇编者和(或)经营时间环境的因素有点模糊,上述法律变化可能会有助于孤立这一问题。

我将努力增加0,使其增加一倍。 在试图将编译工作从编审中翻一番时,可能会混淆不清。

这将反映你在正在工作的违约干扰者中拥有的内容。





相关问题