我写了一些代码来检查类型是否具有模数表示:
#include <iostream>
#include <limits>
using namespace std;
int main( )
{
cout << "Whether float objects have a modulo representation: "
<< numeric_limits<float>::is_modulo << endl;
cout << "Whether double objects have a modulo representation: "
<< numeric_limits<double>::is_modulo << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Whether float objects have a modulo representation: 0
Whether double objects have a modulo representation: 0
Run Code Online (Sandbox Code Playgroud)
但我们可以使用fmod()(from <math.h>)来找到float或的模数double.那么,is_modulo如果可以找到float或double的模数,为什么是false?
的值
std::numeric_limits<T>::is_modulo是true所有算术类型Ť与模溢出处理,也就是说,如果加法,减法,乘法,或除法这种类型的的结果将落在范围外[min(),max()],由这样的操作不同的返回值从期望值乘以倍数max()-min()+1.
浮点数的溢出是未定义的行为,因此std::numeric_limits::is_modulo对float和double是false.