繁体   English   中英

C ++浮点精度不正确

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM