The C++ program shown below gives (when compiled under Ubuntu 16.04
using g++ in c++11 mode) the output

MATH_ERRNO is set
MATH_ERREXCEPT is set

2^2000 = inf
ERROR: numerical overflow

2^2000 = inf
no error

1/2^2000 = 0
ERROR: numerical underflow

1/2^2000 = 0
no error


Whereas the expected output is

MATH_ERRNO is set
MATH_ERREXCEPT is set

2^2000 = inf
ERROR: numerical overflow

2^2000 = inf
ERROR: numerical overflow

1/2^2000 = 0
ERROR: numerical underflow

1/2^2000 = 0
ERROR: numerical underflow


Replacing g++ by clang++ (version 3.8) or icpc (version 16.0.2) gives correct 
results. Here, the test program follows:


#pragma STDC FENV_ACCESS ON

#include <cstdlib>
#include <iostream>
#include <cfenv>
#include <cmath>

void test_fp_exceptions() {
  bool error=false;
  if (std::fetestexcept(FE_DIVBYZERO)) {
    error=true;
    std::cerr << "ERROR: division by zero\n";
  }
  if (std::fetestexcept(FE_OVERFLOW)) {
    error=true;
    std::cerr << "ERROR: numerical overflow\n";
  }
  if (std::fetestexcept(FE_UNDERFLOW)) {
    error=true; 
    std::cerr << "ERROR: numerical underflow\n";
  }
  if (std::fetestexcept(FE_INVALID)) {
    error=true;
    std::cerr << "ERROR: invalid result\n";
  }
  if (not error)
    std::cerr << "no error\n";
  std::feclearexcept(FE_ALL_EXCEPT);
  std::cerr << '\n';
}

int main() {
  std::cout << "MATH_ERRNO is "
            << (math_errhandling & MATH_ERRNO ? "set" : "not set") << '\n'
            << "MATH_ERREXCEPT is "
            << (math_errhandling & MATH_ERREXCEPT ? "set" : "not set") << '\n'
            << '\n';
  std::feclearexcept(FE_ALL_EXCEPT);
  std::cout << "2^2000 = " << std::pow(2., 2000) << '\n';
  test_fp_exceptions();
  std::cout << "2^2000 = " << std::pow(2., 2000.) << '\n';
  test_fp_exceptions();
  std::cout << "1/2^2000 = " << std::pow(2., -2000) << '\n';
  test_fp_exceptions();
  std::cout << "1/2^2000 = " << std::pow(2., -2000.) << '\n';
  test_fp_exceptions();
  return EXIT_SUCCESS;
}

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/429113

Title:
  libm pow() function does not report floating point underflow exception

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/429113/+subscriptions

-- 
ubuntu-bugs mailing list
ubuntu-bugs@lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to