Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang++] FE_INVALID exception wrongly caught #123317

Open
thomasbangels opened this issue Jan 17, 2025 · 2 comments
Open

[clang++] FE_INVALID exception wrongly caught #123317

thomasbangels opened this issue Jan 17, 2025 · 2 comments

Comments

@thomasbangels
Copy link

thomasbangels commented Jan 17, 2025

When the FE_INVALID exception trap is enabled, an exception is caught but there is no reason why this code should produce such an exception.

Script:

#include <cfenv>
#include <iostream>
#include <Eigen/Eigen>

int main()
{
  feenableexcept(FE_INVALID);

  // Ask number from terminal so the compiler cannot optimize at compile time.
  // Enter 0.0
  double a;
  std::cout << "input number: " << std::flush;
  std::cin >> a;

  Eigen::Vector3d x1(1.0 + a, 2.0 + a, 3.0 + a);
  Eigen::Vector3d x2(1.0, 2.0, 3.0);

  Eigen::Vector3d v = x2 - x1;
  double dist = v.norm();
  std::cout << "dist = " << dist << std::endl;
  if (dist == 0) {
    // std::cout << "if-branch" << std::endl;
    v = Eigen::Vector3d(1, 0, 0);
  } else {
    // std::cout << "else-branch" << std::endl;
    v /= dist;
  }
  std::cout << "v = " << v << std::endl;

  return 0;
}

How to reproduce the error:

  • Compile using clang++ with -O3 optimizations
  • When the "input number" is asked, enter 0.0

I found out that no exception is caught (which is correct) in the following scenarios:

  • When the prints, commented out in the branches, are re-enabled
  • When compiling with clang++ without the -O3 optimizations
  • When compiling with g++ (with and without optimizations)

System details:
Operating system: Ubuntu 24.04
Clang version: 18.1.3
GCC version: 13.3.0
Eigen version: 3.3.90

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 17, 2025
@thomasbangels thomasbangels changed the title Clang wrongly catches FE_INVALID exception [clang++] FE_INVALID exception wrongly caught Jan 17, 2025
@EugeneZelenko EugeneZelenko added llvm:optimizations and removed clang Clang issues not falling into any other category labels Jan 17, 2025
@topperc
Copy link
Collaborator

topperc commented Jan 17, 2025

clang is probably assuming default FP environment. Does it have an exception with -ftrapping-math or #pragma STDC FENV_ACCESS ON?

@thomasbangels
Copy link
Author

You are right. The exception does not occur when compiling with -ftrapping-math or when adding #pragma STDC FENV_ACCESS ON.

With this information, would you then regard this case as a bug or is the advice to always add -ftrapping-math or #pragma STDC FENV_ACCESS ON when searching for floating point exceptions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants