🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

tiny-exact-math

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-exact-math

Minimal rational arithmetic library for exact geometric predicates in pure JavaScript

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

tiny-exact-math

Minimal rational arithmetic library for exact geometric predicates in pure JavaScript. Zero dependencies — uses native BigInt.

Install

npm install tiny-exact-math

Usage

import { Rational, fromNumber, determinant, determinant3 } from 'tiny-exact-math';

// Create rationals
const a = new Rational(1n, 3n);       // 1/3
const b = fromNumber(0.1);            // exact representation of 0.1

// Arithmetic
a.add(b);        // returns new Rational
a.subtract(b);
a.multiply(b);
a.divide(b);

// 2D orientation test (sign of 2×2 determinant)
// Returns +1 (counter-clockwise), -1 (clockwise), or 0 (collinear)
determinant(
  { x: 0, y: 0 },
  { x: 1, y: 0 },
  { x: 0, y: 1 }
); // +1

// 3D plane-side test (sign of 3×3 determinant)
// Returns +1 (above plane), -1 (below), or 0 (coplanar)
determinant3(
  { x: 0, y: 0, z: 0 },
  { x: 1, y: 0, z: 0 },
  { x: 0, y: 1, z: 0 },
  { x: 0, y: 0, z: 1 }
); // +1

API

new Rational(numerator, denominator?)

Create a rational number from BigInt or number values. Automatically normalised to lowest terms with a positive denominator.

fromNumber(n)

Convert a JavaScript number (integer or float) to an exact Rational. Uses the shortest decimal round-trip representation to avoid precision loss.

gcd(a, b)

Greatest common divisor of two BigInt values.

Rational instance methods

MethodDescription
add(other)Addition
subtract(other)Subtraction
multiply(other)Multiplication
divide(other)Division
sign()Returns -1, 0, or 1
isZero()Returns true if zero
toNumber()Convert to JS number (may lose precision)
toString()Human-readable string ("1/3")

determinant(p1, p2, p3)

Exact 2D orientation test. Returns the sign of the 2×2 determinant formed by three {x, y} points.

determinant3(p1, p2, p3, p4)

Exact 3D plane-side test. Returns the sign of the 3×3 determinant formed by four {x, y, z} points.

License

MIT

Keywords

rational

FAQs

Package last updated on 25 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts