[英]C++ Float Precision is incorrect
我是編程新手,正在嘗試一些非常基本的程序。 我剛剛編寫了一個將USD轉換為GBP的程序。 當我運行程序時,我沒有得到確切的GBP值。 例如,如果我輸入5 USD,程序將返回3.25 GBP。 但是,正確的值應為3.23。 這是代碼。 可以/會告訴我我做錯了嗎? 請。
#include <iostream>
#include <cmath>
using namespace std;
float dtp(float);
int main()
{
float dollar;
cout <<"Enter the dollar amount you want converted to Great Britain
Pounds: ";
cin >> dollar;
float pound = dtp(dollar);
if (pound <= 1)
{
cout <<"The dollar amount you entered of " << dollar <<" dollar is
equal to " << pound <<" pound.";
}
else
{
cout <<"The dollar amount you entered of " << dollar <<" dollars is
equal to " << pound <<" pounds.";
}
return 0;
}
float dtp(float p)
{
return p * .65;
}
根據我的計算器,5 * .65 = 3.25,這就是您得到的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.