public void ReduceOnBigIntReturnsLargerValues() { BigInteger b1 = new BigInteger("100000000000000000000", 16); BigInteger b2 = b1.negate(); BigInteger b3 = new BigInteger("123456789012345678901234567890"); BigInteger b4 = b3.negate(); ExpectSameObject(b1, Numbers.reduce(b1)); ExpectSameObject(b2, Numbers.reduce(b2)); ExpectSameObject(b3, Numbers.reduce(b3)); ExpectSameObject(b4, Numbers.reduce(b4)); }
public static object BIDivide(BigInteger n, BigInteger d) { if (d.Equals(BigIntegerZero)) throw new ArithmeticException("Divide by zero"); BigInteger gcd = n.gcd(d); if (gcd.Equals(BigIntegerZero)) return 0; n = n.divide(gcd); d = d.divide(gcd); if (d.Equals(BigIntegerOne)) return reduce(n); return new Ratio((d.signum() < 0 ? n.negate() : n), (d.signum() < 0 ? d.negate() : d)); }
public static object matchNumber(string s) { Match m = intRE.Match(s); if ( m.Success ) { if ( m.Groups[2].Success ) // matched 0 only return 0; bool isNeg = m.Groups[1].Value == "-"; string n = null; int radix = 10; if (m.Groups[3].Success) { n = m.Groups[3].Value; radix = 10; } else if (m.Groups[4].Success) { n = m.Groups[4].Value; radix = 16; } else if (m.Groups[5].Success) { n = m.Groups[5].Value; radix = 8; } else if (m.Groups[7].Success) { n = m.Groups[7].Value; radix = Int32.Parse(m.Groups[6].Value); } if (n == null) return null; BigInteger bn = new BigInteger(n, radix); return Numbers.reduce(isNeg ? bn.negate() : bn); } m = floatRE.Match(s); if (m.Success) { return ( s[s.Length-1] == 'M' ) ? new BigDecimal( s.Substring(0,s.Length-1)) // TODO: Fix MS inadequacy : (object)Double.Parse(s); } m = ratioRE.Match(s); if (m.Success) { // There is a bug in the BigInteger c-tor that causes it barf on a leading +. string numerString = m.Groups[1].Value; string denomString = m.Groups[2].Value; if (numerString[0] == '+') numerString = numerString.Substring(1); return Numbers.BIDivide(new BigInteger(numerString), new BigInteger(denomString)); } return null; }