npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rationals

v0.2.3

Published

rational numbers with operations

Downloads

163

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

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