summaryrefslogtreecommitdiff
path: root/node_modules/webworker-threads/examples/ex06_complex.js
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-04-04 01:00:59 -0700
committeryo mama <pepper@scannerjammer.com>2015-04-04 01:00:59 -0700
commitc7c22e3db1c826bcfb2bc66651ec480aae0d4ae0 (patch)
tree8546df448afef40d3814d2581f4dacff7cebb87f /node_modules/webworker-threads/examples/ex06_complex.js
Diffstat (limited to 'node_modules/webworker-threads/examples/ex06_complex.js')
-rw-r--r--node_modules/webworker-threads/examples/ex06_complex.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/node_modules/webworker-threads/examples/ex06_complex.js b/node_modules/webworker-threads/examples/ex06_complex.js
new file mode 100644
index 0000000..d8d71b4
--- /dev/null
+++ b/node_modules/webworker-threads/examples/ex06_complex.js
@@ -0,0 +1,39 @@
+/// A very simple class for complex numbers
+
+function Complex (x, y) {
+ this.x = x;
+ this.y = y;
+ this.add= complex_add;
+ this.mul= complex_mul;
+ this.toString= complex_toString;
+}
+function complex_toString () {
+ return this.x + (this.y > 0 ? " + " + this.y + "i" : this.y < 0 ? " - " + -this.y + "i" : "");
+}
+function complex_add (c) {
+ return new Complex(this.x + c.x, this.y + c.y);
+}
+function complex_mul (c) {
+ return new Complex(this.x * c.x - this.y * c.y, this.x + c.y + this.y * c.x);
+}
+
+/// A class to represent a degree 2 polynomial equation with real coefficients.
+
+function Equation (a, b, c) {
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ this.toString= equation_toString;
+ this.solve= equation_solve;
+}
+function equation_toString () {
+ return this.a + "x^2 + " + this.b + "x + " + this.c
+}
+function equation_solve () {
+ var a = this.a, b = this.b, c = this.c
+ var det = b * b - 4 * a * c;
+ var sdet = Math.sqrt(Math.abs(det));
+ var _2a = 2 * a;
+ if (det >= 0) return [new Complex((-b - sdet) / _2a, 0), new Complex((-b + sdet) / _2a, 0)];
+ else return [new Complex(-b / _2a, -sdet / _2a), new Complex(-b / _2a, sdet / _2a)];
+} \ No newline at end of file