# rationals

> rational numbers with operations

Latest version **0.2.3** (published 2019-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install rationals
pnpm add rationals
yarn add rationals
bun add rationals
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2019-10-12 |
| First published | 2013-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | ashnur |
| Maintainers | ashnur |
| Keywords | rational numbers, rationals, rational arithmetic, dividing with zero, infinity, origo, numbers |

## Links

- npm: https://www.npmjs.com/package/rationals
- Repository: https://github.com/ashnur/rationals
- Homepage: https://github.com/ashnur/rationals#readme
- Issues: https://github.com/ashnur/rationals/issues
- npm.io page: https://npm.io/package/rationals

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.2.3 (latest) — 2019-10-12
- 0.2.1 — 2019-05-06
- 0.1.9 — 2013-09-26
- 0.1.8 — 2013-09-11
- 0.1.7 — 2013-09-11
- 0.1.6 — 2013-09-01
- 0.1.5 — 2013-07-16
- 0.1.4 — 2013-05-13
- 0.1.3 — 2013-05-13
- 0.1.2 — 2013-03-29
- 0.1.1 — 2013-03-29
- 0.1.0 — 2013-03-29
- 0.0.14 — 2013-03-26
- 0.0.13 — 2013-03-26
- 0.0.12 — 2013-03-25
- … 8 more at https://npm.io/package/rationals/versions

## README

# 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](http://www.youtube.com/watch?v=YMQkLojL2ek)

# Support
[![browser support](https://ci.testling.com/ashnur/rationals.png)](https://ci.testling.com/ashnur/rationals)




# 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](http://browserify.org/)**

---
_Source: https://npm.io/package/rationals · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
