jeudi 26 octobre 2017

Floating point formats and isinf() in C++

I am currently dealing with floating point values in C++. Consider the following c++ snippet:

#include <cmath>
#include <cstring>
#include <iostream>

int main() {
    long double num;

    // Set num to a large, valid, floating point value
    memset(&num, 0xcc, sizeof(num));

    std::cout << "num = " << num << std::endl;
    std::cout << "isinf(num) = " << isinf(num) << std::endl;
    std::cout << "std::isinf(num) = " << std::isinf(num) << std::endl;

    return 0;
}

According to Wikipedia, this creates an 80 bit extended precision floating point value, as I am using GCC on an x86 machine. The floating point value is therefore 0xcccc cccc cccc cccc cccc and should be valid value.

Interestingly, the output is as follows:

num = -4.77987e+986
isinf(num) = 1
std::isinf(num) = 0

This makes me wonder:

  • Why behave isinf and std::isinf differently? And which one is to be trusted?
  • Here it says that in C99, isinf is a macro, while in C++11, it became a function. However, when i compile the example e.g. with -std=c++98, i still get the same behavior. Shouldn't std::isinf not even be defined in that case?
  • In general, how can i tell which floating point format my compiler/architecture uses?

Aucun commentaire:

Enregistrer un commentaire