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

            Assert.IsTrue(BitConversion.DoubleToRawInt64Bits(d) == l, "Wrong raw bits");
        }
            public void Test_DoubleToInt64Bits_NaN()
            {
                for (int i = 0; i < SIGNIFICAND_WIDTH - 1; i++)
                {
                    long x = 1 << i;

                    // Strip out sign and exponent bits
                    long y = x & SIGNIF_BIT_MASK;

                    double[] values =
                    {
                        BitConversion.Int64BitsToDouble(EXP_BIT_MASK | y),
                        BitConversion.Int64BitsToDouble(SIGN_BIT_MASK | EXP_BIT_MASK | y)
                    };

                    foreach (double value in values)
                    {
                        assertTrue("Invalid input " + y + "yielded non-NaN: " + value, double.IsNaN(value));
                        long converted = BitConversion.DoubleToInt64Bits(value);
                        assertTrue(string.Format("Non-canoncial NaN bits returned: {0:x8}", converted), 0x7ff8000000000000L == converted);
                    }

                    //testNanCase(1 << i);
                }
            }
        public void Test_DoubleToInt64BitsD()
        {
            // Test for method long java.lang.Double.doubleToLongBits(double)
            double d     = double.MaxValue;
            long   lbits = BitConversion.DoubleToInt64Bits(d);
            double r     = BitConversion.Int64BitsToDouble(lbits);

            Assert.IsTrue(d == r, "Bit conversion failed");
        }
Пример #4
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)));
     }
 }
Пример #5
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)));
         }
     }
 }
Пример #6
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));
            }
        }