# Always use feenableexcept() when doing floating point math

DevFeed: [Always use feenableexcept() when doing floating point math](<https://devfeed.tech/articles/always-use-feenableexcept-when-doing-floating-point-math-36251.md>)

Original publisher: [Read original article](<https://berthub.eu/articles/posts/always-do-this-floating-point/>)

Published: 2022-12-24T20:28:43Z

Content type: tutorial

Language: en

Sources: [Bert Hubert's writings](<https://devfeed.tech/sources/bert-hubert-s-writings.md>)

Topics: [floating-point](<https://devfeed.tech/topics/floating-point.md>), [C](<https://devfeed.tech/topics/c.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [exceptions](<https://devfeed.tech/topics/exceptions.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [gcc](<https://devfeed.tech/topics/gcc.md>), [Linux](<https://devfeed.tech/topics/linux.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [exceptions](<https://devfeed.tech/tags/exceptions.md>), [floating-point](<https://devfeed.tech/tags/floating-point.md>), [linux](<https://devfeed.tech/tags/linux.md>), [math](<https://devfeed.tech/tags/math.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

This tutorial explains how floating-point exceptions, infinities, and NaNs can occur silently in C and C++ programs, especially under compiler optimization. It describes enabling floating-point exceptions under Linux with feenableexcept() and notes remaining issues involving SIMD optimization.

## Source excerpt

This is a refreshed & expanded copy of a very old page I hosted outside of this blog. I recently ran into "silent NaNs" again, and thought it might be a good idea to republish this advice here. A small post that documents something that almost no one appears to know. And if you do anything with floating point, you do need to know. Exceptions In C or C++, try this: