Skip to content

Commit

Permalink
feat(math): add simplifyRatio()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 28, 2019
1 parent 226645f commit 31e369b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/math/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export * from "./interval";
export * from "./min-error";
export * from "./mix";
export * from "./prec";
export * from "./ratio";
export * from "./solve";
export * from "./step";
18 changes: 18 additions & 0 deletions packages/math/src/ratio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const simplifyRatio =
(num: number, denom: number) => {
let e1 = Math.abs(num);
let e2 = Math.abs(denom);
while (true) {
if (e1 < e2) {
const t = e1;
e1 = e2;
e2 = t;
}
const r = e1 % e2;
if (r) {
e1 = r;
} else {
return [num / e2, denom / e2];
}
}
};

0 comments on commit 31e369b

Please sign in to comment.