Std :: isinf does not work with -ffast-math. how to check infinity

Code example:

#include <iostream>
#include <cmath>
#include <stdint.h>

using namespace std;

static bool my_isnan(double val) {
    union { double f; uint64_t x; } u = { val };
    return (u.x << 1) > 0x7ff0000000000000u;
}

int main() {
    cout << std::isinf(std::log(0.0)) << endl;
    cout << std::isnan(std::sqrt(-1.0)) << endl;
    cout << my_isnan(std::sqrt(-1.0)) << endl;
    cout << __isnan(std::sqrt(-1.0)) << endl;

    return 0;
}

Online compiler .

C -ffast-math, this code prints "0, 0, 1, 1" - no, it prints "1, 1, 1, 1".

It is right? I thought std::isinf/ std::isnanshould work with -ffast-mathin these cases.

Also, how can I check infinity / NaN with -ffast-math? You can see what my_isnandoes this, and it really works, but this solution, of course, depends on the architecture. In addition, why my_isnandoes it work here, but std::isnan- no? How about __isnanand __isinf. Do they always work?

C -ffast-math, which is the result of std::sqrt(-1.0)and std::log(0.0). Has it become undefined, or should it be NaN / -Inf?

: (GCC) [ libstd++/50724] : isnan -ffinite-math-only g++, (Mozilla) 416287 - isNaN

+4
1

, -ffast-math / IEEE, . http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Optimize-Options.html#Optimize-Options:

-O, -Ofast, , / IEEE ISO . , , .

, -ffast-math, , .

, -ffast-math -ffinite-math-only, . http://gcc.gnu.org/wiki/FloatingPointMath, ( http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Optimize-Options.html#Optimize-Options)

[...] , , NaN + -Infs

, , -ffast-math, , NaN, , , , , isinf isnan false ( ). , .

, , NaN ( - , isinf isnan), -ffast-math, .

my_isnan ( ), . , - () ( , ), NaN , , , , std::isnan false. , , , sqrt, , NaN -1. , , .

( ) , , ++, , C- ( , , C, ++, ?).

-ffast-math my_isnan, , , , , , , .

-ffast-math -fno-finite-math-only, .

, :

  • NaNs
  • ( , NaNs, , ).

#pragma, __attribute__, -ffast-math ( -ffinite-math-only -fno-finite-math-only) ( , , GCC ) . , , , NaN. , , -ffinite-math-only .

, , -ffast-math , . , ( , , William Kahan , . ). , , (. ). , , , , ,

  • , , .

- , . , (, , ), , -ffast-math, . , :

#include <iostream>
#include <limits>

int main() {
  double d = 1.0;
  double max = std::numeric_limits<double>::max();
  d /= max;
  d *= max;
  std::cout << d << std::endl;
  return 0;
}

1, , - , -ffast-math, 0.

+10

Source: https://habr.com/ru/post/1535504/


All Articles