davideberly / GeometricTools

A collection of source code for computing in the fields of mathematics, geometry, graphics, image analysis and physics.
Boost Software License 1.0
1.08k stars 202 forks source link

Floating point exceptions #62

Closed dyollb closed 1 year ago

dyollb commented 1 year ago

@davideberly, do you test for floating point exceptions in GTE/GTL? If yes, what is your strategy? Do you have any recommendations?

In our application, we enable floating-point exceptions in our MSVC builds. Internal and third-party numerical/geometric codes frequently trigger such exceptions, e.g., due to bad triangle mesh quality (normals of zero area triangles, etc.).

Note: I have not observed any FP exceptions coming from your library (I have only had good experiences, thanks for creating it!).

davideberly commented 1 year ago

I do not enable floating-point exception handling in GTE. MSVS makes handling straightforward. I looked into what effort it takes using GCC, but that looks more painful to deal with. I also had lack of success in modifying the floating-point environment in GCC. Posts that appeared via an Internet search seemed to indicate modifying the environment is not supported. For interval arithmetic, I have code that requires setting the rounding more for the FPU (FPInterval.h). MSVS allows this and it works. GCC ignored any such attempt, so I switched to using software (SWInterval.h) that calls std::nextafter (intervals are not as tight). I recently added code that needs fused-multiply-add (std::fma) and found that I could set -march=native for GCC, but I have not yet tested whether that flag somehow allows me control over the FPU state. If you have experience with this topic using GCC, I will appreciate any feedback.

I will be adding support for floating-point exceptions to GTL, at least useful for testing.

I am surprised that you have not seen FP exceptions triggered in GTE. There must be, especially when subnormals are generated. Maybe you have a limited list of exceptions that you enable handling.

My focus in recent years has been algorithm development that uses mixtures of floating-point arithmetic, interval arithmetic, rational arithmetic, quadratic-field arithmetic, and ways to obtain reasonable performance. The companies and researchers who use my meshing code tend to have good-quality triangle meshes, so perhaps I have been lucky not to receive FP exception reports.