Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Merge branch 'pr/117' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwdraper committed Dec 25, 2013
2 parents 7999ada + 86715fc commit 7668580
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 21 deletions.
116 changes: 105 additions & 11 deletions numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,89 @@
languages[key] = values;
}

/************************************
Floating-point helpers
************************************/

// The floating-point helper functions and implementation
// borrows heavily from sinful.js: http://guipn.github.io/sinful.js/

/**
* Array.prototype.reduce for browsers that don't support it
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Compatibility
*/
if ('function' !== typeof Array.prototype.reduce) {
Array.prototype.reduce = function (callback, opt_initialValue) {
'use strict';

if (null === this || 'undefined' === typeof this) {
// At the moment all modern browsers, that support strict mode, have
// native implementation of Array.prototype.reduce. For instance, IE8
// does not support strict mode, so this check is actually useless.
throw new TypeError('Array.prototype.reduce called on null or undefined');
}

if ('function' !== typeof callback) {
throw new TypeError(callback + ' is not a function');
}

var index,
value,
length = this.length >>> 0,
isValueSet = false;

if (1 < arguments.length) {
value = opt_initialValue;
isValueSet = true;
}

for (index = 0; length > index; ++index) {
if (this.hasOwnProperty(index)) {
if (isValueSet) {
value = callback(value, this[index], index, this);
} else {
value = this[index];
isValueSet = true;
}
}
}

if (!isValueSet) {
throw new TypeError('Reduce of empty array with no initial value');
}

return value;
};
}


/**
* Computes the multiplier necessary to make x >= 1,
* effectively eliminating miscalculations caused by
* finite precision.
*/
function multiplier(x) {
var parts = x.toString().split('.');
if (parts.length < 2) {
return 1;
}
return Math.pow(10, parts[1].length);
}

/**
* Given a variable number of arguments, returns the maximum
* multiplier that must be used to normalize an operation involving
* all of them.
*/
function correctionFactor() {
var args = Array.prototype.slice.call(arguments);
return args.reduce(function (prev, next) {
var mp = multiplier(prev),
mn = multiplier(next);
return mp > mn ? mp : mn;
}, -Infinity);
}


/************************************
Numeral Prototype
Expand Down Expand Up @@ -508,33 +591,44 @@
},

add : function (value) {
this._value = this._value + Number(value);
var corrFactor = correctionFactor.call(null, this._value, value);
function cback(accum, curr, currI, O) {
return accum + corrFactor * curr;
}
this._value = [this._value, value].reduce(cback, 0) / corrFactor;
return this;
},

subtract : function (value) {
this._value = this._value - Number(value);
var corrFactor = correctionFactor.call(null, this._value, value);
function cback(accum, curr, currI, O) {
return accum - corrFactor * curr;
}
this._value = [value].reduce(cback, this._value * corrFactor) / corrFactor;
return this;
},

multiply : function (value) {
this._value = this._value * Number(value);
function cback(accum, curr, currI, O) {
var corrFactor = correctionFactor(accum, curr);
return (accum * corrFactor) * (curr * corrFactor) /
(corrFactor * corrFactor);
}
this._value = [this._value, value].reduce(cback, 1);
return this;
},

divide : function (value) {
this._value = this._value / Number(value);
function cback(accum, curr, currI, O) {
var corrFactor = correctionFactor(accum, curr);
return (accum * corrFactor) / (curr * corrFactor);
}
this._value = [this._value, value].reduce(cback);
return this;
},

difference : function (value) {
var difference = this._value - Number(value);

if (difference < 0) {
difference = -difference;
}

return difference;
return Math.abs(numeral(this._value).subtract(value).value());
}

};
Expand Down
25 changes: 15 additions & 10 deletions tests/numeral/manipulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ var numeral = require('../../numeral');
exports.manipulate = {

add: function (test) {
test.expect(3);
test.expect(4);

var tests = [
[1000,10,1010],
[0.5,3,3.5],
[-100,200,100]
[-100,200,100],
[0.1,0.2,0.3]
],
num;

Expand All @@ -22,12 +23,13 @@ exports.manipulate = {
},

subtract: function (test) {
test.expect(3);
test.expect(4);

var tests = [
[1000,10,990],
[0.5,3,-2.5],
[-100,200,-300]
[-100,200,-300],
[0.3,0.1,0.2]
],
num;

Expand All @@ -41,12 +43,13 @@ exports.manipulate = {
},

multiply: function (test) {
test.expect(3);
test.expect(4);

var tests = [
[1000,10,10000],
[0.5,3,1.5],
[-100,200,-20000]
[-100,200,-20000],
[0.1,0.2,0.02]
],
num;

Expand All @@ -60,12 +63,13 @@ exports.manipulate = {
},

divide: function (test) {
test.expect(3);
test.expect(4);

var tests = [
[1000,10,100],
[0.5,3,0.16666666666666666],
[-100,200,-0.5]
[-100,200,-0.5],
[5.3,0.1,53]
],
num;

Expand All @@ -79,12 +83,13 @@ exports.manipulate = {
},

difference: function (test) {
test.expect(3);
test.expect(4);

var tests = [
[1000,10,990],
[0.5,3,2.5],
[-100,200,300]
[-100,200,300],
[0.3,0.2,0.1]
],
num;

Expand Down

0 comments on commit 7668580

Please sign in to comment.