Example #1
0
        public static void GetDoubleParts(double dbl, out int sign, out int exp, out ulong man, out bool fFinite)
        {
            ulong bits = BitConverter.DoubleToUInt64Bits(dbl);

            sign = 1 - ((int)(bits >> 62) & 2);
            man  = bits & 0x000FFFFFFFFFFFFF;
            exp  = (int)(bits >> 52) & 0x7FF;
            if (exp == 0)
            {
                // Denormalized number.
                fFinite = true;
                if (man != 0)
                {
                    exp = -1074;
                }
            }
            else if (exp == 0x7FF)
            {
                // NaN or Infinite.
                fFinite = false;
                exp     = int.MaxValue;
            }
            else
            {
                fFinite = true;
                man    |= 0x0010000000000000;
                exp    -= 1075;
            }
        }
Example #2
0
        public static void DoubleToUInt64Bits()
        {
            double input  = 123456.3234;
            ulong  result = BitConverter.DoubleToUInt64Bits(input);

            Assert.Equal(4683220267154373240UL, result);
            double roundtripped = BitConverter.UInt64BitsToDouble(result);

            Assert.Equal(input, roundtripped);
        }
Example #3
0
        public static UInt64 ToBits(this Double num)
        {
#if BITWISE_FLOAT_CONVERSION
            return(BitConverter.DoubleToUInt64Bits(num));
#else
            unsafe
            {
                return(*(UInt64 *)&num);
            }
#endif
        }
Example #4
0
        public void TestParserDouble_SpecificPowers(int b, int start, int end)
        {
            for (int i = start; i != end; i++)
            {
                double d    = Math.Pow(b, i);
                ulong  bits = BitConverter.DoubleToUInt64Bits(d);

                TestRoundTripDouble(bits - 1);
                TestRoundTripDouble(bits);
                TestRoundTripDouble(bits + 1);
            }
        }
        private static void AssertBitwiseEqual(double expected, double actual)
        {
            ulong expectedBits = BitConverter.DoubleToUInt64Bits(expected);
            ulong actualBits   = BitConverter.DoubleToUInt64Bits(actual);

            if (expectedBits == actualBits)
            {
                return;
            }

            if (Complex.IsNaN(expected) && Complex.IsNaN(actual))
            {
                return;
            }

            throw new Xunit.Sdk.EqualException(expected, actual);
        }
Example #6
0
 public static void PositiveInfinity()
 {
     Assert.Equal(1.0 / 0.0, double.PositiveInfinity);
     Assert.Equal(0x7FF00000_00000000u, BitConverter.DoubleToUInt64Bits(double.PositiveInfinity));
 }
Example #7
0
 public static void NegativeInfinity()
 {
     Assert.Equal(-1.0 / 0.0, double.NegativeInfinity);
     Assert.Equal(0xFFF00000_00000000u, BitConverter.DoubleToUInt64Bits(double.NegativeInfinity));
 }
Example #8
0
 public static void NaN()
 {
     Assert.Equal(0.0 / 0.0, double.NaN);
     Assert.Equal(0xFFF80000_00000000u, BitConverter.DoubleToUInt64Bits(double.NaN));
 }
Example #9
0
 public static void MinValue()
 {
     Assert.Equal(-1.7976931348623157E+308, double.MinValue);
     Assert.Equal(0xFFEFFFFF_FFFFFFFFu, BitConverter.DoubleToUInt64Bits(double.MinValue));
 }
Example #10
0
 public static void Epsilon()
 {
     Assert.Equal(4.9406564584124654E-324, double.Epsilon);
     Assert.Equal(0x00000000_00000001u, BitConverter.DoubleToUInt64Bits(double.Epsilon));
 }
Example #11
0
 // Writes a double to this stream. The current position of the stream is
 // advanced by eight.
 //
 //| <include file='doc\BinaryWriter.uex' path='docs/doc[@for="BinaryWriter.Write8"]/*' />
 public virtual void Write(double value)
 {
     Write(BitConverter.DoubleToUInt64Bits(value));
 }
Example #12
0
 public static void MinTest(double left, double right, double expected)
 {
     Assert.That(() => BitConverter.DoubleToUInt64Bits(MathUtilities.Min(left, right)),
                 Is.EqualTo(BitConverter.DoubleToUInt64Bits(expected))
                 );
 }
Example #13
0
        public static uint ExtractMostSignificantBit(T value)
        {
            if (typeof(T) == typeof(byte))
            {
                uint bits = (byte)(object)value;
                return(bits >> 7);
            }
            else if (typeof(T) == typeof(double))
            {
                ulong bits = BitConverter.DoubleToUInt64Bits((double)(object)value);
                return((uint)(bits >> 63));
            }
            else if (typeof(T) == typeof(short))
            {
                uint bits = (ushort)(short)(object)value;
                return(bits >> 15);
            }
            else if (typeof(T) == typeof(int))
            {
                uint bits = (uint)(int)(object)value;
                return(bits >> 31);
            }
            else if (typeof(T) == typeof(long))
            {
                ulong bits = (ulong)(long)(object)value;
                return((uint)(bits >> 63));
            }
            else if (typeof(T) == typeof(nint))
            {
#if TARGET_64BIT
                ulong bits = (ulong)(nint)(object)value;
                return((uint)(bits >> 63));
#else
                uint bits = (uint)(nint)(object)value;
                return(bits >> 31);
#endif
            }
            else if (typeof(T) == typeof(nuint))
            {
#if TARGET_64BIT
                ulong bits = (ulong)(nuint)(object)value;
                return((uint)(bits >> 63));
#else
                uint bits = (uint)(nuint)(object)value;
                return(bits >> 31);
#endif
            }
            else if (typeof(T) == typeof(sbyte))
            {
                uint bits = (byte)(sbyte)(object)value;
                return(bits >> 7);
            }
            else if (typeof(T) == typeof(float))
            {
                uint bits = BitConverter.SingleToUInt32Bits((float)(object)value);
                return(bits >> 31);
            }
            else if (typeof(T) == typeof(ushort))
            {
                uint bits = (ushort)(object)value;
                return(bits >> 15);
            }
            else if (typeof(T) == typeof(uint))
            {
                uint bits = (uint)(object)value;
                return(bits >> 31);
            }
            else if (typeof(T) == typeof(ulong))
            {
                ulong bits = (ulong)(object)value;
                return((uint)(bits >> 63));
            }
            else
            {
                throw new NotSupportedException(SR.Arg_TypeNotSupported);
            }
        }