Permalink
Browse files

fixing expansion of toRepeatedDecimal

  • Loading branch information...
1 parent ef7a318 commit 4d2f4d1205fa2118200d1248e40655867dfc70e8 Danny Yoo committed Dec 10, 2010
Showing with 51 additions and 6 deletions.
  1. +6 −2 README
  2. +18 −4 src/js-numbers.js
  3. +27 −0 test/tests.js
View
8 README
@@ -268,9 +268,13 @@ lcm: scheme-number [scheme-number ...] -> scheme-number
Produces the least common mulitple.
+toRepeatedDecimal: scheme-number scheme-number {limit: number}? -> [string, string, string]
+ Produces a string representation of the decimal expansion; the first and second
+ argument must be integers. Returns an array of three parts: the portion before
+ the decimal, the non-repeating part, and then the repeating part.
-
-
+ If the expansion goes beyond the limit (by default, 256 characters), then
+ the expansion will be cut off, and the third portion will be '...'.
======================================================================
View
@@ -3941,19 +3941,28 @@ if (typeof(exports) !== 'undefined') {
//////////////////////////////////////////////////////////////////////
- // toRepeatingDecimal: jsnum jsnum -> [string, string, string]
+ // toRepeatingDecimal: jsnum jsnum {limit: number}? -> [string, string, string]
//
// Given the numerator and denominator parts of a rational,
// produces the repeating-decimal representation, where the first
// part are the digits before the decimal, the second are the
// non-repeating digits after the decimal, and the third are the
// remaining repeating decimals.
+ //
+ // An optional limit on the decimal expansion can be provided, in which
+ // case the search cuts off if we go past the limit.
+ // If this happens, the third argument returned becomes '...' to indicate
+ // that the search was prematurely cut off.
var toRepeatingDecimal = (function() {
- var getResidue = function(r, d) {
+ var getResidue = function(r, d, limit) {
var digits = [];
var seenRemainders = {};
seenRemainders[r] = true;
while(true) {
+ if (limit-- <= 0) {
+ return [digits.join(''), '...']
+ }
+
var nextDigit = quotient(
multiply(r, 10), d);
var nextRemainder = remainder(
@@ -3999,7 +4008,12 @@ if (typeof(exports) !== 'undefined') {
};
- return function(n, d) {
+ return function(n, d, options) {
+ // default limit on decimal expansion; can be overridden
+ var limit = 512;
+ if (options && typeof(options.limit) !== 'undefined') {
+ limit = options.limit;
+ }
if (! isInteger(n)) {
throwRuntimeError('toRepeatingDecimal: n ' + n.toString() +
" is not an integer.");
@@ -4017,7 +4031,7 @@ if (typeof(exports) !== 'undefined') {
var sign = (lessThan(n, 0) ? "-" : "");
n = abs(n);
var beforeDecimalPoint = sign + quotient(n, d);
- var afterDecimals = getResidue(remainder(n, d), d);
+ var afterDecimals = getResidue(remainder(n, d), d, limit);
return [beforeDecimalPoint].concat(afterDecimals);
};
})();
View
@@ -3232,7 +3232,34 @@ describe('repeating decimals', {
assertEquals(['0', '0625', '0'], toRepeatingDecimal(1, 16));
assertEquals(['0', '', '0588235294117647'], toRepeatingDecimal(1, 17));
assertEquals(['5', '8', '144'], toRepeatingDecimal(3227, 555));
+ },
+
+ limitRendering: function() {
+ var OPTIONS = {limit: 5};
+ assertEquals(['1', '', '0'], toRepeatingDecimal(1, 1, OPTIONS));
+ assertEquals(['0', '5', '0'], toRepeatingDecimal(1, 2, OPTIONS));
+ assertEquals(['0', '', '3'], toRepeatingDecimal(1, 3, OPTIONS));
+ assertEquals(['0', '25', '0'], toRepeatingDecimal(1, 4, OPTIONS));
+ assertEquals(['0', '2', '0'], toRepeatingDecimal(1, 5, OPTIONS));
+ assertEquals(['0', '1', '6'], toRepeatingDecimal(1, 6, OPTIONS));
+ assertEquals(['0', '05882', '...'],
+ toRepeatingDecimal(1, 17, {limit : 5}));
+
+ assertEquals(['0', '125', '0'], toRepeatingDecimal(1, 8, {limit : 4}));
+ assertEquals(['0', '125', '...'], toRepeatingDecimal(1, 8, {limit : 3}));
+ assertEquals(['0', '12', '...'], toRepeatingDecimal(1, 8, {limit : 2}));
+ assertEquals(['0', '1', '...'], toRepeatingDecimal(1, 8, {limit : 1}));
+ assertEquals(['0', '', '...'], toRepeatingDecimal(1, 8, {limit : 0}));
+
+ assertEquals(['10012718086',
+ '8577149703838870007766167',
+ '...'],
+ toRepeatingDecimal(makeBignum("239872983562893234879"),
+ makeBignum("23956829852"),
+ {limit:25}));
+
}
+
});

0 comments on commit 4d2f4d1

Please sign in to comment.