private static float ToSingle(uint value) { var t = new SingleToUInt32 { UInt32 = value }; return(t.Single); }
private static uint ToUint32(float value) { var t = new SingleToUInt32 { Single = value }; return(t.UInt32); }
public static int Main() { SingleToUInt32 test = new SingleToUInt32(); TestLibrary.TestFramework.BeginTestCase("SingleToUInt32"); if (test.RunTests()) { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("PASS"); return(100); } else { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("FAIL"); return(0); } }
public static int Main() { SingleToUInt32 test = new SingleToUInt32(); TestLibrary.TestFramework.BeginTestCase("SingleToUInt32"); if (test.RunTests()) { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("PASS"); return 100; } else { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("FAIL"); return 0; } }
private static uint ToUint32(float value) { var t = new SingleToUInt32 { Single = value }; return t.UInt32; }
private static float ToSingle(uint value) { var t = new SingleToUInt32 { UInt32 = value }; return t.Single; }
// ReSharper restore FieldCanBeMadeReadOnly.Local /// <summary> /// Returns a value indicating whether the specified number is <see cref="float.NaN"/>. /// </summary> /// <param name="value">A single-precision floating-point number.</param> /// <returns> /// <see langword="true"/> if <paramref name="value"/> evaluates to <see cref="float.NaN"/>; /// otherwise, <see langword="false"/>. /// </returns> /// <remarks> /// The standard CLR <see cref="float.IsNaN"/> function is slower than this wrapper, so please /// make sure to use this <see cref="IsNaN(float)"/> in performance sensitive code. /// </remarks> public static bool IsNaN(float value) { // IEEE 754: // msb means most significant bit // lsb means least significant bit // 1 8 23 ... widths // +-+-------+-----------------------+ // |s| exp | man | // +-+-------+-----------------------+ // msb lsb msb lsb ... order // // If exp = 255 and man != 0, then value is NaN regardless of s. // // => If the argument is any value in the range 0x7f800001 through 0x7fffffff or in the range // 0xff800001 through 0xffffffff, the result will be NaN. SingleToUInt32 t = new SingleToUInt32 { Single = value }; UInt32 exp = t.UInt32 & 0x7f800000; UInt32 man = t.UInt32 & 0x007fffff; return exp == 0x7f800000 && man != 0; }
public static uint GetSignificantBitsUnsigned(float value, int n) { // Reference: http://aras-p.info/blog/2014/01/16/rough-sorting-by-depth/ // Example: // Taking highest 9 bits for rough sort for positive floats. // 0.01 maps to 240, // 0.1 maps to 247, // 1.0 maps to 254, // 10.0 maps to 260, // 100.0 maps to 267, // 1000.0 maps to 273, etc. SingleToUInt32 t = new SingleToUInt32 { Single = value }; uint b = t.UInt32 >> (31 - n); // Take highest n bits. return b; }
public static uint GetSignificantBitsSigned(float value, int n) { // Example: // Taking highest 10 bits for rough sort for positive floats. // 0.01 maps to 752, // 0.1 maps to 759, // 1.0 maps to 766, // 10.0 maps to 772, // 100.0 maps to 779, etc. // Negative numbers go similarly in 0 ... 511 range. SingleToUInt32 t = new SingleToUInt32 { Single = value }; t.UInt32 = FloatFlip(t.UInt32); // Flip bits to be sortable. uint b = t.UInt32 >> (32 - n); // Take highest n bits. return b; }