[Pkg-javascript-commits] [science.js] 36/87: Add science.expm1.

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:56 UTC 2016


This is an automated email from the git hooks/post-receive script.

bhuvan-guest pushed a commit to branch master
in repository science.js.

commit 7ca8a3ae576e6bd675b2fc58c67b1eef4b4551f3
Author: Jason Davies <jason at jasondavies.com>
Date:   Sat Aug 27 22:16:22 2011 +0100

    Add science.expm1.
---
 Makefile                |  1 +
 science.js              |  4 ++++
 science.min.js          |  2 +-
 src/core/expm1.js       |  4 ++++
 test/core/expm1-test.js | 21 +++++++++++++++++++++
 test/env-assert.js      | 22 ++++++++++++++++++++++
 6 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 03f41c4..7678670 100644
--- a/Makefile
+++ b/Makefile
@@ -18,6 +18,7 @@ all: \
 
 science.core.js: \
 	src/core/core.js \
+	src/core/expm1.js \
 	src/core/functor.js \
 	src/core/hypot.js \
 	src/core/zeroes.js
diff --git a/science.js b/science.js
index 058aa85..e760260 100644
--- a/science.js
+++ b/science.js
@@ -1,4 +1,8 @@
 (function(){science = {version: "1.5.0"}; // semver
+// Compute exp(x) - 1 accurately for small x.
+science.expm1 = function(x) {
+  return (x < 1e-5 && x > -1e-5) ? x + .5 * x * x : Math.exp(x) - 1;
+};
 science.functor = function(v) {
   return typeof v === "function" ? v : function() { return v; };
 };
diff --git a/science.min.js b/science.min.js
index a68af5b..b4edbed 100644
--- a/science.min.js
+++ b/science.min.js
@@ -1 +1 @@
-(function(){science={version:"1.5.0"},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.zeroes=function(a){var b=-1,c=[];if(arguments.length===1)while(++b<a)c[b]=0;else while(++b<a)c[b]=science.zeroes.apply(this,Array.prototype.slice.call(arguments,1));return c},science.vector={},science.vector.cross=function(a,b){return[a[1]*b [...]
\ No newline at end of file
+(function(){science={version:"1.5.0"},science.expm1=function(a){return a<1e-5&&a>-0.00001?a+.5*a*a:Math.exp(a)-1},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.zeroes=function(a){var b=-1,c=[];if(arguments.length===1)while(++b<a)c[b]=0;else while(++b<a)c[b]=science.zeroes.apply(this,Array.prototype.slice.call(arguments,1)); [...]
\ No newline at end of file
diff --git a/src/core/expm1.js b/src/core/expm1.js
new file mode 100644
index 0000000..5e6d4b4
--- /dev/null
+++ b/src/core/expm1.js
@@ -0,0 +1,4 @@
+// Compute exp(x) - 1 accurately for small x.
+science.expm1 = function(x) {
+  return (x < 1e-5 && x > -1e-5) ? x + .5 * x * x : Math.exp(x) - 1;
+};
diff --git a/test/core/expm1-test.js b/test/core/expm1-test.js
new file mode 100644
index 0000000..07affb3
--- /dev/null
+++ b/test/core/expm1-test.js
@@ -0,0 +1,21 @@
+require("../../science");
+
+var vows = require("vows"),
+    assert = require("assert");
+
+var suite = vows.describe("science.expm1");
+
+suite.addBatch({
+  "expm1": {
+    "simple": function() {
+      var expm1 = science.expm1;
+      assert.inDelta(expm1(-1  ),       -0.632120558828558,       1e-6);
+      assert.inDelta(expm1( 0  ),        0,                       1e-6);
+      assert.inDelta(expm1(1e-5 - 1e-8), 0.000009990049900216168, 1e-6);
+      assert.inDelta(expm1(1e-5 + 1e-8), 0.000010010050100217178, 1e-6);
+      assert.inDelta(expm1( 0.5),        0.6487212707001282,      1e-6);
+    }
+  }
+});
+
+suite.export(module);
diff --git a/test/env-assert.js b/test/env-assert.js
new file mode 100644
index 0000000..a3932cb
--- /dev/null
+++ b/test/env-assert.js
@@ -0,0 +1,22 @@
+var assert = require("assert");
+
+assert.inDelta = function(actual, expected, delta, message) {
+  if (!inDelta(actual, expected, delta)) {
+    assert.fail(actual, expected, message || "expected {actual} to be in within *" + delta + "* of {expected}", null, assert.inDelta);
+  }
+};
+
+function inDelta(actual, expected, delta) {
+  return (Array.isArray(expected) ? inDeltaArray : inDeltaNumber)(actual, expected, delta);
+}
+
+function inDeltaArray(actual, expected, delta) {
+  var n = expected.length, i = -1;
+  if (actual.length !== n) return false;
+  while (++i < n) if (!inDelta(actual[i], expected[i], delta)) return false;
+  return true;
+}
+
+function inDeltaNumber(actual, expected, delta) {
+  return actual >= expected - delta && actual <= expected + delta;
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/science.js.git



More information about the Pkg-javascript-commits mailing list