在转换和舍入中使用C ++的问题 [英] Problems with C++ in casting and rounding

查看:104
本文介绍了在转换和舍入中使用C ++的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入2.09时,它乘以100,输出结果为208.

以下是程序和输出:



  #include   <   iostream  >  

using namespace std;

int main()
{

float userNumber;
int change = 0 ;
int n = 100 ;

cout<< 输入现金:;

cin>> userNumber;

cout<< \ nnuserNumber value:<< userNumber<< \ n;

change = userNumber * n;

cout<< \ n在userNumber * 100之后更改:<<改变<< userNumber:<< userNumber<< \ n \ n;
}



-------------------------以下是输出 - -----------------





输入现金:2.09 

userNumber值:2.09

用户号码后更改* 100:208 userNumber:2.09





我尝试了什么:



我去过几个网站



在转换前添加0.5(如果x> 0)或减去0.5(如果x< 0),因为编译器将始终截断。

解决方案

< blockquote>使用 round [ ^ ]。


此行为的原因是大多数浮点数无法表示完全。



2.09是这样的数字。将字符串转换为单个精度( float )时,存储的值为2.089999914。所以乘法的结果是208.9999914。因为转换为 int 只是截断,结果是208.



即使使用双精度( double )无济于事,因为它也无法存储确切的值2.09。



解决方案是使用 round()按照CPallini的建议或添加0.5:

  float  floatResult = userNumber * n; 
change = static_cast< int>(floatResult< 0 ?floatResult - 0 .5f: floatResult + 0 .5f);< / int>



但请注意,上述内容仅应使用当你知道结果将适合整数(不产生溢出)时。


如果你使用setprecision,它会显示发生了什么。小数已被截断。在math.h中使用ceil()进行四舍五入。



  #include   <   iostream  >  
#include < iomanip >
#include < math.h >
使用 命名空间标准;

int main()
{

float userNumber;
float h,k;
int change = 0 ;
int n = 100 ;

cout<< 输入现金:;

cin>> userNumber;

cout<< \ nnuserNumber value:<< userNumber<< \ n;
h = userNumber * n;

cout<< setprecision( 10 )<< H;

k = ceil(userNumber * n);

change = k;

cout<< \ n在userNumber * 100之后更改:<<改变<< userNumber:<< userNumber<< \ n \ n;
}


When the user inputs 2.09 it is multiplied by 100 the output comes out as 208.
Below is the program and the output:

#include<iostream>

using namespace std;

int main ( )
{

	float userNumber;
	int change = 0;               
	int n = 100;

	cout << "Enter cash: ";

	cin >> userNumber;

	cout << "\nuserNumber value: " << userNumber << "\n"; 

	change = userNumber * n;

	cout << "\nChange after userNumber * 100: " << change  << " userNumber: " <<     userNumber  <<   "\n\n";                                                 
}


-------------------------Below is the output------------------


Enter cash: 2.09

userNumber value: 2.09

Change after userNumber * 100: 208 userNumber: 2.09



What I have tried:

I have gone to several sites

Add 0.5 before casting (if x > 0) or subtract 0.5 (if x < 0), because the compiler will always truncate.

解决方案

Use round[^].


The reason for this behaviour is that most floating point numbers can't be represented exactly.

2.09 is such a number. When converting the string to a single precision (float) the stored value is 2.089999914. So the result of the multiplication is 208.9999914. Because casting to int is just truncating, the result is 208.

Even using double precision (double) would not help because that can't store the exact value 2.09 too.

The solution is to use round() as suggested by CPallini or adding 0.5:

float floatResult = userNumber * n;
change = static_cast<int>(floatResult < 0 ? floatResult - 0.5f : floatResult + 0.5f);</int>


But note that the above should only be used when you know that the result will fit in an integer (does not generate an overflow).


if you use setprecision, it shows what has happened. The decimal has been truncated. Use ceil() in math.h to round up.

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main ( )
{

float userNumber;
float h,k;
int change = 0;
int n = 100;

cout << "Enter cash: ";

cin >> userNumber;

cout << "\nuserNumber value: " << userNumber << "\n";
h=userNumber*n;

cout << setprecision(10) << h;

k=ceil(userNumber*n);

change = k;

cout << "\nChange after userNumber * 100: " << change << " userNumber: " << userNumber << "\n\n";
}


这篇关于在转换和舍入中使用C ++的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆