GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - nberlette/math: Standalone implementation of the entire `Math` namespace (and more!), compatible with any JS runtime. Zero dependencies.

BranchesTags
Open more actions menu

Latest commit

 

History

371 Commits
371 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
bench
bench
 
 
scripts
scripts
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
deno.json
deno.json
 
 
deno.lock
deno.lock
 
 

Repository files navigation

@nick/math

Dependency-free implementation of the Math.* namespace


Overview

This package provides zero-dependency standalone implementations for all of the [functions] and [constants] found in the native Math namespace, most of those found on the global Number constructor function, with some additional non-standard helpers, constants, and guards for various other purposes.

Usage

Written in pure TypeScript with absolutely zero dependencies. Organized in a modular structure to facilitate bundler tree-shaking (dead code elimination), you can either import the entire package as a namespace (useful for rapid prototyping during development), or on a modular basis where only the features required are imported (recommended for production).

When imported as a namespace (or via the default export), this package is suitable to use as a drop-in replacement for any standard Math.* feature's usage1.

Install

This package is published to JSR as @nick/math, and to NPM and [GPR] as @nberlette/math, for maximum availability on all major package managers:

deno add jsr:@nick/math
pnpm add jsr:@nick/math
yarn add jsr:@nick/math
vlt add jsr:@nick/math
bunx jsr add @nick/math
npx jsr add @nick/math

NPM and GPR

deno add npm:@nberlette/math
pnpm add @nberlette/math
yarn add @nberlette/math
vlt add @nberlette/math
bun add @nberlette/math
npm i @nberlette/math

Example Usage

Namespace

import * as math from "@nick/math";
import assert from "node:assert";

assert.ok((math.acos(0.5) - Math.acos(0.5)) < 1e-9);
assert.strictEqual(math.NEGATIVE_ZERO, -0);
assert.strictEqual(math.PI, 3.141592653589793 /* Math.PI */);

Modular

import { PI } from "@nick/math/pi";
import { f16round } from "@nick/math/f16round";
import { sign } from "@nick/math/sign";
import { NEGATIVE_INFINITY } from "@nick/math/constants/negative-infinity";
import assert from "node:assert";

assert.strictEqual(f16round(PI), 3.140625);
assert.strictEqual(sign(-0), -0);
assert.strictEqual(sign(PI), 1);
assert.strictEqual(sign(NEGATIVE_INFINITY), -1);

API

Table of Contents
  • Constants
    • E (Euler's number)
    • LN10 (Natural logarithm of 10)
    • LN2 (Natural logarithm of 2)
    • LOG2E (Base 2 logarithm of E)
    • LOG10E (Base 10 logarithm of E)
    • PI (π)
    • SQRT1_2 (Square root of 1/2)
    • SQRT2 (Square root of 2)
    • EPSILON
    • MAX_SAFE_INTEGER
    • MIN_SAFE_INTEGER
    • MAX_VALUE
    • MIN_VALUE
    • NEGATIVE_INFINITY
    • POSITIVE_INFINITY
    • NAN
  • Functions
    • abs
    • acos
    • acosh
    • asin
    • asinh
    • atan
    • atan2
    • atanh
    • cbrt
    • ceil
    • clz32
    • cos
    • cosh
    • exp
    • expm1
    • floor
    • fround
    • f16round
    • hypot
    • imul
    • log
    • log1p
    • log10
    • log2
    • max
    • min
    • pow
    • random
    • round
    • sign
    • sin
    • sinh
    • sqrt
    • tan
    • tanh
    • trunc
  • Guards
    • isFinite
    • isInteger
    • isNaN
    • isSafeInteger
    • isNegativeZero
    • isPositiveInfinity
    • isNegativeInfinity

Constants

E (Euler's number)

import { E } from "@nick/math/constants/e";
import assert from "node:assert";

assert.strictEqual(E, 2.718281828459045);

LN10 (Natural logarithm of 10)

import { LN10 } from "@nick/math/constants/ln10";
import assert from "node:assert";

assert.strictEqual(LN10, 2.302585092994046);

LN2 (Natural logarithm of 2)

import { LN2 } from "@nick/math/constants/ln2";
import assert from "node:assert";

assert.strictEqual(LN2, 0.6931471805599453);

LOG2E (Base 2 logarithm of E)

import { LOG2E } from "@nick/math/constants/log2e";
import assert from "node:assert";

assert.strictEqual(LOG2E, 1.4426950408889634);

LOG10E (Base 10 logarithm of E)

import { LOG10E } from "@nick/math/constants/log10e";
import assert from "node:assert";

assert.strictEqual(LOG10E, 0.4342944819032518);

PI (π)

import { PI } from "@nick/math/constants/pi";
import assert from "node:assert";

assert.strictEqual(PI, 3.141592653589793);

SQRT1_2 (Square root of 1/2)

import { SQRT1_2 } from "@nick/math/constants/sqrt1_2";
import assert from "node:assert";

assert.strictEqual(SQRT1_2, 0.7071067811865476);

SQRT2 (Square root of 2)

import { SQRT2 } from "@nick/math/constants/sqrt2";
import assert from "node:assert";

assert.strictEqual(SQRT2, 1.4142135623730951);

EPSILON

Represents the smallest positive number that can be added to 1 to get a result different from 1.

import { EPSILON } from "@nick/math/constants/epsilon";
import assert from "node:assert";

assert.strictEqual(EPSILON, 2.220446049250313e-16);

MAX_SAFE_INTEGER

The maximum safe integer in JavaScript, 2^53 - 1.

import { MAX_SAFE_INTEGER } from "@nick/math/constants/max-safe-integer";
import assert from "node:assert";

assert.strictEqual(MAX_SAFE_INTEGER, 9007199254740991);

MIN_SAFE_INTEGER

The minimum safe integer in JavaScript, -2^53 + 1.

import { MIN_SAFE_INTEGER } from "@nick/math/constants/min-safe-integer";
import assert from "node:assert";

assert.strictEqual(MIN_SAFE_INTEGER, -9007199254740991);

MAX_VALUE

The maximum representable value in JavaScript.

import { MAX_VALUE } from "@nick/math/constants/max-value";
import assert from "node:assert";

assert.strictEqual(MAX_VALUE, 1.7976931348623157e308);

MIN_VALUE

The minimum representable value in JavaScript.

import { MIN_VALUE } from "@nick/math/constants/min-value";
import assert from "node:assert";

assert.strictEqual(MIN_VALUE, 5e-324);

NEGATIVE_INFINITY

The negative infinity value in JavaScript.

import { NEGATIVE_INFINITY } from "@nick/math/constants/negative-infinity";
import assert from "node:assert";

assert.strictEqual(NEGATIVE_INFINITY, -Infinity);

POSITIVE_INFINITY

The positive infinity value in JavaScript.

import { POSITIVE_INFINITY } from "@nick/math/constants/positive-infinity";
import assert from "node:assert";

assert.strictEqual(POSITIVE_INFINITY, Infinity);

NAN

The NaN value in JavaScript, representing "Not a Number".

import { NAN } from "@nick/math/constants/nan";
import assert from "node:assert";

assert.ok(NAN !== NAN);
Alias: NaN
import { NaN } from "@nick/math/constants/nan";
import assert from "node:assert";

assert.ok(NaN !== NaN);

Functions

abs

Returns the absolute value of a number.

import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.strictEqual(abs(-1), 1);

acos

Returns the arccosine of a number.

import { acos } from "@nick/math/acos";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(acos(0.5) - Math.acos(0.5)) < 1e-9);

acosh

Returns the inverse hyperbolic cosine of a number.

import { acosh } from "@nick/math/acosh";
import assert from "node:assert";

assert.strictEqual(acosh(1), 0);

asin

Returns the arcsine of a number.

import { asin } from "@nick/math/asin";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(asin(0.5) - Math.asin(0.5)) < 1e-9);

asinh

Returns the inverse hyperbolic sine of a number.

import { asinh } from "@nick/math/asinh";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(asinh(0.5) - Math.asinh(0.5)) < 1e-9);

atan

Returns the arctangent of a number.

import { atan } from "@nick/math/atan";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(atan(0.5) - Math.atan(0.5)) < 1e-9);

atan2

Returns the arctangent of the quotient of its arguments.

import { atan2 } from "@nick/math/atan2";
import assert from "node:assert";

assert.strictEqual(atan2(1, 1), Math.atan2(1, 1));

atanh

Returns the inverse hyperbolic tangent of a number.

import { atanh } from "@nick/math/atanh";
import assert from "node:assert";

assert.strictEqual(atanh(0.5), Math.atanh(0.5));

cbrt

Returns the cube root of a number.

import { cbrt } from "@nick/math/cbrt";
import assert from "node:assert";

assert.strictEqual(cbrt(27), 3);

ceil

Returns the smallest integer greater than or equal to a number.

import { ceil } from "@nick/math/ceil";
import assert from "node:assert";

assert.strictEqual(ceil(1.5), 2);

clz32

Returns the number of leading zero bits in the 32-bit binary representation of a number.

import { clz32 } from "@nick/math/clz32";
import assert from "node:assert";

assert.strictEqual(clz32(1), 31);

cos

Returns the cosine of a number.

import { cos } from "@nick/math/cos";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(cos(0.5) - Math.cos(0.5)) < 1e-9);

cosh

Returns the hyperbolic cosine of a number.

import { cosh } from "@nick/math/cosh";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(cosh(0.5) - Math.cosh(0.5)) < 1e-9);

exp

Returns E raised to the power of a number.

import { exp } from "@nick/math/exp";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(exp(1) - Math.exp(1)) < 1e-9);

expm1

Returns E raised to the power of a number, minus 1.

import { expm1 } from "@nick/math/expm1";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(expm1(1) - Math.expm1(1)) < 1e-9);

floor

Returns the largest integer less than or equal to a number.

import { floor } from "@nick/math/floor";
import assert from "node:assert";

assert.strictEqual(floor(1.5), 1);

fround

Returns the nearest single precision float representation of a number.

import { fround } from "@nick/math/fround";
import { PI } from "@nick/math/constants/pi";

console.log(PI, "->", fround(PI));
// 3.141592653589793 -> 3.1415927410125732

f16round

Returns the nearest 16-bit float representation of a number, as per the IEEE 754 standard. Introduced by the TC39 Proposal for Float16Array.

import { f16round } from "@nick/math/f16round";
import { PI } from "@nick/math/constants/pi";
import assert from "node:assert";

assert.strictEqual(f16round(PI), 3.140625);

hypot

Returns the square root of the sum of the squares of its arguments.

import { hypot } from "@nick/math/hypot";
import assert from "node:assert";

assert.strictEqual(hypot(3, 4), 5);

imul

Returns the result of a 32-bit integer multiplication.

import { imul } from "@nick/math/imul";
import assert from "node:assert";

assert.strictEqual(imul(2, 3), 6);

log

Returns the natural logarithm of a number.

import { log } from "@nick/math/log";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(log(9) - Math.log(9)) < 1e-9);

log1p

Returns the natural logarithm of 1 plus a number.

import { log1p } from "@nick/math/log1p";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(log1p(9) - Math.log1p(9)) < 1e-9);

log10

Returns the base 10 logarithm of a number.

import { log10 } from "@nick/math/log10";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(log10(100) - Math.log10(100)) < 1e-9);

log2

Returns the base 2 logarithm of a number.

import { log2 } from "@nick/math/log2";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(log2(8) - Math.log2(8)) < 1e-9);

max

Returns the largest of zero or more numbers.

import { max } from "@nick/math/max";
import assert from "node:assert";

assert.strictEqual(max(1, 2, 3), 3);

min

Returns the smallest of zero or more numbers.

import { min } from "@nick/math/min";
import assert from "node:assert";

assert.strictEqual(min(1, 2, 3), 1);

pow

Returns the base to the exponent power.

import { pow } from "@nick/math/pow";
import assert from "node:assert";

assert.strictEqual(pow(2, 3), 8);

random

Returns a pseudo-random number between 0 and 1.

import { random } from "@nick/math/random";
import assert from "node:assert";

const value = random();

assert.ok(value >= 0 && value < 1);

round

Returns the value of a number rounded to the nearest integer.

import { round } from "@nick/math/round";
import assert from "node:assert";

assert.strictEqual(round(1.5), 2);

sign

Returns the sign of a number, indicating whether it is positive, negative, or zero.

import { sign } from "@nick/math/sign";
import assert from "node:assert";

assert.strictEqual(sign(-1), -1);

sin

Returns the sine of a number.

import { sin } from "@nick/math/sin";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(sin(0.5) - Math.sin(0.5)) < 1e-9);

sinh

Returns the hyperbolic sine of a number.

import { sinh } from "@nick/math/sinh";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(sinh(0.5) - Math.sinh(0.5)) < 1e-9);

sqrt

Returns the square root of a number.

import { sqrt } from "@nick/math/sqrt";
import assert from "node:assert";

assert.strictEqual(sqrt(9), 3);

tan

Returns the tangent of a number.

import { tan } from "@nick/math/tan";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(tan(0.5) - Math.tan(0.5)) < 1e-9);

tanh

Returns the hyperbolic tangent of a number.

import { tanh } from "@nick/math/tanh";
import { abs } from "@nick/math/abs";
import assert from "node:assert";

assert.ok(abs(tanh(0.5) - Math.tanh(0.5)) < 1e-9);

trunc

Returns the integer part of a number by removing any fractional digits.

import { trunc } from "@nick/math/trunc";
import assert from "node:assert";

assert.strictEqual(trunc(1.5), 1);

Guards

isFinite

Determines whether a number is finite.

import { isFinite } from "@nick/math/is/finite";
import assert from "node:assert";

assert.ok(isFinite(1));

isInteger

Determines whether a number is an integer.

import { isInteger } from "@nick/math/is/integer";
import assert from "node:assert";

assert.ok(isInteger(1));

isNaN

Determines whether a value is NaN.

import { isNaN } from "@nick/math/is/nan";
import assert from "node:assert";

assert.ok(isNaN(NaN));

isSafeInteger

Determines whether a number is a safe integer.

import { isSafeInteger } from "@nick/math/is/safe-integer";
import assert from "node:assert";

assert.ok(isSafeInteger(1));

isNegativeZero

Determines whether a number is -0.

import { isNegativeZero } from "@nick/math/is/negative-zero";
import assert from "node:assert";

assert.ok(isNegativeZero(-0));
assert.ok(!isNegativeZero(0));

isPositiveInfinity

Determines whether a number is positive infinity.

import { isPositiveInfinity } from "@nick/math/is/positive-infinity";
import assert from "node:assert";

assert.ok(isPositiveInfinity(Infinity));

isNegativeInfinity

Determines whether a number is negative infinity.

import { isNegativeInfinity } from "@nick/math/is/negative-infinity";
import assert from "node:assert";

assert.ok(isNegativeInfinity(-Infinity));

MIT © Nicholas Berlette. All rights reserved.

github · issues · jsr · npm · contributing

Footnotes

  1. All modules in this package are implemented using 100% manual logic and arithmetic, and do not rely on any native code or objects. As such, the performance will typically be slightly slower than the native Math.* implementations. There may also be some minor deviations in output. If you believe a specific function is inaccurate or otherwise inconsistent with the expected behavior, please open an issue so I can address it and attempt to improve it. Thank you! ↩

About

Standalone implementation of the entire `Math` namespace (and more!), compatible with any JS runtime. Zero dependencies.

jsr.io/@nick/math/doc

Topics

arithmeticconstantsf16floating-pointlogarithmicmathmathematicsponyfillpure-javascriptroundingtrigonometrytype-guardstypescriptuniversal

Resources

Readme
MIT license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity

Stars

3 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.