/** * Reduces this rational number to the smallest numerator/denominator with the same value. * * @return the reduced rational number */ public BigRational reduce() { BigInteger n = numerator.toBigInteger(); BigInteger d = denominator.toBigInteger(); BigInteger gcd = n.gcd(d); n = n.divide(gcd); d = d.divide(gcd); return(valueOf(n, d)); }
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)); }
// $ANTLR end "stat" // $ANTLR start "expr" // BuildOptions\\ProfileTreeGrammar.g3:63:0: expr returns [BigInteger value] : ( ^( '+' a= expr b= expr ) | ^( '-' a= expr b= expr ) | ^( '*' a= expr b= expr ) | ^( '/' a= expr b= expr ) | ^( '%' a= expr b= expr ) | ID | INT | call ); private BigInteger expr( ) { BigInteger value = default(BigInteger); CommonTree ID4 = null; CommonTree INT5 = null; BigInteger a = default(BigInteger); BigInteger b = default(BigInteger); BigInteger call6 = default(BigInteger); try { dbg.EnterRule(GrammarFileName, "expr"); if (RuleLevel == 0) { dbg.Commence(); } IncRuleLevel(); dbg.Location(63, -1); try { // BuildOptions\\ProfileTreeGrammar.g3:64:9: ( ^( '+' a= expr b= expr ) | ^( '-' a= expr b= expr ) | ^( '*' a= expr b= expr ) | ^( '/' a= expr b= expr ) | ^( '%' a= expr b= expr ) | ID | INT | call ) int alt4 = 8; try { dbg.EnterDecision(4); switch (input.LA(1)) { case 16: { alt4 = 1; } break; case 10: { alt4 = 2; } break; case 14: { alt4 = 3; } break; case 15: { alt4 = 4; } break; case 11: { alt4 = 5; } break; case ID: { alt4 = 6; } break; case INT: { alt4 = 7; } break; case CALL: { alt4 = 8; } break; default: { NoViableAltException nvae = new NoViableAltException("", 4, 0, input); dbg.RecognitionException(nvae); throw nvae; } } } finally { dbg.ExitDecision(4); } switch (alt4) { case 1: dbg.EnterAlt(1); // BuildOptions\\ProfileTreeGrammar.g3:64:9: ^( '+' a= expr b= expr ) { dbg.Location(64, 8); dbg.Location(64, 10); Match(input, 16, Follow._16_in_expr172); Match(input, TokenTypes.Down, null); dbg.Location(64, 15); PushFollow(Follow._expr_in_expr176); a = expr(); state._fsp--; dbg.Location(64, 22); PushFollow(Follow._expr_in_expr180); b = expr(); state._fsp--; Match(input, TokenTypes.Up, null); dbg.Location(64, 35); value = a.add(b); } break; case 2: dbg.EnterAlt(2); // BuildOptions\\ProfileTreeGrammar.g3:65:9: ^( '-' a= expr b= expr ) { dbg.Location(65, 8); dbg.Location(65, 10); Match(input, 10, Follow._10_in_expr200); Match(input, TokenTypes.Down, null); dbg.Location(65, 15); PushFollow(Follow._expr_in_expr204); a = expr(); state._fsp--; dbg.Location(65, 22); PushFollow(Follow._expr_in_expr208); b = expr(); state._fsp--; Match(input, TokenTypes.Up, null); dbg.Location(65, 35); value = a.subtract(b); } break; case 3: dbg.EnterAlt(3); // BuildOptions\\ProfileTreeGrammar.g3:66:9: ^( '*' a= expr b= expr ) { dbg.Location(66, 8); dbg.Location(66, 10); Match(input, 14, Follow._14_in_expr228); Match(input, TokenTypes.Down, null); dbg.Location(66, 15); PushFollow(Follow._expr_in_expr232); a = expr(); state._fsp--; dbg.Location(66, 22); PushFollow(Follow._expr_in_expr236); b = expr(); state._fsp--; Match(input, TokenTypes.Up, null); dbg.Location(66, 35); value = a.multiply(b); } break; case 4: dbg.EnterAlt(4); // BuildOptions\\ProfileTreeGrammar.g3:67:9: ^( '/' a= expr b= expr ) { dbg.Location(67, 8); dbg.Location(67, 10); Match(input, 15, Follow._15_in_expr256); Match(input, TokenTypes.Down, null); dbg.Location(67, 15); PushFollow(Follow._expr_in_expr260); a = expr(); state._fsp--; dbg.Location(67, 22); PushFollow(Follow._expr_in_expr264); b = expr(); state._fsp--; Match(input, TokenTypes.Up, null); dbg.Location(67, 35); value = a.divide(b); } break; case 5: dbg.EnterAlt(5); // BuildOptions\\ProfileTreeGrammar.g3:68:9: ^( '%' a= expr b= expr ) { dbg.Location(68, 8); dbg.Location(68, 10); Match(input, 11, Follow._11_in_expr284); Match(input, TokenTypes.Down, null); dbg.Location(68, 15); PushFollow(Follow._expr_in_expr288); a = expr(); state._fsp--; dbg.Location(68, 22); PushFollow(Follow._expr_in_expr292); b = expr(); state._fsp--; Match(input, TokenTypes.Up, null); dbg.Location(68, 35); value = a.remainder(b); } break; case 6: dbg.EnterAlt(6); // BuildOptions\\ProfileTreeGrammar.g3:69:9: ID { dbg.Location(69, 8); ID4 = (CommonTree)Match(input, ID, Follow._ID_in_expr311); dbg.Location(69, 35); value = getValue((ID4 != null?ID4.Text:null)); } break; case 7: dbg.EnterAlt(7); // BuildOptions\\ProfileTreeGrammar.g3:70:9: INT { dbg.Location(70, 8); INT5 = (CommonTree)Match(input, INT, Follow._INT_in_expr347); dbg.Location(70, 35); value = new BigInteger((INT5 != null?INT5.Text:null)); } break; case 8: dbg.EnterAlt(8); // BuildOptions\\ProfileTreeGrammar.g3:71:9: call { dbg.Location(71, 8); PushFollow(Follow._call_in_expr382); call6 = call(); state._fsp--; dbg.Location(71, 35); value = call6; } break; } } catch (RecognitionException re) { ReportError(re); Recover(input, re); } finally { } dbg.Location(72, 4); } finally { dbg.ExitRule(GrammarFileName, "expr"); DecRuleLevel(); if (RuleLevel == 0) { dbg.Terminate(); } } return(value); }