Double.js example

node v10.24.1
version: 2.0.0
endpointsharetweet
let D = require("double.js") // calculating numerical differentiation of function f(x) = 1 / x // in point x = 1e-6 with double.js and native float let f = function(x) { return 1000 / x } let df = function(x) { let dx = 1e-10; return (f(x + dx) - f(x - dx)) / 2 / dx } let F = function(X) { return X.inv(); } let dF = function(X) { let dX = new D(1e-10); return F(X.add(dX)).sub(F(X.sub(dX))).div(2).div(dX); } let df1 = df(1e-6); let df2 = dF(new D(1e-6)).toNumber(); let actual = -1 / 1e-6 / 1e-6; console.log(new D('0.3').sub(new D('0.1')).toNumber()); console.log(new D('0.000000000000002').add(new D('0.000000000000001')).toNumber()); console.log(new D('0.000000000000002').div(new D('0.000000000000003')).toNumber()); console.log('with native float df=' + df1 + ', error = ' + Math.abs(df1-actual)); console.log('with double.js df=' + df2 + ', error = ' + Math.abs(df2-actual));
Loading…

no comments

    sign in to comment