Anyone care to explain in detail (code by code) what this is doing? I have read Bruce Dawson's paper on comparing floats and have found converted C# code of it but don't quite understand it. What is maxDeltaBits
and its purpose? In Dawson's paper it states that this can also be applied to double
, so if that is the case then would you need to convert the double
value to Int64
instead of int32
?
public static int FloatToInt32Bits( float f )
{
return BitConverter.ToInt32( BitConverter.GetBytes( b ), 0 );
}
public static bool AlmostEqual2sComplement
( float a, float b, int maxDeltaBits )
{
int aInt = FloatToInt32Bits( a );
if ( aInt < 0 ) // Why only if it is less than 0?
aInt = Int32.MinValue - aInt; // What is the purpose of this?
int bInt = FloatToInt32Bits( b );
if ( bInt < 0 ) // Why only if it is less than 0?
bInt = Int32.MinValue - bInt; // What is the purpose of this?
int intDiff = Math.Abs( aInt - bInt );
return intDiff <= ( 1 << maxDeltaBits ); // Why ( 1 << maxDeltaBits )?
}