static bool NonOverlaps(this double x, double y) { // Check non-number flags if (!x.IsNumber()) { throw new ArgumentOutOfRangeException("x"); } if (!y.IsNumber()) { throw new ArgumentOutOfRangeException("y"); } // Special case for 0: // Shewchuk p3: "The number zero does not overlap any number." if (x == 0.0 || y == 0.0) { return(true); } // Break into components var xc = new DoubleComponents(x); // We now have that x = xc.Mantissa * 2 ^ xc.Exponent // Normalize - this writes x = r.2^s with the maximal exponent s xc.MaximizeExponent(); // now compare abs of y with 2^s return((double)Math.Abs(y) < (double)(Math.Pow(2.0, xc.Exponent))); }
public static bool IsPowerOfTwo(this double d) { var dc = new DoubleComponents(d); dc.MaximizeExponent(); return(dc.Mantissa == 1); }
/// <summary> /// Returns a random double from the full range of possible double values. /// The sign, mantissa and the exponent are independent random numbers, /// thus the distribution of numbers is not uniform. /// </summary> /// <returns></returns> public double NextDoubleFullRange() { long mantissa = NextLong(52); int exponent = _random.Next(-1023-52, 1024-52); int sign = NextBool() ? -1 : +1; // Gets us -1 or +1. double result = sign * (double)mantissa * (double)Math.Pow(2.0, exponent); var dc = new DoubleComponents(result); Debug.Assert(result == dc.CalcValue()); return result; }
/// <summary> /// Returns a random double from the full range of possible double values. /// The sign, mantissa and the exponent are independent random numbers, /// thus the distribution of numbers is not uniform. /// </summary> /// <returns></returns> public double NextDoubleFullRange() { long mantissa = NextLong(52); int exponent = _random.Next(-1023 - 52, 1024 - 52); int sign = NextBool() ? -1 : +1; // Gets us -1 or +1. double result = sign * (double)mantissa * (double)Math.Pow(2.0, exponent); var dc = new DoubleComponents(result); Debug.Assert(result == dc.CalcValue()); return(result); }
/// <summary> /// Formats a double as a floating point binary string, e.g. 0.25 -> 0.01 /// </summary> /// <param name="d"></param> /// <returns></returns> /// <example>4 = 1.2^2 -> 100 /// 0.25 = 1.2^-2 -> 0.01 /// </example> public static string ToFloatingPointBinaryString(this double d) { var dc = new DoubleComponents(d); dc.MaximizeExponent(); var revChars = new List <char>(); while (dc.Exponent < 0) { revChars.Add(((dc.Mantissa & 1L) == 1L) ? '1' : '0'); dc.Exponent++; dc.Mantissa >>= 1; } revChars.Add('.'); // Write out 0s as long as the exponent suggests while (dc.Exponent > 0) { revChars.Add('0'); dc.Exponent--; } // write out the rest of the Mantissa while (dc.Mantissa != 0L) { revChars.Add(((dc.Mantissa & 1L) == 1L) ? '1' : '0'); dc.Mantissa >>= 1; } // Before we reverse - add a '0' to the end (which will go in front) if needed if (revChars.Last() == '.') { revChars.Add('0'); } // Prepend the sign (by adding before we reverse) if (dc.Negative) { revChars.Add('-'); } revChars.Reverse(); // Now a '0' after the '.' if needed (alternative would be to strip to '.') if (revChars.Last() == '.') { revChars.Add('0'); } return(new string(revChars.ToArray())); }
// Returns a double in the range where we expect the robust arithmetic to be valid // - exponents between -142 and 201. (According to S. p.3) public double NextDoubleValidRange() { double result = double.NaN; while (!(result.IsNumber() && result.IsInRange())) { long mantissa = NextLong(52); int exponent = _random.Next(-1023 - 52, 1024 - 52); int sign = NextBool() ? -1 : +1; // Gets us -1 or +1. result = sign * (double)mantissa * (double)Math.Pow(2.0, exponent); var dc = new DoubleComponents(result); Debug.Assert(result == dc.CalcValue()); } return result; }
// Returns a double in the range where we expect the robust arithmetic to be valid // - exponents between -142 and 201. (According to S. p.3) public double NextDoubleValidRange() { double result = double.NaN; while (!(result.IsNumber() && result.IsInRange())) { long mantissa = NextLong(52); int exponent = _random.Next(-1023 - 52, 1024 - 52); int sign = NextBool() ? -1 : +1; // Gets us -1 or +1. result = sign * (double)mantissa * (double)Math.Pow(2.0, exponent); var dc = new DoubleComponents(result); Debug.Assert(result == dc.CalcValue()); } return(result); }
static bool NonOverlaps(this double x, double y) { // Check non-number flags if (!x.IsNumber()) throw new ArgumentOutOfRangeException("x"); if (!y.IsNumber()) throw new ArgumentOutOfRangeException("y"); // Special case for 0: // Shewchuk p3: "The number zero does not overlap any number." if (x == 0.0 || y == 0.0) return true; // Break into components var xc = new DoubleComponents(x); // We now have that x = xc.Mantissa * 2 ^ xc.Exponent // Normalize - this writes x = r.2^s with the maximal exponent s xc.MaximizeExponent(); // now compare abs of y with 2^s return (double)Math.Abs(y) < (double)(Math.Pow(2.0, xc.Exponent)); }
public static bool IsPowerOfTwo(this double d) { var dc = new DoubleComponents(d); dc.MaximizeExponent(); return dc.Mantissa == 1; }
/// <summary> /// Formats a double as a floating point binary string, e.g. 0.25 -> 0.01 /// </summary> /// <param name="d"></param> /// <returns></returns> /// <example>4 = 1.2^2 -> 100 /// 0.25 = 1.2^-2 -> 0.01 /// </example> public static string ToFloatingPointBinaryString(this double d) { var dc = new DoubleComponents(d); dc.MaximizeExponent(); var revChars = new List<char>(); while (dc.Exponent < 0) { revChars.Add(((dc.Mantissa & 1L) == 1L) ? '1' : '0'); dc.Exponent++; dc.Mantissa >>= 1; } revChars.Add('.'); // Write out 0s as long as the exponent suggests while (dc.Exponent > 0) { revChars.Add('0'); dc.Exponent--; } // write out the rest of the Mantissa while (dc.Mantissa != 0L) { revChars.Add( ((dc.Mantissa & 1L) == 1L) ? '1' : '0'); dc.Mantissa >>= 1; } // Before we reverse - add a '0' to the end (which will go in front) if needed if (revChars.Last() == '.') revChars.Add('0'); // Prepend the sign (by adding before we reverse) if (dc.Negative) revChars.Add('-'); revChars.Reverse(); // Now a '0' after the '.' if needed (alternative would be to strip to '.') if (revChars.Last() == '.') revChars.Add('0'); return new string(revChars.ToArray()); }