为什么"is_modulo"对于double和float来说是假的?

Skr*_*ing 4 c++

我写了一些代码来检查类型是否具有模数表示:

#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?

Cor*_*mer 6

来自cppreference

的值std::numeric_limits<T>::is_modulotrue所有算术类型Ť与模溢出处理,也就是说,如果加法,减法,乘法,或除法这种类型的的结果将落在范围外[ min(),max()],由这样的操作不同的返回值从期望值乘以倍数max()-min()+1.

浮点数的溢出是未定义的行为,因此std::numeric_limits::is_modulofloatdoublefalse.