Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #236 Large quotient

Initialize a quotient q = a/b of arbitrary precision. a and b are large integers.

q = Rational(a, b)
import "math/big"
q := new(big.Rat)
q.SetString(str)
import "math/big"
q := new(big.Rat)
q.SetFrac(a, b)
import "math/big"
q := big.NewRat(a, b)
import java.math.BigDecimal;
BigDecimal A = new BigDecimal(a),
           B = new BigDecimal(b),
           q = A.divide(B);
use v5.10;
use Math::BigRat;
my $a = Math::BigRat->new(  '1_000_000_000_000_000_000' );
my $b = Math::BigRat->new( '10_000_000_000_000_000_000' );
my $q = $a / $b;

say $q;  # prints 1/10
import fractions
q = fractions.Fraction(a, b)

New implementation...