我写了一些代码来检查一个类型是否有模表示:
#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?
最佳答案
The value of
std::numeric_limits<T>::is_modulo
istrue
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 ofmax()-min()+1
.
float 溢出is undefined behavior ,因此 std::numeric_limits::is_modulo
对于 float
和 double
是false
.
关于c++ - 为什么 "is_modulo"对于 double 和 float 是假的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230387/