* ./hurricane/src/hurricane :
- New: Utility functions to manage float rounding errors.
This commit is contained in:
parent
e0c9f0ba3f
commit
0c3c37066d
|
@ -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.
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue