| 1 | /** |
| 2 | * @license Fraction.js v2.7.0 01/06/2015 |
| 3 | * http://www.xarg.org/2014/03/rational-numbers-in-javascript/ |
| 4 | * |
| 5 | * Copyright (c) 2015, Robert Eisele ([email protected]) |
| 6 | * Dual licensed under the MIT or GPL Version 2 licenses. |
| 7 | **/ |
| 8 | function closestTapeMeasure(frac) { |
| 9 | |
| 10 | /* |
| 11 | k/16 ≤ a/b < (k+1)/16 |
| 12 | ⇔ k ≤ 16*a/b < (k+1) |
| 13 | ⇔ k = floor(16*a/b) |
| 14 | */ |
| 15 | return new Fraction(Math.round(16 * Fraction(frac).valueOf()), 16); |
| 16 | } |
| 17 | console.log(closestTapeMeasure("1/3")); // 5/16 |