Skip to content

Welcome to MathPrecise, the JavaScript library for when you need your numbers to be, well, precise. Say goodbye to floating-point weirdness and hello to arbitrary-precision decimal arithmetic that just works.

License

Notifications You must be signed in to change notification settings

enderhacker/mathprecise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

MathPrecise v0.1 - Documentation

Welcome to MathPrecise, the JavaScript library for when you need your numbers to be, well, precise. Say goodbye to floating-point weirdness and hello to arbitrary-precision decimal arithmetic that just works.

Installation

Getting started is simple.

Node.js

npm install mathprecise

Then, require it in your project:

const MathPrecise = require("mathprecise");

Browser

Include the precisemath.js file in your HTML:

<script src="path/to/precisemath.js"></script>

The MathPrecise constructor will then be available globally.

Core Usage

The core of the library is the MathPrecise object. You can create one from a number, string, or another MathPrecise instance.

const a = new MathPrecise("123.4567890123456789");
const b = new MathPrecise(0.1);
const c = new MathPrecise("1e-20"); // Scientific notation is supported!

Pro Tip: For maximum precision, always pass numbers as strings. JavaScript's native number type can introduce floating-point inaccuracies before the library even gets the value.

Configuration

Set the default precision for division, square roots, and other operations. The initial default is 20.

MathPrecise.config({ precision })

Sets the global default precision for calculations.

// Set default precision to 40 decimal places
MathPrecise.config({ precision: 40 });

const result = new MathPrecise(1).divide(3); // Result will have 40 decimal places
console.log(result.toString()); // "0.3333333333333333333333333333333333333333"

API Reference

All instance methods return a new MathPrecise instance, allowing you to chain operations fluently.

Basic Arithmetic

Method Description Example
.add(n) Adds n to the number. new MathPrecise('0.1').add('0.2') -> '0.3'
.subtract(n) Subtracts n from the number. new MathPrecise('10').subtract('0.05') -> '9.95'
.multiply(n) Multiplies the number by n. new MathPrecise('1.5').multiply(3) -> '4.5'
.divide(n, [prec]) Divides the number by n. new MathPrecise(2).divide(3, 5) -> '0.66667'

Advanced Operations

Method Description Example
.power(exp) Raises the number to the integer power of exp. new MathPrecise(2).power(10) -> '1024'
.sqrt([prec]) Calculates the square root. new MathPrecise(144).sqrt() -> '12'
.nthRoot(n, [prec]) Calculates the nth root. new MathPrecise(27).nthRoot(3) -> '3'
.mod(n) Returns the remainder of a division. new MathPrecise('10').mod('3') -> '1'
.exp([prec]) Calculates e^x (the exponential function). new MathPrecise(1).exp(10) -> '2.7182818285'
.log([base], [prec]) Calculates the logarithm. Base is e if omitted. new MathPrecise(100).log(10) -> '2'

Number Theory

Method Description Example
.gcd(n) Finds the greatest common divisor. new MathPrecise(48).gcd(18) -> '6'
.lcm(n) Finds the least common multiple. new MathPrecise(12).lcm(18) -> '36'

Rounding & Truncation

Method Description Example
.round([prec]) Rounds to a specified number of decimal places (default is 0). new MathPrecise('2.555').round(1) -> '2.6'
.floor([prec]) Rounds down to the specified precision. new MathPrecise('3.99').floor() -> '3'
.ceil([prec]) Rounds up to the specified precision. new MathPrecise('3.01').ceil() -> '4'

Comparison & State Checks

These methods are your tools for logical operations.

Method Description Example
.compareTo(n) Returns -1 if less than n, 1 if greater, 0 if equal. new MathPrecise(5).compareTo(10) -> -1
.equals(n) Returns true if the numbers are equal. new MathPrecise('0.1').add('0.2').equals('0.3') -> true
.gt(n) Greater than. .gte(n) Greater than or equal to.
.lt(n) Less than. .lte(n) Less than or equal to.
.isZero() Returns true if the value is zero.
.isNegative() Returns true if the value is negative.
.isPositive() Returns true if the value is positive and not zero.
.isInteger() Returns true if the value has no fractional part.

Formatting & Transformation

Method Description Example
.abs() Returns the absolute value. new MathPrecise('-99').abs() -> '99'
.negate() Flips the sign of the number. new MathPrecise(5).negate() -> '-5'
.toFraction() Converts the number to a string fraction. new MathPrecise('1.5').toFraction() -> '3/2'
.toFixed(d) Formats with a fixed number of decimal places. new MathPrecise(5).toFixed(3) -> '5.000'
.toString() Returns the string representation. (Also valueOf, toJSON) new MathPrecise('123.450').toString() -> '123.45'
.precision() Returns the number of decimal places in the current instance. new MathPrecise('12.345').precision() -> 3

Static Methods

These are called directly on the MathPrecise constructor.

Method Description Example
MathPrecise.max(...args) Returns the largest of the given numbers. MathPrecise.max('10', 5, '10.1') -> '10.1'
MathPrecise.min(...args) Returns the smallest of the given numbers. MathPrecise.min(0, '-10', '-9.9') -> '-10'

Constants

These are called directly on the MathPrecise constructor.

Constant Description Example
MathPrecise.PI π (pi) MathPrecise.PI.toString() -> '3.14159265358979323846'
MathPrecise.E e (Euler's number) MathPrecise.E.toString() -> '2.71828182845904523536'
MathPrecise.LN2 Natural log of 2 MathPrecise.LN2.toString() -> '0.6931471805599453'
MathPrecise.LN10 Natural log of 10 MathPrecise.LN10.toString() -> '2.302585092994046'
MathPrecise.SQRT2 Square root of 2 MathPrecise.SQRT2.toString() -> '1.4142135623730951'
MathPrecise.SQRT1_2 1 divided by √2 MathPrecise.SQRT1_2.toString() -> '0.7071067811865476'
MathPrecise.PHI Golden ratio (φ) MathPrecise.PHI.toString() -> '1.6180339887498948'
MathPrecise.GAMMA Euler–Mascheroni constant MathPrecise.GAMMA.toString() -> '0.5772156649015329'
MathPrecise.PI_2 π / 2 MathPrecise.PI_2.toString() -> '1.5707963267948966'
MathPrecise.PI_4 π / 4 MathPrecise.PI_4.toString() -> '0.7853981633974483'
MathPrecise.INV_PI 1 / π MathPrecise.INV_PI.toString() -> '0.3183098861837907'
MathPrecise.INV_SQRT2 1 / √2 MathPrecise.INV_SQRT2.toString() -> '0.7071067811865476'

Made with ❤️ by enderhacker

About

Welcome to MathPrecise, the JavaScript library for when you need your numbers to be, well, precise. Say goodbye to floating-point weirdness and hello to arbitrary-precision decimal arithmetic that just works.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published