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

math-multitool

v1.0.3

Published

A handy Swiss Army knife of math tools

Downloads

9

Readme

Math Multitool Calculator

A collection of simple, handy JavaScript math tools that can be implemented anywhere.

Installation

npm install --save math-multitool

In node:

const MMCalculator = require('math-multitool');

In browsers:

<script type="module" src="./path/to/math-multitool.js"></script>

-or-

import MMCalculator from './path/to/math-multitool.js';

Usage

In plain JavaScript, decimal calculations can be inaccurate:

const x = [0.1,0.2];

console.log(x[0] + x[1]);    // Returns 0.30000000000000004
console.log(x[0] * x[1]);    // Returns 0.020000000000000004

Math Multitool provides a class (MMCalculator) with getters and utility methods that allow you to do things like sum, multiply, average, and get the median of arrays of numbers accurately. With MMCalculator, decimal calculations will be accurate up to 15 digits.

Currently, MMCalculator will perform the following actions:

  • Sum
  • Multiply
  • Divide
  • Subtract
  • Average (mean, median, and mode)
  • Get Minimum
  • Get Maximum

MMCalculator class

The MMCalculator class is initialized with an array of numbers.

const myArray = [2, 2.5, 5, 5, 4];
const myCalc = new MMCalculator (myArray);

// MMCalculator uses getter syntax to make performing calculations simple:

console.log(myCalc.max);            // Returns 5
console.log(myCalc.min);            // Returns 2

console.log(myCalc.product);        // Returns 500
console.log(myCalc.sum);            // Returns 18.5
console.log(myCalc.mean);           // Returns 3.7
console.log(myCalc.median);         // Returns 4
console.log(myCalc.mode);           // Returns 5

To change the array, set the value of the calculator object to a new array:

myCalc.value = [5, 15, 8.7, 103, 0.05];

MMCalculator utility methods

MMCalculator's utility methods are:

  • add(arg1, ...argN)
  • subtract(arg1, arg2)
  • multiply(arg1, ...argN)
  • divide(arg1, arg2)
  • fractionalize(value)
  • decimalize(numerator, denominator).

Example:

console.log(MMCalculator.add(0.125, 0.20501)) // Returns exactly 0.33001

Note: Fractionalize is designed to return fractions with denominators equal to a power of 10. That is to say, rather than returning a fraction of [1, 2], it will always return a fraction of [5, 10].

Future Plans

I'll add more math functions to math-multitool.js as I can.