/// <summary> /// /// </summary> /// <param name="d1"></param> /// <param name="d2"></param> /// <returns></returns> public static double MaximumCommonMantissa(double d1, double d2) { if (d1 == 0.0 || d2 == 0.0) return 0.0; DoubleBits db1 = new DoubleBits(d1); DoubleBits db2 = new DoubleBits(d2); if (db1.Exponent != db2.Exponent) return 0.0; int maxCommon = db1.NumCommonMantissaBits(db2); db1.ZeroLowerBits(64 - (12 + maxCommon)); return db1.Double; }
/// <summary> /// /// </summary> /// <param name="d"></param> /// <returns></returns> public static string ToBinaryString(double d) { DoubleBits db = new DoubleBits(d); return db.ToString(); }
/// <summary> /// /// </summary> /// <param name="d"></param> /// <returns></returns> public static int GetExponent(double d) { DoubleBits db = new DoubleBits(d); return db.Exponent; }
/// <summary> /// /// </summary> /// <param name="d"></param> /// <returns></returns> public static double TruncateToPowerOfTwo(double d) { DoubleBits db = new DoubleBits(d); db.ZeroLowerBits(52); return db.Double; }
/// <summary> /// This computes the number of common most-significant bits in the mantissa. /// It does not count the hidden bit, which is always 1. /// It does not determine whether the numbers have the same exponent - if they do /// not, the value computed by this function is meaningless. /// </summary> /// <param name="db"></param> /// <returns> The number of common most-significant mantissa bits.</returns> public virtual int NumCommonMantissaBits(DoubleBits db) { for (int i = 0; i < 52; i++) { if (GetBit(i) != db.GetBit(i)) return i; } return 52; }