Arbitrary precision rational number arithmetic
var rat = require('big-rat')
//Construct a pair of rational numbers;
// a = 1/10
// b = 2/10
var a = rat(1, 10)
var b = rat(2, 10)
//Compute their sum
var add = require('big-rat/add')
var c = add(a, b)
//Print out sum
var toString = require('big-rat/to-string')
console.log('a+b=', toString(c))
//And also convert to a number
var toFloat = require('big-rat/to-float')
console.log('exact rational result:', toFloat(c))
//For comparison, here is the same computation performed with floats
var x = 0.1
var y = 0.2
console.log('approximate float result:', x + y)
a+b= 3/10
exact rational result: 0.3
approximate float result: 0.30000000000000004
npm i big-rat
Constructs a rational number as the quotient n/d
n
is the numerator. Can be a float, string, bignum or rationald
is the denominator (optional, default1
)
Returns A rational number
Returns The closest floating point number to r
Returns A string representing the big rat r
Returns true
if r
is a big rat
Returns a+b
Returns a-b
Returns a*b
Returns a/b
Returns -a
Returns 1/a
Returns One of the following values:
-1
ifa<0
0
ifa=0
+1
ifa>0
Returns |a|
Returns min(a,b)
Returns max(a,b)
Returns true
if a=b
, false
otherwise
Returns
-1
ifa<b
0
ifa=b
+1
ifa>b
(c) 2015 MIT License