由于某些原因,使用浮点数乘法是错误的

时间:2017-03-05 07:37:36

标签: c++ math

SOk我删掉了所有无关的信息。我还对一个新文件进行了一些测试,看起来当你将两个数字相加时,它们会相互产生变量,从而产生错误的结果。就像在我的代码中一样,用户首先以英尺为单位输入英尺长度。然后将英寸除以12并加上英尺。还有四舍五入。宽度也一样。然后,当你将长度和宽度相乘时会产生错误的结果。

为什么?我该如何解决?

using namespace std;

void setdata();

int main(){

    setdata();      
    return 0;

}

void setdata(){
    int idnumber, lengthfeet, lengthinches, widthfeet, widthinches;
    float costsqfoot, discount, lengthinchdec, widthinchdec, foot1, reallength, realwidth, arearoom;
    foot1= 12;
    cout << "What is length of room \t Feet: "; cin >> lengthfeet; cout << "\t \t \t Inches: "; cin >> lengthinches; 
    cout << "What is width of room \t Feet: "; cin >> widthfeet; cout << "\t \t \t Inches: "; cin >> widthinches;
    cout << fixed << setprecision(2);
    lengthinchdec = lengthinches / foot1; cout << lengthinchdec << endl; widthinchdec = widthinches / foot1; cout << widthinchdec; 
    reallength = lengthfeet + lengthinchdec; realwidth = widthfeet + widthinchdec; arearoom = (reallength * realwidth);
    cout << endl;
    cout << reallength << endl; cout << realwidth << endl;
    cout << arearoom;
}

例如

长度尺的输入:30

长度为英寸的输入:5

宽度尺的输入:18

输入宽度英寸:11

reallength的输出为30.42。长度被除以12,所以5/12在向上舍入时为.42。

实际宽度输出为18.92。 widthinches被除以12,因此在向上舍入时11/12为0.92。

答案出来575.38。 它应该出来575.54

3 个答案:

答案 0 :(得分:1)

为了检查计算器,你正在整理中间结果。

你的程序不是四舍五入中间结果,只是乘法的结果。

575.38是正确的答案!

答案 1 :(得分:1)

帝国单位地狱:) 手动倍增并得到了 30英尺5英寸* 18英尺11英寸= 82 855英寸2 这是575.38194444444平方英尺 所以...问题是什么?

只是为了有趣的格式化代码并将其推入CoLiRu,所有变量都设置为double而不是float,以防万一......
http://coliru.stacked-crooked.com/a/2fcef984c5561159
得到了相同的结果

答案 2 :(得分:0)

浮点计算可能很棘手。像5/12的结果这样的值不能用具有有限位数的二进制浮点数表示,因此它们被计算并存储在具有一定数量的floatdouble类型中舍入错误。

在某些情况下(例如您的特定示例),可以使用整数算法来避免这些错误(即使在实践中可以忽略不计)。

考虑这段代码以及它如何处理您的问题:

#include <iostream>
#include <iomanip>

int main() {
    using std::cout;
    using std::cin;
    constexpr int inches_in_one_foot = 12;
    constexpr int square_inches_in_one_square_foot =
                                       inches_in_one_foot * inches_in_one_foot;

    // Please, note that in real code user input should be checked
    int length_feet, length_inches, width_feet, width_inches;    
    cout << "What is the length of the room?\nFeet: ";
    cin >> length_feet;     
    cout << "Inches: ";
    cin >> length_inches; 
    cout << "What is the width of the room?\nFeet: ";
    cin >> width_feet;
    cout << "Inches: ";
    cin >> width_inches;

    // calculate the dimensions in inches
    int length    = length_inches + inches_in_one_foot * length_feet;
    int width     = width_inches + inches_in_one_foot * width_feet;
    // the area is precisely calculated in square inches
    int area      = length * width;
    int area_feet = area / square_inches_in_one_square_foot;
    // note that this   ^^^  is an integer division, or:   82855 / 144 = 575
    int area_inches = area - area_feet * square_inches_in_one_square_foot;

    cout << "\nArea: " << area_feet << " square feet and "
         << area_inches << " square inches.\n";

    cout << "or : " << std::fixed << std::setprecision(2)
         << static_cast<float>(area) / square_inches_in_one_square_foot
         << " square feet.\n";

    return 0;
}

输入值30 5 18 11,它会提供以下输出:

Area: 575 square feet and 55 square inches.
or : 575.38 square feet.

请注意,在第二个结果中,小数部分不代表38平方英寸,而是0.38平方英尺,作为值的近似值0.381944。

相关问题