public void Test_DoubleToRawInt64BitsD()
        {
            long   l = 0x7ff80000000004d2L;
            double d = BitConversion.Int64BitsToDouble(l);

            Assert.IsTrue(BitConversion.DoubleToRawInt64Bits(d) == l, "Wrong raw bits");
        }
Exemple #2
0
 public static double NextUp(this double value)
 {
     if (double.IsNaN(value) || value == double.PositiveInfinity)
     {
         return(value);
     }
     else
     {
         value += 0.0d;
         return(BitConversion.Int64BitsToDouble(BitConversion.DoubleToRawInt64Bits(value) +
                                                ((value >= 0.0d) ? +1L : -1L)));
     }
 }
Exemple #3
0
 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)));
         }
     }
 }
Exemple #4
0
        public static double NextAfter(this double 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 (double.IsNaN(start) || double.IsNaN(direction))
            {
                // return a NaN derived from the input NaN(s)
                return(start + direction);
            }
            else if (start == direction)
            {
                return(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.
                long transducer = BitConversion.DoubleToRawInt64Bits(start + 0.0d);

                /*
                 * 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 >= 0L ? 1L : -1L);
                }
                else
                { // Calculate next lesser value
                    Debug.Assert(direction < start);
                    if (transducer > 0L)
                    {
                        --transducer;
                    }
                    else
                    if (transducer < 0L)
                    {
                        ++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 = DoubleSignBitMask | 1L;
                    }
                }

                return(BitConversion.Int64BitsToDouble(transducer));
            }
        }
Exemple #5
0
 public static bool IsNegativeZero(this double d)
 {
     return(d == 0 && BitConversion.DoubleToRawInt64Bits(d) == BitConversion.DoubleToRawInt64Bits(NegativeZero));
 }