c++ - 为什么 "is_modulo"对于 double 和 float 是假的?

标签 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;
}

输出:

Whether float objects have a modulo representation: 0
Whether double objects have a modulo representation: 0

但是我们可以使用fmod() (来自 <math.h> )找到 float 的模数或 double .那么,为什么 is_modulo如果可以找到 float 或 double 的模数,则为 false?

最佳答案

From cppreference

The value of std::numeric_limits<T>::is_modulo is true for all arithmetic types T that handle overflows with modulo arithmetic, that is, if the result of addition, subtraction, multiplication, or division of this type would fall outside the range [min(), max()], the value returned by such operation differs from the expected value by a multiple of max()-min()+1.

float 溢出is undefined behavior ,因此 std::numeric_limits::is_modulo对于 floatdoublefalse .

关于c++ - 为什么 "is_modulo"对于 double 和 float 是假的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230387/

相关文章:

c++ - g++ 模板错误 Small_size

c++ - 无法加载, undefined symbol C++ 名称修改

c++ - 一个线程等待多个线程事件

c++ - c++中object * x和object& x有什么区别

c++ - 为什么 cppcheck 工具找不到未初始化的变量?

c++ - OpenCV 错误可能是由于不同的 "step"

c++ - 是否可以在非托管代码中使用 Mock/Fake 框架?

c++ - 如何仅测试字母的 u32string(使用语言环境)

c++ - 加载和卸载 shell 扩展库

c++ - 如何通过不同的类将一个 vector 复制到另一个 vector