[Pkg-javascript-commits] [science.js] 02/87: Add science.hypot (with overflow protection).

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:52 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 a86e4b9a306835400a19da8b3989da84ab591bcf
Author: Jason Davies <jason at jasondavies.com>
Date:   Thu Jul 14 17:09:19 2011 +0100

    Add science.hypot (with overflow protection).
---
 Makefile             |  4 +++-
 science.js           | 12 ++++++++++++
 science.min.js       |  2 +-
 src/core/hypot.js    | 12 ++++++++++++
 tests/test-hypot.js  |  5 +++++
 tests/test-hypot.out |  1 +
 6 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 5bae3c0..9115db3 100644
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,8 @@ all: \
 
 science.core.js: \
 	src/core/core.js \
-	src/core/functor.js
+	src/core/functor.js \
+	src/core/hypot.js
 
 science.stats.js: \
 	src/start.js \
@@ -31,6 +32,7 @@ science.stats.js: \
 	src/end.js
 
 tests: \
+	tests/test-hypot.test \
 	tests/test-stats-bandwidth.test \
 	tests/test-stats-iqr.test \
 	tests/test-stats-mean.test \
diff --git a/science.js b/science.js
index b873840..e01318f 100644
--- a/science.js
+++ b/science.js
@@ -2,4 +2,16 @@
 science.functor = function(v) {
   return typeof v === "function" ? v : function() { return v; };
 };
+// Based on:
+// http://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/
+science.hypot = function(x, y) {
+  x = Math.abs(x);
+  y = Math.abs(y);
+  var max,
+      min;
+  if (x > y) { max = x; min = y; }
+  else       { max = y; min = x; }
+  var r = min / max;
+  return max * Math.sqrt(1 + r * r);
+}
 })()
\ No newline at end of file
diff --git a/science.min.js b/science.min.js
index 6ac6a1a..4c7ab43 100644
--- a/science.min.js
+++ b/science.min.js
@@ -1 +1 @@
-(function(){science={version:"0.0.2"},science.functor=function(a){return typeof a=="function"?a:function(){return a}}})()
\ No newline at end of file
+(function(){science={version:"0.0.2"},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)}})()
\ No newline at end of file
diff --git a/src/core/hypot.js b/src/core/hypot.js
new file mode 100644
index 0000000..7ab3cc2
--- /dev/null
+++ b/src/core/hypot.js
@@ -0,0 +1,12 @@
+// Based on:
+// http://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/
+science.hypot = function(x, y) {
+  x = Math.abs(x);
+  y = Math.abs(y);
+  var max,
+      min;
+  if (x > y) { max = x; min = y; }
+  else       { max = y; min = x; }
+  var r = min / max;
+  return max * Math.sqrt(1 + r * r);
+}
diff --git a/tests/test-hypot.js b/tests/test-hypot.js
new file mode 100644
index 0000000..284185e
--- /dev/null
+++ b/tests/test-hypot.js
@@ -0,0 +1,5 @@
+require("./../lib/env-js/envjs/node");
+require("./../science");
+
+var max = Number.MAX_VALUE / Math.sqrt(2);
+console.log("hypot", science.hypot(max, max));
diff --git a/tests/test-hypot.out b/tests/test-hypot.out
new file mode 100644
index 0000000..08e7d5f
--- /dev/null
+++ b/tests/test-hypot.out
@@ -0,0 +1 @@
+hypot 1.7976931348623155e+308

-- 
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