Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "is_modulo" is false for double and float?

Tags:

c++

I wrote some code to check if a type has a modulo representation:

#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;
}

Output:

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

But we can use fmod() (from <math.h>) to find modulo of float or double. So, why is is_modulo false if it is possible to find a modulo of a float or double?

like image 310
SkrewEverything Avatar asked Dec 19 '22 00:12

SkrewEverything


1 Answers

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.

Overflow of floating point numbers is undefined behavior, therefore std::numeric_limits::is_modulo for float and double is false.

like image 128
Cory Kramer Avatar answered Dec 21 '22 14:12

Cory Kramer