dimanche 24 mai 2020

Does raising by too small double result in 0.0? If not, how to achieve this?

I am dealing with floating point very small in magnitude and would like to round to 0 whenever the floating point cannot be represented by a double because it is too small in magnitude.

This code:

#include <iostream>
#include <cmath>

int main() {
  double x{100.0};
  double y{(-1.0) * 1.7976931348623157e308};
  double z{std::pow(x, y)};
  std::cout << typeid(z).name() << ": " << z << std::endl;
  std::cout << (z == 0) << std::endl;
  return 0;
}

prints

$ ./a.out
d: 0
1

for me (clang version 11.0.0, -std=c++11), as desired.

Question: Do expressions that result in a floating point too small in magnitude to be represented by a double always evaluate to 0 when assigned to a double? (including for other C++ compilers?) If not, how can I achieve this behavior, or test for such an expression being too small in magnitude?

Aucun commentaire:

Enregistrer un commentaire