Added memory reinterpretation functions for int, long, float and double

git-svn-id: file:///srv/devel/repo-conversion/nusu@73 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2008-05-28 19:11:18 +00:00
parent 1132bc5681
commit bf5c8d4e19
3 changed files with 114 additions and 9 deletions

View File

@ -59,6 +59,49 @@ namespace Nuclex.Support {
); );
} }
/// <summary>Tests the integer reinterpretation functions</summary>
[Test]
public void TestIntegerReinterpretation() {
Assert.AreEqual(
12345.0f,
FloatHelper.ReinterpretAsFloat(FloatHelper.ReinterpretAsInt(12345.0f)),
"Number hasn't changed after mirrored reinterpretation"
);
}
/// <summary>Tests the long reinterpretation functions</summary>
[Test]
public void TestLongReinterpretation() {
Assert.AreEqual(
12345.67890,
FloatHelper.ReinterpretAsDouble(FloatHelper.ReinterpretAsLong(12345.67890)),
"Number hasn't changed after mirrored reinterpretation"
);
}
/// <summary>Tests the floating point reinterpretation functions</summary>
[Test]
public void TestFloatReinterpretation() {
Assert.AreEqual(
12345,
FloatHelper.ReinterpretAsInt(FloatHelper.ReinterpretAsFloat(12345)),
"Number hasn't changed after mirrored reinterpretation"
);
}
/// <summary>
/// Tests the double prevision floating point reinterpretation functions
/// </summary>
[Test]
public void TestDoubleReinterpretation() {
Assert.AreEqual(
1234567890,
FloatHelper.ReinterpretAsLong(FloatHelper.ReinterpretAsDouble(1234567890)),
"Number hasn't changed after mirrored reinterpretation"
);
}
} }
} // namespace Nuclex.Support } // namespace Nuclex.Support

View File

@ -143,6 +143,66 @@ namespace Nuclex.Support {
return (Math.Abs(leftUnion.Long - rightUnion.Long) <= maxUlps); return (Math.Abs(leftUnion.Long - rightUnion.Long) <= maxUlps);
} }
/// <summary>
/// Reinterprets the memory contents of a floating point value as an integer value
/// </summary>
/// <param name="value">
/// Floating point value whose memory contents to reinterpret
/// </param>
/// <returns>
/// The memory contents of the floating point value interpreted as an integer
/// </returns>
public static int ReinterpretAsInt(float value) {
FloatIntUnion union = new FloatIntUnion();
union.Float = value;
return union.Int;
}
/// <summary>
/// Reinterprets the memory contents of a double precision floating point
/// value as an integer value
/// </summary>
/// <param name="value">
/// Double precision floating point value whose memory contents to reinterpret
/// </param>
/// <returns>
/// The memory contents of the double precision floating point value
/// interpreted as an integer
/// </returns>
public static long ReinterpretAsLong(double value) {
DoubleLongUnion union = new DoubleLongUnion();
union.Double = value;
return union.Long;
}
/// <summary>
/// Reinterprets the memory contents of an integer as a floating point value
/// </summary>
/// <param name="value">Integer value whose memory contents to reinterpret</param>
/// <returns>
/// The memory contents of the integer value interpreted as a floating point value
/// </returns>
public static float ReinterpretAsFloat(int value) {
FloatIntUnion union = new FloatIntUnion();
union.Int = value;
return union.Float;
}
/// <summary>
/// Reinterprets the memory contents of an integer value as a double precision
/// floating point value
/// </summary>
/// <param name="value">Integer whose memory contents to reinterpret</param>
/// <returns>
/// The memory contents of the integer interpreted as a double precision
/// floating point value
/// </returns>
public static double ReinterpretAsDouble(long value) {
DoubleLongUnion union = new DoubleLongUnion();
union.Long = value;
return union.Double;
}
} }
} // namespace Nuclex.Support } // namespace Nuclex.Support

View File

@ -23,7 +23,9 @@ using System.Threading;
namespace Nuclex.Support.Tracking { namespace Nuclex.Support.Tracking {
/// <summary>Base class for actions on which that give an indication of their progress</summary> /// <summary>
/// Base class for actions on which that give an indication of their progress
/// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// By encapsulating long-running operations which will ideally be running in /// By encapsulating long-running operations which will ideally be running in