Skip to content

Commit

Permalink
add abs function
Browse files Browse the repository at this point in the history
Returns the absolute value of the number as a new number.

fixes #1
  • Loading branch information
defunctzombie committed Nov 10, 2012
1 parent b417d22 commit 5a3cfbc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -15,7 +15,7 @@ npm install num
```javascript
var num = require('num');

var foo = int('0.1').add('0.2');
var foo = num('0.1').add('0.2');

// did it work?
console.log(foo.toString());
Expand All @@ -24,7 +24,7 @@ console.log(foo.toString());

## api ##

Besides the **num** function, all of the other methods operate on the objects returned by **int**
Besides the **num** function, all of the other methods operate on the objects returned by **num**

### num (value) ###
> construct a new decimal
Expand All @@ -44,10 +44,13 @@ Besides the **num** function, all of the other methods operate on the objects re
> divide our num by {value} and return a new num
### neg ###
> return a new int that is the negative
> return a new num that is the negative
### abs ###
> return a new int that is the absolute value
> return new num that is the absolute value
### abs ###
> return a new num that is the absolute value
### cmp (value) ###
> compare our value to {value}
Expand Down
5 changes: 5 additions & 0 deletions num.js
Expand Up @@ -123,6 +123,11 @@ Num.prototype.neg = function() {
return new Num(this._int.neg(), this._precision);
};

/// returns new Num, absolute value of this
Num.prototype.abs = function() {
return new Num(this._int.abs(), this._precision);
};

/// returns a + b
/// a, b can each be either a Num, String, or Number
/// will return a new Num with the greatest precision of the operands
Expand Down
12 changes: 12 additions & 0 deletions test/abs.js
@@ -0,0 +1,12 @@

var assert = require('assert');
var num = require('../');

test('abs', function() {
assert.equal(num(-1).abs(), '1');
assert.equal(num(0).sub(3).abs(), '3');

assert.equal(num(44).abs(), '44');
assert.equal(num(-2).abs().add(2), '4');
});

0 comments on commit 5a3cfbc

Please sign in to comment.