A javascript implementation of the International System of Quantities, ISO-80000.
The latest documentation can be read at Read the Docs.
Features
- Uncertainty -
SI.Quantity('123.456(4) km')
- SI notation -
SI.Quantity('1.234 56(4) × 10² km')
- ASCII notation -
SI.Quantity('1.234 56(4) x 10^2 km')
- Conversion -
SI.Quantity('25 m/s').to('km/h')
- Symbolic expressions -
SI.Quantity('W/(m² sr)')
- Pluggable -
SI.config.Number = require('big.js')
Install with npm
> npm install isq
Usage
Include the package
var SI = require('isq')
Create a number with SI.Number
var a = SI.Number(0.3),
b = SI.Number('0.1'),
c = SI.Number('0.03(1)'), // 0.03±0.01
d = SI.Number(0.03, 0.01); // same as c
Create a quantity with SI.Quantity with an optional number and a unit
var a = SI.Quantity('1.03 kg'),
b = SI.Quantity('1.03(1) kg') // 1.03±0.01 kg
kg = SI.Quantity('kg'); // 1 kg
A Quantity is converted to a string with the toString() method. It uses a heuristic to determine the best symbol to use.
SI.Quantity('5 * 10^-6 s').toString() // 5 µs
SI.Quantity('50 000 V/A').toString() // 50 kΩ
Javascript does not allow overiding of operators, so named methods are used. The methods are also chainable. For example, the hypotenuse of a triangle is
var a = SI.Quantity('1.2 m'),
b = SI.Quantity('80 cm'), // 0.8 m
c = a.pow(2).plus(b.pow(2)).sqrt(); // ~ 1.44 m
Rounding errors
Rounding and precision errors are notorious in Javascript. For example 0.3 - 0.1
produces 0.19999999999999998
and NOT 0.2
. ISQ can be configured to use a 'big number' package that avoids these issues.
SI.config.Number = require('big.js');
SI.Number(0.3).minus(SI.Number(0.1)) // 0.2
Uncertainty
Uncertainity, or margin of error, describes the imperfect nature of a measurement. Typically, it is the standard deviation of actual measurements. Anytime a calculation is performed, propagation of uncertainity is also performed to determine the uncertainty of the result.
let a = SI.Number('1.2(2)'), // 1.2±0.2
b = SI.Number('1.3(3)'), // 1.3±0.3
length = a.plus(b); // 2.5±0.4
When comparing uncertain numbers, the uncertainity of both values is taken into consideration. Equality Is the difference of the two values within the resulting uncertainty?
License
The MIT license.
Copyright © 2015-2016 Richard Schneider ([email protected])