C ++函数返回错误的数据类型 [英] C++ function returning wrong data type
问题描述
在下面的代码中,round()似乎返回了错误的数据类型值。从我在第50行(else语句)中删除所有长整型转换时,编译器抛出的错误可以明显看出这一点。
可能是什么原因?
The round() seems to return a wrong data type value in the following code. This is apparent from the error thrown by the compiler when I remove all long long casts in line 50 (The else statement). What could be the reason?
编译期间遇到的错误:-
The error encountered during compilation:-
|| ===构建文件:无项目中的无目标(编译器:未知)=== |
C:\Users\MUNNU\Desktop\ KBIGNUMB.cpp ||在函数'int main()':|
C:\Users\MUNNU\Desktop\KBIGNUMB.cpp | 50 |错误:类型为'double'的无效操作数并将'double'转换为二进制'operator%'|
|| ===构建失败:1个错误,0个警告(0分钟,0秒) === |
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\MUNNU\Desktop\KBIGNUMB.cpp||In function 'int main()':|
C:\Users\MUNNU\Desktop\KBIGNUMB.cpp|50|error: invalid operands of types 'double' and 'double' to binary 'operator%'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
程序代码:-
#include<iostream>
#include<cmath>
using namespace std;
long long modexp(long long n,long long p,long long m)
{
if(p==0)
return 1;
else
{
long long z=modexp(n,p/2,m);
if(p%2)
return (((z*z+m)%m)*n+m)%m;
else return (z*z+m)%m;
}
}
long long round(long double a)
{
long long z=((long long)(a+0.5));
return z;
}
long long digits(long long a)
{
long long c=0;
while(a!=0)
{
a/=10;
//cout<<a<<endl;
c++;
}
return c;
}
int main()
{
int t;
cin>>t;
long long a,n,m;
for(register int i=0;i<t;i++)
{
cin>>a>>n>>m;
//cout<<(long long)(((long long)((pow(100,n)-1)/99)%m)*(a%m))%m<<endl<<(pow(100,n)-1)/99<<endl<<(((100*100-1)/99)*a)%m<<endl<<'@'<<((long long)(pow(10,floor(log10(a*10)))+0.5)-1)<<'@'<<endl;
if(a==0)
cout<<0<<endl;
else
//cout<<"p"<<digits(11)<<'@';
{
long double y=pow(10,digits(a));
long long x=(modexp(round(y),n,m*round(y-1))-1+(m*(long long)round(y-1)));
cout<<(((a+m)%m)*(((x%(m*(long long)round(y-1)))/((long long)round(y-1))+m)%m)+m)%m<<endl;
}
}
}
推荐答案
当您通过双重加价调用 round()
时,您实际上会调用:
When you call round()
with double agrument you actually call:
double round(double x);
来自< cmath>
,而不是您的版本因此错误。如果重命名,函数错误将不进行强制转换。避免使用命名空间std
的另一个原因。
from <cmath>
not your version hence the error. If you rename your function error would be gone without cast. Yet another reason to avoid using namespace std
.
这篇关于C ++函数返回错误的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!