Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 85 lines (55 sloc) 1.889 kB
bb61090 @defunctzombie run browser tests with zuul
authored
1 [![Build Status](https://secure.travis-ci.org/defunctzombie/num.svg)](http://travis-ci.org/defunctzombie/num)
813da80 @defunctzombie update README
authored
2
bb61090 @defunctzombie run browser tests with zuul
authored
3 **num** is an arbitrary size fixed precision library written in javascript for node.js and browsers
813da80 @defunctzombie update README
authored
4
6e31f02 @defunctzombie readme: fix link to `int` module
authored
5 Looking for just integers? Check out [int](https://github.com/defunctzombie/int)
813da80 @defunctzombie update README
authored
6
7 ```
8 npm install num
9 ```
10
11 ```javascript
12 var num = require('num');
13
bb61090 @defunctzombie run browser tests with zuul
authored
14 // ordinary js floating point math
15 console.log(0.1 + 0.2); // 0.30000000000000004 :(
813da80 @defunctzombie update README
authored
16
bb61090 @defunctzombie run browser tests with zuul
authored
17 // with `num`
18 console.log(num(0.1).add(0.2).toString()); //'0.3'
813da80 @defunctzombie update README
authored
19 ```
20
21 ## api ##
22
5a3cfbc @defunctzombie add `abs` function
authored
23 Besides the **num** function, all of the other methods operate on the objects returned by **num**
813da80 @defunctzombie update README
authored
24
25 ### num (value) ###
26 > construct a new decimal
27
28 > valid values are integers, numbers, or strings
29
30 ### add (value) ###
31 > add {value} to our number and return a new num
32
33 ### sub (value) ###
34 > subtract {value} from our number and return a new num
35
36 ### mul (value) ###
37 > multiply our num by {value} and return a new num
38
39 ### div (value) ###
40 > divide our num by {value} and return a new num
41
9263208 @defunctzombie add note about div precision
authored
42 ```
43 // note that `div` uses the precision of the numerator
44 num('1').div('3.0') -> 0
45 num('1.0').div('3') -> 0.3
46 ```
47
9ed0668 @defunctzombie update readme
authored
48 ### neg ###
5a3cfbc @defunctzombie add `abs` function
authored
49 > return a new num that is the negative
9ed0668 @defunctzombie update readme
authored
50
51 ### abs ###
5a3cfbc @defunctzombie add `abs` function
authored
52 > return new num that is the absolute value
53
54 ### abs ###
55 > return a new num that is the absolute value
9ed0668 @defunctzombie update readme
authored
56
57 ### cmp (value) ###
58 > compare our value to {value}
59
60 > return 0 if self and value are equal, -1 if self < value, 1 if self > value
61
62 ### lt (value) ###
63 > return true if self < value
64
65 ### lte (value) ###
66 > return true if self <= value
67
68 ### gt (value) ###
69 > return true if self > value
70
71 ### gte (value) ###
72 > return true if self >= value
73
74 ### eq (value) ###
75 > return true if self == value
76
77 ### ne (value) ###
78 > return true if self != value
a874a9a @defunctzombie document set_precision
authored
79
80 ### set_precision (precision) ###
81
82 > set the precision for the number. Can be used to alter how many places after the decimal are relevant.
83
84 > return self
Something went wrong with that request. Please try again.