Skip to content
munrocket edited this page Dec 29, 2019 · 20 revisions

Choose ES5, ES6 or typescript workflow and add script on page <script src="http://unpkg.com/double.js"></script> or install it from NPM npm install double.js.

import Double as D from 'double.js'; //es6/typescript
var D = require('double.js');        //es5

Try to calculate something, here example for R = sqrt(a^2 + b^2)

let R = a.sqr().add(b.sqr()).sqrt();

Most of all functions have static and instance methods. Instance methods more handy. Static methods are little bit faster but you need to control memory allocation by yourself. Result of static methods always returned in first variable, that's why they mutate it. If you want to avoid mutation you need to clone it before usage.

Here same example with static method

let R = D.sqrt(D.add22(D.sqr2(D.clone(a)), D.sqr2(D.clone(b)));

Here full API specification, mutable variables tagged with exclamation mark (!). You can predict theoretical performance time with column Relative time, for example r.pow(3) in 34.5 times slower than r.pown(3). Names of instance methods similar to WASM operations.

Description Instance method Static method Relative time
Constructors new D([a, b])
-//- D.clone(X)
-//- D.fromMul11(a, b) 17 flop
-//- D.fromSum11(a, b) 6 flop
-//- new D(number) D.fromNumber(number)
-//- new D('double_number') D.fromString(string) on RegEx
Converters X.toNumber()
-//- X.toExponential(precision)
Addition X.add(Y) D.add22(X!, Y) 20 flop
-//- -//- D.add21(X!, f) 10 flop
Subtraction X.sub(Y) D.sub22(X!, Y) 20 flop
-//- -//- D.sub21(X!, f) 10 flop
Multiplication X.mul(Y) D.mul22(X!, Y) 24 flop
-//- -//- D.mul21(X!, f) 25 flop
Mult. to a=2^n D.mul21pow2(X!, f) 2 flop
Division X.div(Y) D.div22(X!, f) 27 flop
-//- -//- D.div21(X!, f) 31 flop
Power X.pow(Y) D.pow22(X!, Y) ~2588 flop
Power of integer X.pown(n) D.pow2n(X!, n) 24*log2(n) flop
Absolute value X.abs() D.abs2(X!)
Negate X.neg() D.neg2(X!)
Inverse X.inv() D.inv2(X!) 27 flop
Square X.sqr() D.sqr2(X!) 18 flop
Square root X.sqrt() D.sqrt2(X!) ~30 flop
Exponential fn. X.exp() D.exp2(X!) 1264 flop
Natural logarithm X.ln() D.ln2(X!) ~1264+60 flop
Hyperbolic sine X.sinh() D.sinh2(X!) 1264+53 flop
Hyperbolic cosine X.cosh() D.cosh2(X!) 1264+53 flop
Equals X.eq(Y) D.eq22(X, Y)
-//- -//- D.eq21(X, f)
Not equal X.ne(Y) D.ne22(X, Y)
-//- -//- D.ne21(X, f)
Greater than X.gt(Y) D.gt22(X, Y)
-//- -//- D.gt21(X, f)
Greater or equal X.ge(Y) D.ge22(X, Y)
-//- -//- D.ge21(X, f)
Less than X.lt(Y) D.lt22(X, Y)
-//- -//- D.lt21(X, f)
Less or equal X.le(Y) D.le22(X, Y)
-//- -//- D.le21(X, f)

Also you can find example sandbox, tests and benchmark.

Clone this wiki locally