Double.js example

node v8.17.0
version: 1.1.0
endpointsharetweet
Clone this notebook to play with library
// calculating numerical differentiation of function f(x) = 1 / x // in point x = 1e-6 with double.js and native float let D = require("double.js"); 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('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