* ./hurricane/src/hurricane :

- New: Utility functions to manage float rounding errors.
This commit is contained in:
Jean-Paul Chaput 2009-05-13 14:58:53 +00:00
parent e0c9f0ba3f
commit 0c3c37066d
2 changed files with 32 additions and 0 deletions

View File

@ -44,6 +44,7 @@
#include <cstdio>
#include <cassert>
#include <cmath>
#include <tr1/memory>
#include <string>
@ -132,6 +133,33 @@ namespace Hurricane {
inline string demangle ( const type_info& info ) { return demangle(info.name()); }
// For a complete explanation of this function, please look at :
// http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
inline int floatCompare ( float a, float b )
{
assert ( sizeof(float) == sizeof(int) );
if ( a == b ) return 0;
return *(int*)&a - *(int*)&b;
}
inline int floatDifference ( float a, float b, int threshold )
{
int difference = floatCompare(a,b);
if ( abs(difference) < threshold ) return 0;
return (difference<0) ? -1 : 1;
}
inline void floatRound ( float& value, float precision )
{
float rounded = roundf ( value*precision );
value = rounded / precision;
}
} // End of Hurricane namespace.

View File

@ -296,6 +296,10 @@ INSPECTOR_P_SUPPORT(Hurricane::Net::ComponentSet);
INSPECTOR_P_SUPPORT(Hurricane::Net::RubberSet);
INSPECTOR_PV_SUPPORT(Hurricane::Net::Type);
INSPECTOR_PV_SUPPORT(Hurricane::Net::Direction);
IOSTREAM_POINTER_SUPPORT(Hurricane::Net::Type::Code);
IOSTREAM_VALUE_SUPPORT(Hurricane::Net::Type::Code);
IOSTREAM_POINTER_SUPPORT(Hurricane::Net::Direction::Code);
IOSTREAM_VALUE_SUPPORT(Hurricane::Net::Direction::Code);
#endif // HURRICANE_NET