floating
v3.0.0
Published
Simple wrapper for basic arithmetic
Downloads
73
Maintainers
Readme
floating
Simple wrapper for basic arithmetic—addition, subtraction, multiplication and division.
Useful for plugging it into arbitrary-precision or linear-arbitrary-precision as a fallback to specialised arbitrary precision libraries.
Install
npm i floating
Usage
Factory and configuration
var Floating = require('floating')();
new Floating(1).div(new Floating(3).valueOf(); // => 1/3
Operations
new Floating(0.1).plus(new Floating(0.2)).valueOf(); // => 0.1 + 0.2
new Floating(0.3).minus(new Floating(0.1)).valueOf(); // => 0.3 - 0.1
new Floating(0.6).times(new Floating(3)).valueOf(); // => 0.6 * 3
new Floating(0.3).div(new Floating(0.2)).valueOf(); // => 0.3 / 0.2
toString, valueOf and toJSON
var floatingThird = new Floating(1).div(new Floating(3));
floatingThird.toString(); // => '0.3333333333333333'
floatingThird.valueOf() === floatingThird.toJSON(); // => true
Number(floatingThird); // => 1/3
JSON.stringify and JSON.parse with reviver
var stringified = JSON.stringify(floatingThird); // => '0.3333333333333333'
JSON.parse(stringified, Floating.reviver); // => new Floating(0.3333333333333333)
See spec.
Related projects
- linear-converter: flexible linear converter with built in conversions for common units.
- linear-conversion: Linear conversion class for linear-converter.
- rescale: rescales a point given two scales.
- rescale-util: Rescale utilities.
- scale: scales normalised data.
- normalise: normalise data to [0, 1].