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

rational-number

v0.2.1

Published

Rational number object.

Downloads

30

Readme

rational-number

RationalNumber is an implementation for fractions in JavaScript consisting of two integers. Mathematical operations such as addition, subtraction, multiplication and division are provided. Division by zero is allowed and should produce correct mathematical results according to the use of Infinity and NaN in JavaScript.

Fractions are maintained by default in reduced form. For example, setting the value to 2/4 will automatically be reduced (simplified) to 1/2. However functions forcing non-reduced fractions are provided, particularly to avoid speed issues in intermediate calculations (although the current mathematical functions will reduce the fraction after every calculation to minimize the chance of overflow).

This implementation of rational numbers limits the numerator and denominator to the range from 0 to 2^53–1 plus a sign. Optional overflow detection is included, with the methods checkOverflowOn() and CheckOverflowOff() turning this feature on and off.

Using in node applications

The RationalNumber module can be installed globally for use in node with the command:

$ npm install -g rational-number

If you want only to use locally in a node project, then install as a package dependency with the command:

$ npm install --save rational-number

Here is an example of loading the module into a node script and adding two rational numbers together:

var RationalNumber = require('rational-number');
    
var rn1 = new RationalNumber(4, 5);
var rn2 = new RationalNumber('26/4');
var rn3 = rn1.add(rn2);
console.log(rn1.toString(),"+",rn2.toString(),"=",rn3.toString());
console.log(rn1.toStringMixed("_"),"+",rn2.toStringMixed("_"),
   "=",rn3.toStringMixed("_"));
console.log(rn1.valueOf()+" + "+rn2.valueOf()+" = "+rn3.valueOf());

The above code should output the following text to the console:

4/5 + 13/2 = 73/10
4/5 + 6_1/2 = 7_3/10
0.8 + 6.5 = 7.3

Using in web browsers

The JavaScript files for RationalNumber can also be used within a webpage by including these two files:

<script src="RationalNumber-base.js"></script>
<script src="RationalNumber-math.js"></script>

The first file (RationalNumber-base.js) is required, while the second one containing mathematical functions (add, subtract, etc.) is optional. The lib directory contains a makefile with example commands to generate minified versions of the programs files. Visit the RationalNumber homepage to try an online demo of rational number calculations running within a webpage.

Testing

Input and output from the code can be tested using mocha and the JavaScript files in the test directory. To test from a node installation:

$ npm install   # to download mocha dependency if necessary
$ npm test

Website

The website for RationalNumber documentation is http://rationalnumber.sapp.org.

And the corresponding GitHub repository is https://github.com/craigsapp/RationalNumber.

List of RationalNumber methods

Click on the method name to view documentation for that function.

Additional methods provided in RationalNumber-math.js:

  • abs — Return non-negative copy of the rational number.
  • invert — Switch the numerator and denominator.
  • inversion — Alias for getInversion method.
  • getInversion — Return a reciprocal copy of called object.
  • negate — Make positive values negative and vice-versa.
  • negation — Alias for getNegation method.
  • getNegation — Return a copy of the object, with sign reversed.
  • addTo — To this RationalNumber, add additional numbers.
  • add — Similar to addTo(), but returns sum rather than altering contents.
  • subtractTo — To this RationalNumber, subtract values.
  • subtract — Similar to subtractTo(), but returns result rather than altering calling object.
  • multiplyTo — To this RationalNumber, multiply values.
  • multiply — Similar to multiplyTo(), but returns result rather than altering calling object.
  • divideTo — To this RationalNumber, divide values.
  • divide — Similar to divideTo(), but returns result rather than altering calling object.