//| <include file='doc\BinaryReader.uex' path='docs/doc[@for="BinaryReader.ReadDouble"]/*' /> public virtual double ReadDouble() { FillBuffer(8); uint lo = (uint)(m_buffer[0] | (m_buffer[1]) << 8 | (m_buffer[2]) << 16 | m_buffer[3] << 24); uint hi = (uint)(m_buffer[4] | (m_buffer[5]) << 8 | (m_buffer[6]) << 16 | m_buffer[7] << 24); return(BitConverter.UInt64BitsToDouble(((ulong)hi) << 32 | lo)); }
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); }
private void TestRoundTripDouble(ulong bits) { double d = BitConverter.UInt64BitsToDouble(bits); if (double.IsFinite(d)) { string s = InvariantToStringDouble(d); CheckOneDouble(s, bits); } }
public static Double ToFloat(this UInt64 bits) { #if BITWISE_FLOAT_CONVERSION return(BitConverter.UInt64BitsToDouble(bits)); #else unsafe { return(*(Double *)&bits); } #endif }
public static double GetDoubleFromParts(int sign, int exp, ulong man) { ulong bits; if (man == 0) { bits = 0; } else { // Normalize so that 0x0010 0000 0000 0000 is the highest bit set. int cbitShift = BitOperations.LeadingZeroCount(man) - 11; if (cbitShift < 0) { man >>= -cbitShift; } else { man <<= cbitShift; } exp -= cbitShift; Debug.Assert((man & 0xFFF0000000000000) == 0x0010000000000000); // Move the point to just behind the leading 1: 0x001.0 0000 0000 0000 // (52 bits) and skew the exponent (by 0x3FF == 1023). exp += 1075; if (exp >= 0x7FF) { // Infinity. bits = 0x7FF0000000000000; } else if (exp <= 0) { // Denormalized. exp--; if (exp < -52) { // Underflow to zero. bits = 0; } else { bits = man >> -exp; Debug.Assert(bits != 0); } } else { // Mask off the implicit high bit. bits = (man & 0x000FFFFFFFFFFFFF) | ((ulong)exp << 52); } } if (sign < 0) { bits |= 0x8000000000000000; } return(BitConverter.UInt64BitsToDouble(bits)); }
private void CheckOneDouble(string s, ulong expectedBits) { CheckOneDouble(s, BitConverter.UInt64BitsToDouble(expectedBits)); }