Last active
December 11, 2015 00:59
-
-
Save kanryu/4520496 to your computer and use it in GitHub Desktop.
The Fraction of Math(in TypeScript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * The Fraction of Math | |
| * written by k.kanryu@gmail.com | |
| * MIT or 2-BSD Licensed. | |
| */ | |
| class Fraction { | |
| constructor(public numerator: number, | |
| public denominator: number) | |
| { | |
| this.euclid(); | |
| } | |
| public real() { return this.numerator/this.denominator; } | |
| public toString() { return String(this.numerator)+"/"+String(this.denominator); } | |
| private euclid() { | |
| this.euclid2(this.numerator, this.denominator); | |
| } | |
| private euclid2(a: number, b: number) { | |
| var c,d: number; | |
| if(a < b) { | |
| c = b; d = a; | |
| } else { | |
| var c = a % b; | |
| if(c == 0) { | |
| this.numerator /= b; | |
| this.denominator /= b; | |
| return; | |
| } | |
| if(c == 1) return; | |
| d = b; | |
| } | |
| this.euclid2(c, d); | |
| } | |
| // 1/2 * 1/3 -> 1/6 | |
| public static mul(lhs: Fraction, rhs: Fraction): Fraction; | |
| public static mul(lhs: Fraction, rhs: number): Fraction; | |
| public static mul(lhs: number, rhs: Fraction): Fraction; | |
| public static mul(lhs: any, rhs: any): Fraction { | |
| var llhs = typeof lhs == "number" ? new Fraction(lhs, 1) : lhs; | |
| var rrhs = typeof rhs == "number" ? new Fraction(rhs, 1) : rhs; | |
| var result = new Fraction(llhs.numerator * rrhs.numerator, llhs.denominator * rrhs.denominator); | |
| return result; | |
| } | |
| // (1/2) / (1/3) -> 3/2 | |
| public static div(lhs: Fraction, rhs: Fraction): Fraction; | |
| public static div(lhs: Fraction, rhs: number): Fraction; | |
| public static div(lhs: number, rhs: Fraction): Fraction; | |
| public static div(lhs: any, rhs: any): Fraction { | |
| var llhs = typeof lhs == "number" ? new Fraction(lhs, 1) : lhs; | |
| var rrhs = typeof rhs == "number" ? new Fraction(rhs, 1) : rhs; | |
| var result = new Fraction(llhs.numerator * rrhs.denominator, llhs.denominator * rrhs.numerator); | |
| return result; | |
| } | |
| // 1/2 + 1/3 -> 5/6 | |
| public static add(lhs: Fraction, rhs: Fraction): Fraction; | |
| public static add(lhs: Fraction, rhs: number): Fraction; | |
| public static add(lhs: number, rhs: Fraction): Fraction; | |
| public static add(lhs: any, rhs: any): Fraction { | |
| var llhs = typeof lhs == "number" ? new Fraction(lhs, 1) : lhs; | |
| var rrhs = typeof rhs == "number" ? new Fraction(rhs, 1) : rhs; | |
| var result = new Fraction( | |
| llhs.numerator*rrhs.denominator + llhs.denominator*rrhs.numerator, | |
| llhs.denominator * rrhs.denominator); | |
| return result; | |
| } | |
| // 1/2 - 1/3 -> 1/6 | |
| public static sub(lhs: Fraction, rhs: Fraction): Fraction; | |
| public static sub(lhs: Fraction, rhs: number): Fraction; | |
| public static sub(lhs: number, rhs: Fraction): Fraction; | |
| public static sub(lhs: any, rhs: any): Fraction { | |
| var llhs = typeof lhs == "number" ? new Fraction(lhs, 1) : lhs; | |
| var rrhs = typeof rhs == "number" ? new Fraction(rhs, 1) : rhs; | |
| var result = new Fraction( | |
| llhs.numerator*rrhs.denominator - llhs.denominator*rrhs.numerator, | |
| llhs.denominator * rrhs.denominator); | |
| return result; | |
| } | |
| } | |
| var f = new Fraction(3, 4); | |
| console.log(f.real()); | |
| var f2 = new Fraction(8, 12); | |
| //f2.euclid(); | |
| console.log("euclid", f2.toString()); | |
| var f3 = Fraction.mul(f, f2); | |
| console.log("mul", f3.toString()); | |
| var f4 = Fraction.mul(f, 5); | |
| console.log("mul", f4.toString()); | |
| var f5 = Fraction.div(f, f2); | |
| console.log("div", f5.toString()); | |
| var f6 = Fraction.add(f, f2); | |
| console.log("add", f6.toString()); | |
| //[output] | |
| //0.75 | |
| //euclid 2/3 | |
| //mul 1/2 | |
| //mul 15/4 | |
| //div 9/8 | |
| //add 17/12 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment