對於我們給了一個代碼,基本上需要數字10000000000,3,-10000000000和5,打印它們,將它們加在一起,然後將它們除以四,以使平均。它們都被標記爲浮點數,當代碼吐出平均值時,它會忽略等式中的3和5。我發現用「double」替換「const float」可以修復它,但是分配的目的是找到一種不改變標識符類型的方法。有沒有人有任何想法?我的代碼忽略小浮動時添加和dividng
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(void)
{
const float YEAR_ONE = 10000000000;
const float YEAR_TWO = 3;
const float YEAR_THREE = -10000000000;
const float YEAR_FOUR = 5;
int numberOfYears = 4;
float average = YEAR_ONE + YEAR_TWO;
average += YEAR_THREE + YEAR_FOUR;
average = average/numberOfYears;
cout << fixed;
cout << setprecision(2);
cout << "1998 earnings were: $" << YEAR_ONE << endl;
//Prints the amount earned in 1998
cout << "1999 earings were: $" << YEAR_TWO << endl;
//Prints the amount earned in 1999
cout << "2000 earnings were: $" << YEAR_THREE << endl;
//Prints the amount earned in 2000
cout << "2001 earnings were: $" << YEAR_FOUR << endl;
//Prints the amount earned in 2001
cout << "Average earnings was: $" << average << endl;
//Prints the average amount earned between 1998 and 2001
return 0;
}
那麼,浮動並沒有很多的精度開始。精度實際上下降了數字越大。所以...沒有什麼驚喜。 – dtech 2014-10-05 22:15:53
考慮浮動和雙打的* range *和* precision *的問題。如果你不熟悉這些條款,那麼(a)你在課堂上沒有注意到,(b)你最喜歡的搜索引擎會用有用的資源壓倒你。 – 2014-10-05 22:16:56
這聽起來像練習旨在教你一些東西。你到目前爲止學到了什麼?你能把它和你在課堂上已經提到過的東西聯繫起來嗎? – 2014-10-05 22:18:35