rationals
rational numbers with operations
Last updated 7 years ago by ashnur .
DAFUQ · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install rationals 
SYNC missed versions from official npm registry.

rationals

extended rational number type for javascript

Basically, each rational number is reduced to normal form, then memoized so referential equation works, each instance of any rational number will === with any other instance of the same rational number, even if they were constructed using different inputs.

Of course, if you run out of memory, you run out of memory, but you knew that when you decided to use BigInt.

I've got the inspiration for this lib from here: MF105: The extended rational numbers in practice

Support

browser support

Examples

import rat as R from rationals

// denominator of 1 is optional
R(1) === R(1); // true

R(1,1) === R(1); // true

// all rationals will be always reduced
R(2,4) === R(13,26); // true

R(100,50) === R(2); // true

// my personal favorite aproximation of Pi, from ancient china
R(355,113); // 3.1415929203539825

// you can give floats and they will be converted into rationals
fromNumber(0.4,0.1) == rats(4);

API

  • a & b are objects created with the rationals() function
  • in the parentheses you have some common aliases for the methods

Addition

`.add(a, b)`

Subtraction

`.sub(a, b)`

Multiplication

`.mul(a, b)`

Division

`.div(a, b)`

Examining

  • toString

    Examining an object can be hard, but if you cast it to a string: r(355,113)+'' will return '355/113'.

    So r(625,125).toString() will return '5/1'.

Display

  • display

    Just like toString(), but the numerator will be shown only if it's not 1. That is, integers will appear without a slash symbol and a denominator.

    r(625,125).display() returns '5'.

Approximation

  • val (value)

    Will return the numerator divided with the denominator. Note that this casts bigints to ints so whatever can happen.

    r(355,113).val(); //3.1415929203539825

Ordering

  • compare (value)

    Will return -1, 0 or 1 if the rational is smaller, equal or larger than the rational it is compared to

    .compare(R(-999,605), R(272,835)) // -1 .compare(R(-966,743), R(-632,198)) // 1 .compare(R(-3,9), R(12,-36)) // 0

Compare absolute values

  • compareAbs (value)

    Same as compare but without signs.

    .compare(R(-999,605), R(272,835)) // 1

Install

npm install rationals

You can use it in the browser with browserify

Current Tags

  • 0.2.3                                ...           latest (a year ago)

23 Versions

  • 0.2.3                                ...           a year ago
  • 0.2.1                                ...           a year ago
  • 0.1.9                                ...           7 years ago
  • 0.1.8                                ...           7 years ago
  • 0.1.7                                ...           7 years ago
  • 0.1.6                                ...           7 years ago
  • 0.1.5                                ...           7 years ago
  • 0.1.4                                ...           7 years ago
  • 0.1.3                                ...           7 years ago
  • 0.1.2                                ...           7 years ago
  • 0.1.1                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
  • 0.0.14                                ...           7 years ago
  • 0.0.13                                ...           7 years ago
  • 0.0.12                                ...           7 years ago
  • 0.0.11                                ...           7 years ago
  • 0.0.9                                ...           7 years ago
  • 0.0.8                                ...           7 years ago
  • 0.0.5                                ...           7 years ago
  • 0.0.3                                ...           8 years ago
  • 0.0.2                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
  • 0.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (2)

Copyright 2014 - 2016 © taobao.org |