Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix for precision argument #5

Closed
wants to merge 3 commits into from

2 participants

@pixelfreak

Here you go. This is for Issue #2. I added an argument called isNumInt because now prec is being used differently internally and externally.

I ran the test suite you have and the only thing that failed is the build#precision test, which makes sense because the behavior has changed. The test is updated with the isNumInt set to true.

Please review. Thanks!

@defunctzombie

Not sure I like adding an isNumInt argument, I feel like the old (from int) api can maybe go away.

Also, when making patches, please follow the coding style of the project :)

I will review the changes and think of the isNumInt argument. My initial thoughts are that it isn't needed and we can just stick with the precision.

@pixelfreak

What do you mean by stick with the precision?

@defunctzombie
@pixelfreak

I see. Well, let me know when you have decided. Meanwhile, I'll go with what I have for now. Thanks!

@defunctzombie

214d0b4

So it seems the main reason the precision/int combination exists is for internal use. I am thinking that a separate method could exist num.fromInt or some such that would have a decimal offset field.

@pixelfreak

Sweet! Works for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 19 deletions.
  1. +40 −15 num.js
  2. +3 −4 test/build.js
View
55 num.js
@@ -1,8 +1,8 @@
var int = require('int');
-function Num(num, prec) {
+function Num(num, prec, isNumInt) {
if (!(this instanceof Num)) {
- return new Num(num, prec);
+ return new Num(num, prec, isNumInt);
}
var self = this;
@@ -15,20 +15,45 @@ function Num(num, prec) {
// convert to a string
num = '' + num;
+ var currentPrecision = 0;
+
// find Num point
var dec = num.indexOf('.');
+ num = num.replace('.', '');
+
+ if (dec >= 0)
+ {
+ currentPrecision = num.length - dec;
+ }
- if (dec >= 0) {
- // take out the Num point
- num = num.replace('.', '');
- var precision = num.length - dec;
+ if (isNaN(prec))
+ {
+ this._precision = currentPrecision;
}
- else {
- var precision = 0;
+ else
+ {
+ // Pad with zero as needed if it's not an internal int
+ if (isNumInt !== true)
+ {
+ if (prec > currentPrecision)
+ {
+ var zeroWidth = prec - currentPrecision + 1;
+ if (zeroWidth > 1)
+ {
+ num = num + new Array(zeroWidth).join('0');
+ }
+ }
+ else
+ {
+ // Trim
+ num = num.substr(0, num.length - (currentPrecision - prec));
+ }
+ }
+
+ this._precision = prec;
}
this._int = int(num);
- this._precision = prec || precision;
}
// TODO (shtylman) cleanup
@@ -120,12 +145,12 @@ Num.prototype.round = function(precision) {
/// returns new Num, -this
Num.prototype.neg = function() {
- return new Num(this._int.neg(), this._precision);
+ return new Num(this._int.neg(), this._precision, true);
};
/// returns new Num, absolute value of this
Num.prototype.abs = function() {
- return new Num(this._int.abs(), this._precision);
+ return new Num(this._int.abs(), this._precision, true);
};
/// returns a + b
@@ -144,7 +169,7 @@ Num.add = function(a, b) {
// the integer result
var num_res = a._int.add(b._int);
- return new Num(num_res, precision);
+ return new Num(num_res, precision, true);
};
/// returns a - b
@@ -162,7 +187,7 @@ Num.sub = function(a, b) {
Num.mul = function(a, b) {
a = ensure_num(a);
b = ensure_num(b);
- return new Num(a._int.mul(b._int), a._precision + b._precision);
+ return new Num(a._int.mul(b._int), a._precision + b._precision, true);
};
Num.div = function(a, b) {
@@ -176,7 +201,7 @@ Num.div = function(a, b) {
a_int = a_int.mul(10);
}
- return Num(a_int.div(b._int), a._precision);
+ return Num(a_int.div(b._int), a._precision, true);
};
Num.mod = function(a, b) {
@@ -198,7 +223,7 @@ Num.mod = function(a, b) {
prec_b += 1;
}
- return Num(a.mod(b), prec_a);
+ return Num(a.mod(b), prec_a, true);
};
/// returns < 0 if a < b, 0 if a == b, > 0 if a > b
View
7 test/build.js
@@ -1,4 +1,3 @@
-
var assert = require('assert');
var num = require('../');
@@ -36,8 +35,8 @@ test('build', function() {
});
test('build#precision', function() {
- assert.equal(num(15, 1), '1.5');
- assert.equal(num(1234567890, 5), '12345.67890');
- assert.equal(num(-122, 4), '-0.0122');
+ assert.equal(num(15, 1, true), '1.5');
+ assert.equal(num(1234567890, 5, true), '12345.67890');
+ assert.equal(num(-122, 4, true), '-0.0122');
});
Something went wrong with that request. Please try again.