public void Test_SingleToRawInt32BitsF() { int i = 0x7fc004d2; float f = BitConversion.Int32BitsToSingle(i); Assert.IsTrue(BitConversion.SingleToRawInt32Bits(f) == i, "Wrong raw bits"); }
public void Test_SingleToInt32Bits_NaN() { for (int i = 0; i < SIGNIFICAND_WIDTH - 1; i++) { int x = 1 << i; // Strip out sign and exponent bits int y = x & SIGNIF_BIT_MASK; float[] values = { BitConversion.Int32BitsToSingle(EXP_BIT_MASK | y), BitConversion.Int32BitsToSingle(SIGN_BIT_MASK | EXP_BIT_MASK | y) }; foreach (float value in values) { assertTrue("Invalid input " + y + "yielded non-NaN: " + value, float.IsNaN(value)); int converted = BitConversion.SingleToInt32Bits(value); assertTrue(string.Format("Non-canoncial NaN bits returned: {0:x8}", converted), 0x7fc00000 == converted); } //testNanCase(1 << i); } }
public void Test_Int32BitsToSingleI() { float f = 9876.2345f; int bits = BitConversion.SingleToInt32Bits(f); float r = BitConversion.Int32BitsToSingle(bits); Assert.AreEqual(f, r, 0F, "Incorrect intBits returned"); }
public void Test_SingleToInt32BitsF() { float f = 9876.2345f; int bits = BitConversion.SingleToInt32Bits(f); float r = BitConversion.Int32BitsToSingle(bits); Assert.IsTrue(f == r, "Incorrect intBits returned"); }
public static float NextUp(this float value) { if (float.IsNaN(value) || value == float.PositiveInfinity) { return(value); } else { value += 0.0f; return(BitConversion.Int32BitsToSingle(BitConversion.SingleToRawInt32Bits(value) + ((value >= 0.0f) ? +1 : -1))); } }
public static float NextDown(this float value) { if (float.IsNaN(value) || value == float.NegativeInfinity) { return(value); } else { if (value == 0.0f) { return(-float.MinValue); } else { return(BitConversion.Int32BitsToSingle(BitConversion.SingleToRawInt32Bits(value) + ((value > 0.0f) ? -1 : +1))); } } }
public static float NextAfter(this float start, double direction) { /* * The cases: * * NextAfter(+infinity, 0) == MaxValue * NextAfter(+infinity, +infinity) == +infinity * NextAfter(-infinity, 0) == -MaxValue * NextAfter(-infinity, -infinity) == -infinity * * are naturally handled without any additional testing */ // First check for NaN values if (float.IsNaN(start) || double.IsNaN(direction)) { // return a NaN derived from the input NaN(s) return(start + (float)direction); } else if (start == direction) { return((float)direction); } else { // start > direction or start < direction // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0) // then bitwise convert start to integer. int transducer = BitConversion.SingleToRawInt32Bits(start + 0.0f); /* * IEEE 754 floating-point numbers are lexicographically * ordered if treated as signed- magnitude integers . * Since .NET's integers are two's complement, * incrementing" the two's complement representation of a * logically negative floating-point value *decrements* * the signed-magnitude representation. Therefore, when * the integer representation of a floating-point values * is less than zero, the adjustment to the representation * is in the opposite direction than would be expected at * first. */ if (direction > start) {// Calculate next greater value transducer = transducer + (transducer >= 0 ? 1 : -1); } else { // Calculate next lesser value Debug.Assert(direction < start); if (transducer > 0) { --transducer; } else if (transducer < 0) { ++transducer; } /* * transducer==0, the result is -MinValue * * The transition from zero (implicitly * positive) to the smallest negative * signed magnitude value must be done * explicitly. */ else { transducer = SingleSignBitMask | 1; } } return(BitConversion.Int32BitsToSingle(transducer)); } }