export class Rational {
constructor(num, den) {
if (den < 0) {
num = -num;
den = -den;
}
const common = this.gcd(Math.abs(num), Math.abs(den));
this.num = num / common;
this.den = den / common;
}
gcd(a, b) {
return b === 0 ? a : this.gcd(b, a % b);
}
add(other) {
return new Rational(this.num * other.den + other.num * this.den, this.den * other.den);
}
sub(other) {
return new Rational(this.num * other.den - other.num * this.den, this.den * other.den);
}
mul(other) {
return new Rational(this.num * other.num, this.den * other.den);
}
div(other) {
return new Rational(this.num * other.den, other.num * this.den);
}
abs() {
return new Rational(Math.abs(this.num), Math.abs(this.den));
}
exprational(n) {
if (n >= 0) {
return new Rational(Math.pow(this.num, n), Math.pow(this.den, n));
} else {
const m = Math.abs(n);
return new Rational(Math.pow(this.den, m), Math.pow(this.num, m));
}
}
expreal(x) {
return Math.pow(Math.pow(this.num, x) / Math.pow(this.den, x), 1);
}
reduce() {
return this;
}
}