[英]C++ floating point accuracy in while loops
我試圖通過使用一系列 while 循環來計算總計中美元和硬幣面額的數量。 然而,當我深入到硬幣上時,我差了一分錢。 當我輸入 99.95 時,我會得到 3 個四分之三、1 角錢、1 鎳和 4 便士的輸出。 我已將問題縮小到浮點精度問題。 但是,我研究過的所有解決方案都不適用於我的情況。 任何指針?
#include <iostream>
using namespace std;
int main()
{
float amount;
cout<<"enter amount" << endl;
cin>>amount;
int pennies=0, nickels=0, dimes=0, quarters=0, ones=0, fives=0,
tens=0,
twenties=0, fifties=0, hundreds=0;
while (amount >= 100)
{
hundreds = hundreds +1;
amount = amount - 100;
}
while (amount >= 50)
{
fifties = fifties +1;
amount = amount - 50;
}
while (amount >= 20)
{
twenties = twenties +1;
amount = amount - 20;
}
while (amount >= 10)
{
tens = tens +1;
amount = amount - 10;
}
while (amount >= 5)
{
fives = fives +1;
amount = amount - 5;
}
while (amount >= 1)
{
ones = ones +1;
amount = amount - 1;
}
while (amount >= .25)
{
quarters = quarters +1;
amount = amount - .25;
}
while (amount >= .10)
{
dimes = dimes +1;
amount = amount - .10;
}
while (amount >= .05)
{
nickels = nickels +1;
amount = amount - .05;
}
while (amount >= .01)
{
pennies = pennies +1;
amount = amount - .01;
}
cout<<endl<<"pennies:"<< pennies;
cout<<endl<<"nickels:"<<nickels;
cout<<endl<<"dimes:"<<dimes;
cout<<endl<<"quarters:"<<quarters;
cout<<endl<<"ones:"<<ones;
cout<<endl<<"fives:"<<fives;
cout<<endl<<"tens:"<<tens;
cout<<endl<<"twenties:"<<twenties;
cout<<endl<<"fifties:"<<fifties;
cout<<endl<<"hundreds:"<<hundreds<<endl;
return 0;
}
在需要精確值的情況下不要使用浮點數。 99.95 不能用浮點數或雙精度數精確表示,有點像 1/3 不能用有限數量的正常十進制數字精確表示。
正如 Doug T. 所建議的,您可以使用一個整數來表示便士的數量。 當用戶鍵入 123.45 時,將其讀為兩個整數,然后將其存儲為 12345 便士,而不是 123.45 美元。
在這種情況下,您還可以嘗試將上次while (amount >= .01)
更改為while (amount >= .005)
。 這不是一個普遍推薦的解決方案,如果這是一個真實的銀行應用程序,你真的應該避免它,但它至少有助於防止一些錯誤。
在這種情況下,您應該使用定點值,而不是浮點值。
盡管人們以美元來看待金錢,這並不准確,但金錢以美分來衡量,這是准確的。 只需計算美分的數量,除以 100 即可獲得美元金額,並取模數即可獲得美分金額。 在這種情況下,浮點不是合適的工具。
二進制浮點數通常不能表示小數部分,即使小數總數很小。 例如,無法准確表示0.1
。
為了處理精確值,存在不同的方法,這些方法都相當於使用與2
不同的基數。 根據 tge 的特定需求,這些方法或多或少涉及。 對於您的情況,最簡單的方法是使用固定精度而不是可變精度。 要以固定精度進行計算,您只需使用一個整數來表示您實際想要的值乘以合適的10
冪。 根據您執行的計算量,您可以將邏輯打包到一個類中或將其分發到整個代碼中。 在“真正的”應用程序中,我會將它打包到一個類中,在作業(家庭作業、面試等)中,我可能只是將邏輯直接應用於int
。
這是固定代碼,我使用了 TJD 的建議,而不是使用 .01,而是使用了 .0099。 對於我的問題似乎有效,謝謝大家!
#include <iostream>
using namespace std;
int main()
{
float amount;
cout<<"enter amount" << endl;
cin>>amount;
int pennies=0, nickels=0, dimes=0, quarters=0, ones=0, fives=0,
tens=0,
twenties=0, fifties=0, hundreds=0;
float p = .0099, n = .0499, d = .099, q = .2499;
while (amount >= 100)
{
hundreds = hundreds +1;
amount = amount - 100;
}
while (amount >= 50)
{
fifties = fifties +1;
amount = amount - 50;
}
while (amount >= 20)
{
twenties = twenties +1;
amount = amount - 20;
}
while (amount >= 10)
{
tens = tens +1;
amount = amount - 10;
}
while (amount >= 5)
{
fives = fives +1;
amount = amount - 5;
}
while (amount >= 1)
{
ones = ones +1;
amount = amount - 1;
}
while (amount >= q)
{
quarters = quarters +1;
amount = amount - q;
}
while (amount >= d)
{
dimes = dimes +1;
amount = amount - d;
}
while (amount >= n)
{
nickels = nickels +1;
amount = amount - n;
}
while (amount >= p)
{
pennies = pennies +1;
amount = amount - p;
}
cout<<endl<<"pennies:"<< pennies;
cout<<endl<<"nickels:"<<nickels;
cout<<endl<<"dimes:"<<dimes;
cout<<endl<<"quarters:"<<quarters;
cout<<endl<<"ones:"<<ones;
cout<<endl<<"fives:"<<fives;
cout<<endl<<"tens:"<<tens;
cout<<endl<<"twenties:"<<twenties;
cout<<endl<<"fifties:"<<fifties;
cout<<endl<<"hundreds:"<<hundreds<<endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.