How to check for NAN / INF / IND in C++
2014-01-19 19:54
429 查看
http://studiofreya.com/c/how-to-check-for-nan-inf-ind-in-c/
Recently we had occasional, but serious problems with some software we’re making. The problems were with invalid floating point numbers.
Once invalid (
numbers have infested your simulation, it’s very difficult to get rid of it. It’s like a viral infection. The best way to avoid invalid floating point numbers are to prevent them happening in the first place.
What happened was that invalid floating point values were introduced while calculating the angle between two vectors. The acos method
calculate the angle, and it’s domain is
slightly less or slightly above the domain, which resulted in an invalid number.
The way we caught the error was to modify the vector3d-class and insert breakpoints when the expression
true. Only
behave like that.
After the breakpoints were set, the call stack gave it all away.
Here is a sample program for detecting invalid numbers.
Output is:
How to check for NAN / INF / IND in C++
Recently we had occasional, but serious problems with some software we’re making. The problems were with invalid floating point numbers.Once invalid (
IND/
INF/
NAN)
numbers have infested your simulation, it’s very difficult to get rid of it. It’s like a viral infection. The best way to avoid invalid floating point numbers are to prevent them happening in the first place.
What happened was that invalid floating point values were introduced while calculating the angle between two vectors. The acos method
calculate the angle, and it’s domain is
[-1, 1]. Due to rounding errors, the actual value passed to
acoswas
slightly less or slightly above the domain, which resulted in an invalid number.
The way we caught the error was to modify the vector3d-class and insert breakpoints when the expression
value != valueis
true. Only
NANand
INDvalues
behave like that.
After the breakpoints were set, the call stack gave it all away.
Here is a sample program for detecting invalid numbers.
相关文章推荐
- What is the best way to implement a heartbeat in C++ to check for socket connectivity?
- How-to: disable the web-security-check in Chrome for Mac
- JS:Trim() in javascript, how to define a function of checkinput for a WebControl(ascx)
- How to check for and disable Java in OS X
- how to write a DLL/SO in C/C++ for Python
- How to Check for Newer Versions of Stored Pages in a Registry
- How to separate the implementation and definition for template function in c++
- How to check memory leaks in android app?
- C++--How to configure Visual Studio for compiling drivers
- How to use "for" in DOS
- How to install WebObjects in Ubuntu for free(II)
- how to make form:checkboxes in JSP
- How to thanks company and family for supporting in long career
- How to use NQ logs to check data in DB
- How to convert from the color camera space to the depth camera space in Kinect For Windows
- How to install the Nokia S60 SDK in NetBeans for J2ME development
- How to get file extension from string in C++
- How to Enable Concurrent Sessions for Remote Desktop in Windows 7 RTM
- How to check the Gateway address in Windows
- How to use binary mode for stdin/stdout in VC