c++正在舍入我的函数的结果



我创建了一个程序,其中包含将数字从字符串转换为数值变量的函数(下面将复制该程序)。我开始使用双精度;然而,由于某种原因,每当我构建它时,如果我使用小数,它总是将最后一个数字向上或向下舍入。我不明白为什么,所以如果你们能分享我的见解,我会很感激的。

#include <iostream>
using namespace std;
//variables initialised in a set of curly brackets are called local variables
//variables initialised outside a set of curly brackets are called global variables
// local variables can only be used in that set of curly brackets
// global variables can be used anywhere after it has been declared
// global variables are automatically set to 0 when it has not been assigned a value
//a void function is a function that returns nothing
//StringName.length() returns the length of a string

/*----------------------------------------FUNCTION-------------------------------------*/
int pow(int a, int b){
int c = 1;
for(int i=0;i<b;i++){
c*=a;
}
return c;
}
float StringNoToNo(string a){
float b=0.0;
int y = 1;
int s = 0;
//cout<<"a.length() is: "<<a.length()<<endl;
for(int i = (a.length()); i>0;i--){
//  cout<<"the loop: "<<(a.length()-i)<<endl;
int z = a[i-1];
//cout<<"z = "<<z<<endl;
switch(z){
case 48 ... 57:
//    cout<<"(a[i]>=48)&&(a[i]<=57) is true"<<endl;
//cout<<"pow(10.0,(a.length()-i)) = "<<pow(10.0,(a.length()-i))<<endl;
//cout<<"a.length() - i = "<<(a.length()- i)<<endl;
b += ((a[i-1]-48)* pow(10.0,(a.length()-i-s)));
//  cout<<"b= "<<b<<endl;
break;
case 46:
y=pow(10,(a.length()-i));
//    cout<<"y = "<<y<<endl;
if(s==0){
s++;}else{
goto v;
}
break;
default:
v:
//  cout<<"(a[i]<48)||(a[i]>57) is true"<<endl;
cout<< "the number was not written properly"<<endl;
return 0;
break;
}
}
//cout<<" b = "<<b<<endl;
//cout<<"b/y = "<<b/y<<endl;
return (b/y);
}
/*----------------------------------------FUNCTION-------------------------------------*/
// overloading functions - you can create multiple functions with the same name so as long
// as they have different parameters.
// as long as the function is declared at the beginning, even if the function meant to
// overload it is written at the end of the code, it can still be used.
main() {
string no;
cout << "write a number: ";
cin >> no;
cout << "the number is: "<<StringNoToNo(no)<<endl;
}

首先,我为函数使用双变量,然后将其更改为float。在那之后,我在循环的各个部分使用了cout,以查看每一步都发生了什么,但仍然无法理解问题所在。

我得到的输入和输出是:

write a number: 1234.6789
the number is: 1234.68

进程返回0 (0x0)执行时间:4.713 s按任意键继续。

当然,如果我从cout函数中删除//:

write a number: 1234.6789
the number is: a.length() is: 9
the loop: 0
z = 57
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 1
a.length() - i = 0
b= 9
the loop: 1
z = 56
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 10
a.length() - i = 1
b= 89
the loop: 2
z = 55
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 100
a.length() - i = 2
b= 789
the loop: 3
z = 54
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 1000
a.length() - i = 3
b= 6789
the loop: 4
z = 46
the loop: 5
z = 52
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 100000
a.length() - i = 5
b= 46789
the loop: 6
z = 51
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 1000000
a.length() - i = 6
b= 346789
the loop: 7
z = 50
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 10000000
a.length() - i = 7
b= 2.34679e+06
the loop: 8
z = 49
(a[i]>=48)&&(a[i]<=57) is true
pow(10.0,(a.length()-i)) = 100000000
a.length() - i = 8
b= 1.23468e+07
1234.68
Process returned 0 (0x0)   execution time : 24.055 s
Press any key to continue.

b= 1.23468e+07证明你的函数不是四舍五入,它返回正确的值。

实际上是cout <<在进行舍入。使用cout << precision(8)使其显示8位精度,而不是默认的6位。

最新更新