我正在构建一个C++程序来验证一个数学猜想,迭代次数高达1000亿次。为了测试这么高的数字,我不能使用C++ int,所以我使用NTL库,使用类型ZZ作为我的number类型。
我的算法是这样的:
ZZ generateNthSeq(ZZ n)
{
return floor(n*sqrt(2));
}我有两个要导入的库:
#include <cmath>
#include <NTL/ZZ.h>但很明显,这不能编译,因为我得到了错误:
$ g++ deepness*.cpp
deepness.cpp: In function ‘NTL::ZZ generateNthSeq(NTL::ZZ)’:
deepness.cpp:41: error: no matching function for call to ‘floor(NTL::ZZ)’
/usr/include/bits/mathcalls.h:185: note: candidates are: double floor(double)
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/cmath:262: note: long double std::floor(long double)
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/cmath:258: note: float std::floor(float)声明floor数学运算不能接受ZZ类类型。但我需要的是很大的数字。在使用NTL库的同时,我如何才能完成我想要做的事情呢?
发布于 2015-05-28 01:00:14
请注意,将floor应用于整型并没有实际意义(它确实如此,它只是一个无操作)。您真正应该担心的是,您的代码显然是在向floor传递ZZ类型的内容!
也就是说,n * sqrt(2)在这里可能是什么意思?
另外,在写这篇文章之前,我已经检查了文档,看看integer *浮点是否真的存在于库中--通常为了让它有用,你需要任意精度的浮点类型。
检查标题,只有一个乘法运算符:
ZZ operator*(const ZZ& a, const ZZ& b);还有一个转换构造函数:
explicit ZZ(long a); // promotion constructor我甚至不知道你的代码是如何编译的。也许您使用的库版本与我看到的不同,并且转换构造函数是隐式的,并且您的double将被“提升”为ZZ。这肯定不是您想要的,因为将sqrt(2)提升为ZZ只会给出整数1。
您需要:
检查NTL是否有任意精度的浮点数,如果库中有任意精度的浮点数,请检查capabilities
。
最后一个问题在这里相当简单:您希望
return SqrRoot(sqr(n) * 2); // sqr(n) will be a bit more efficient than `n * n`https://stackoverflow.com/questions/30488643
复制相似问题