/// <summary> /// math.frexp implementation in C# /// </summary> /// <param name="value"></param> /// <returns></returns> public static FRexpResult frexp(double value) { FRexpResult result = new FRexpResult(); long bits = BitConverter.DoubleToInt64Bits(value); double realMant = 1; // Test for NaN, infinity, and zero. if (Double.IsNaN(value) || value + value == value || Double.IsInfinity(value)) { result.exponent = 0; result.mantissa = value; } else { bool neg = (bits < 0); int exponent = (int)((bits >> 52) & 0x7ffL); long mantissa = bits & 0xfffffffffffffL; if (exponent == 0) { exponent++; } else { mantissa = mantissa | (1L << 52); } // bias the exponent - actually biased by 1023. // we are treating the mantissa as m.0 instead of 0.m // so subtract another 52. exponent -= 1075; realMant = mantissa; // normalize while (realMant > 1.0) { mantissa >>= 1; realMant /= 2; exponent++; } if (neg) { realMant = realMant * -1; } result.exponent = exponent; result.mantissa = realMant; } return(result); }
/// <summary> /// math.frexp implementation in C# /// </summary> /// <param name="value"></param> /// <returns></returns> public static FRexpResult frexp(double value) { FRexpResult result = new FRexpResult(); long bits = BitConverter.DoubleToInt64Bits(value); double realMant = 1; // Test for NaN, infinity, and zero. if (Double.IsNaN(value) || value + value == value || Double.IsInfinity(value)) { result.exponent = 0; result.mantissa = value; } else { bool neg = (bits < 0); int exponent = (int)((bits >> 52) & 0x7ffL); long mantissa = bits & 0xfffffffffffffL; if (exponent == 0) { exponent++; } else { mantissa = mantissa | (1L << 52); } // bias the exponent - actually biased by 1023. // we are treating the mantissa as m.0 instead of 0.m // so subtract another 52. exponent -= 1075; realMant = mantissa; // normalize while (realMant > 1.0) { mantissa >>= 1; realMant /= 2; exponent++; } if (neg) { realMant = realMant * -1; } result.exponent = exponent; result.mantissa = realMant; } return result; }