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 double NextDown(this double value) { if (double.IsNaN(value) || value == double.NegativeInfinity) { return(value); } else { if (value == 0.0) { return(-double.MinValue); } else { return(BitConversion.Int64BitsToDouble(BitConversion.DoubleToRawInt64Bits(value) + ((value > 0.0d) ? -1L : +1L))); } } }
public void Test_DoubleToInt64Bits_NegativeInfinity() { assertTrue("Bad conversion for -infinity.", BitConversion.DoubleToInt64Bits(double.NegativeInfinity) == unchecked ((long)0xfff0000000000000L)); }
public void Test_DoubleToInt64Bits_PositiveInfinity() { assertTrue("Bad conversion for +infinity.", BitConversion.DoubleToInt64Bits(double.PositiveInfinity) == 0x7ff0000000000000L); }
public void Test_SingleToInt32Bits_NegativeInfinity() { assertTrue("Bad conversion for -infinity.", BitConversion.SingleToInt32Bits(float.NegativeInfinity) == unchecked ((int)0xFF800000)); }
public void Test_SingleToInt32Bits_PositiveInfinity() { assertTrue("Bad conversion for +infinity.", BitConversion.SingleToInt32Bits(float.PositiveInfinity) == 0x7F800000); }
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)); } }
public static bool IsNegativeZero(this double d) { return(d == 0 && BitConversion.DoubleToRawInt64Bits(d) == BitConversion.DoubleToRawInt64Bits(NegativeZero)); }
public static bool IsNegativeZero(this float f) { return(f == 0 && BitConversion.SingleToRawInt32Bits(f) == BitConversion.SingleToRawInt32Bits(NegativeZero)); }