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

@rnacanvas/math

v1.19.0

Published

General math utilities

Downloads

707

Readme

Installation

With npm:

npm install @rnacanvas/math

Usage

All exports of this package can be accessed as named imports.

// some example imports
import { min, max } from '@rnacanvas/math';
import { mean, median } from '@rnacanvas/math';
import { sortNumbers, sortedNumbers } from '@rnacanvas/math';

How to pass collections of numbers as parameters

In general throughout this package collections of numbers are passed to functions/methods in the form of iterable objects (e.g., an array), as opposed to argument lists.

sum([1, 2, 3, 4, 5]); // do this
sum(1, 2, 3, 4, 5); // NOT this

This decision was made due to the limit on how many arguments can possibly be passed to a function in JavaScript on the stack (often only in the 10's of thousands), while numbers passed in as arrays are limited only by the total memory available.

sum()

Calculates the sum of an array of numbers. Returns 0 for an empty array of numbers.

sum([]); // 0
sum([4]); // 4
sum([5, 10, -2, 23, -54]); // -18

mean()

Calculates the mean of an array of numbers. Returns NaN for an empty array of numbers.

mean([]); // NaN
mean([18]); // 18
mean([8, 2, -20, 16, 54]); // 12

average()

An alias for the mean() function.

median()

Returns the median of an array of numbers. If there an even number of numbers, then the average of the middle two numbers is returned. Returns NaN for an empty array of numbers.

median([]); // NaN
median([6]); // 6
median([11, 17]); // 14
median([4, -9, 28, 22, 9, 7, -3]); // 7

min()

Returns the minimum of an array of numbers. Returns Infinity for an empty array of numbers.

min([]); // Infinity
min([57]); // 57
min([5, 9, -12, 18, -19, 24]); // -19

max()

Returns the maximum of an array of numbers. Returns -Infinity for an empty array of numbers.

max([]); // -Infinity
max([-84]); // -84
max([18, 1, 55, -28, 19.4]); // 55

isBetween()

Returns true if and only if a number is within a specified range, inclusive.

isBetween(n, floor, ceiling); // usage

isBetween(6, 7, 9); // false
isBetween(7, 7, 9); // true
isBetween(8, 7, 9); // true
isBetween(9, 7, 9); // true
isBetween(10, 7, 9); // false

isBetweenInclusive()

An alias for the isBetween() function.

isBetweenExclusive()

Returns true if and only if a number is within a specified range, exclusive.

isBetweenExclusive(n, floor, ceiling); // usage

isBetweenExclusive(6, 7, 9); // false
isBetweenExclusive(7, 7, 9); // false
isBetweenExclusive(8, 7, 9); // true
isBetweenExclusive(9, 7, 9); // false
isBetweenExclusive(10, 7, 9); // false

clamp()

Clamp a number to a given range.

clamp(n, floor, ceiling); // usage

clamp(2, 5, 10); // 5
clamp(18, 5, 10); // 10
clamp(6, 5, 10); // 6 (is already in range)

areWithin()

Returns true if two numbers are within a specified distance from each other.

areWithin(n1, n2, maxDifference); // usage

areWithin(5, 5, 0); // true
areWithin(5, 5, 2); // true
areWithin(5, 6, 2); // true
areWithin(5, 7, 2); // true
areWithin(5, 8, 2); // false

sortNumbers()

Sorts an array of numbers in place in ascending order.

var ns = [8, -3, 55, 24, 39];
sortNumbers(ns);
ns; // [-3, 8, 24, 39, 55]

sortNumbersAscending()

An alias for the sortNumbers() function.

sortNumbersDescending()

Sorts an array of numbers in place in descending order.

var ns = [9, 27, -84, 0, -19];
sortNumbersDescending(ns);
ns; // [27, 9, 0, -19, -84]

sortedNumbers()

Returns a copy of an array of numbers sorted in ascending order. Does not modify the input array of numbers.

sortedNumbers([5, 28, -44, 2, -1]); // [-44, -1, 2, 5, 28]

sortedNumbersAscending()

An alias for the sortedNumbers() function.

sortedNumbersDescending()

Returns a copy of an array of numbers sorted in descending order. Does not modify the input array of numbers.

sortedNumbersDescending([6, 2, 91, -15, 25]); // [91, 25, 6, 2, -15]

degrees()

Convert a given angle in radians to degrees.

degrees(Math.PI); // 180
degrees(-Math.PI); // -180

degrees(Math.PI / 3); // 60
degrees(-11 * Math.PI / 4); // -495

radians()

Convert a given angle in degrees to radians.

radians(180); // Math.PI
radians(-180); // -Math.PI

radians(60); // Math.PI / 3
radians(-495); // -11 * Math.PI / 4

normalizeAngle()

Normalize an angle (in radians) to a given range.

By default, normalizes angles to the range [-Math.PI, Math.PI), which is the default range returned by methods such as Math.atan2().

The range to normalize to is indicated by specifying a second floor argument, which is the bottom end (inclusive) of the range to normalize to.

normalizeAngle((Math.PI / 3) + (12 * Math.PI)); // Math.PI / 3

// normalizes to the range [-Math.PI, Math.PI) by default
normalizeAngle(3 * Math.PI / 2); // -Math.PI / 2

// normalize angles to the range [0, 2 * Math.PI) instead
// (by passing in a second "floor" argument of zero)
normalizeAngle(3 * Math.PI / 2, 0); // 3 * Math.PI / 2

// the second "floor" argument can be any angle
normalizeAngle(Math.PI, -14 * Math.PI); // Math.PI - (14 * Math.PI)