source: trunk/contrib/cppunit-1.12.2/include/cppunit/portability/FloatingPoint.h @ 876

Last change on this file since 876 was 876, checked in by sam, 9 years ago

contrib: add a precompiled Win32 version of CppUnit and update build system
accordingly.

File size: 2.1 KB
Line 
1#ifndef CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED
2#define CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED
3
4#include <cppunit/Portability.h>
5#include <math.h>
6
7CPPUNIT_NS_BEGIN
8
9/// \brief Tests if a floating-point is a NaN.
10// According to IEEE-754 floating point standard,
11// (see e.g. page 8 of
12// http://www.cs.berkeley.edu/~wkahan/ieee754status/ieee754.ps)
13// all comparisons with NaN are false except "x != x", which is true.
14//
15// At least Microsoft Visual Studio 6 is known not to implement this test correctly.
16// It emits the following code to test equality:
17//  fcomp       qword ptr [nan]
18//  fnstsw      ax                        // copie fp (floating-point) status register to ax
19//  test        ah,40h                    // test bit 14 of ax (0x4000) => C3 of fp status register
20// According to the following documentation on the x86 floating point status register,
21// the C2 bit should be tested to test for NaN value.
22// http://webster.cs.ucr.edu/AoA/Windows/HTML/RealArithmetic.html#1000117
23// In Microsoft Visual Studio 2003 & 2005, the test is implemented with:
24//  test        ah,44h         // Visual Studio 2005 test both C2 & C3...
25//
26// To work around this, a NaN is assumed to be detected if no strict ordering is found.
27inline bool floatingPointIsUnordered( double x )
28{
29   // x != x will detect a NaN on conformant platform
30   // (2.0 < x  &&  x < 1.0) will detect a NaN on non conformant platform:
31   // => no ordering can be found for x.
32   return  (x != x) ||  (2.0 < x  &&  x < 1.0);
33}
34
35
36/// \brief Tests if a floating-point is finite.
37/// @return \c true if x is neither a NaN, nor +inf, nor -inf, \c false otherwise.
38inline int floatingPointIsFinite( double x )
39{
40#if defined(CPPUNIT_HAVE_ISFINITE)
41   return isfinite( x );
42#elif defined(CPPUNIT_HAVE_FINITE)
43   return finite( x );
44#elif defined(CPPUNIT_HAVE__FINITE)
45   return _finite(x);
46#else
47   double testInf = x * 0.0;  // Produce 0.0 if x is finite, a NaN otherwise.
48   return testInf == 0.0  &&  !floatingPointIsUnordered(testInf);
49#endif
50}
51
52CPPUNIT_NS_END
53
54#endif // CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED
Note: See TracBrowser for help on using the repository browser.