diff options
Diffstat (limited to 'node_modules/webworker-threads/examples/ex06_complex.js')
| -rw-r--r-- | node_modules/webworker-threads/examples/ex06_complex.js | 39 |
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 |
