[英]C++ Float Precision is incorrect
I'm new to programming and I'm experimenting with very basic programs. 我是编程新手,正在尝试一些非常基本的程序。 I just wrote a program to convert USD to GBP.
我刚刚编写了一个将USD转换为GBP的程序。 When I run the program I don't get the exact GBP value.
当我运行程序时,我没有得到确切的GBP值。 For example if I enter 5 USD the program returns 3.25 GBP.
例如,如果我输入5 USD,程序将返回3.25 GBP。 However, the correct value should be 3.23.
但是,正确的值应为3.23。 Here is the code.
这是代码。 Can/will someone tell me what I'm doing wrong?
可以/会告诉我我做错了吗? Please.
请。
#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.