diff --git a/src/js-numbers.js b/src/js-numbers.js index c4aab81..4121ef8 100644 --- a/src/js-numbers.js +++ b/src/js-numbers.js @@ -90,9 +90,9 @@ if (! this['plt']['lib']['Numbers']) { case 1: // Rational return new Rational(x, 1); case 2: // FloatPoint - return FloatPoint.makeInstance(x); + return new FloatPoint(x); case 3: // Complex - return Complex.makeInstance(x, 0); + return new Complex(x, 0); default: return throwRuntimeError("IMPOSSIBLE: cannot lift fixnum integer to " + other.toString()); } @@ -1292,9 +1292,10 @@ if (! this['plt']['lib']['Numbers']) { return "+inf.0"; if (this.n === Number.NEGATIVE_INFINITY) return "-inf.0"; - if (this === NEGATIVE_ZERO) { + if (this === NEGATIVE_ZERO) return "-0.0"; - } + if (this.n === 0) + return "0.0"; return this.n.toString(); }; @@ -1379,7 +1380,7 @@ if (! this['plt']['lib']['Numbers']) { }; FloatPoint.prototype.multiply = function(other) { - if (this.n === 0 || other.n === 0) { return 0; } + if (this.n === 0 || other.n === 0) { return FloatPoint.makeInstance(0.0); } if (this.isFinite() && other.isFinite()) { var product = this.n * other.n; @@ -1607,6 +1608,9 @@ if (! this['plt']['lib']['Numbers']) { if (i === undefined) { i = 0; } if (typeof(r) === 'number') { r = fromFixnum(r); } if (typeof(i) === 'number') { i = fromFixnum(i); } + if (isExact(i) && isInteger(i) && _integerIsZero(i)) { + return r; + } return new Complex(r, i); }; @@ -1736,16 +1740,12 @@ if (! this['plt']['lib']['Numbers']) { multiply(this.r, other.r), multiply(this.i, other.r)); } - var r = subtract( multiply(this.r, other.r), multiply(this.i, other.i)); var i = add( multiply(this.r, other.i), multiply(this.i, other.r)); - if (equals(i, 0)) { - return r; - } return Complex.makeInstance(r, i); }; diff --git a/test/tests.js b/test/tests.js index 80bccc1..ce57f41 100644 --- a/test/tests.js +++ b/test/tests.js @@ -285,18 +285,18 @@ describe('equals', { assertFalse(equals(makeBignum("1.1e2000"), makeComplex(makeBignum("1e2000"), 0))); - assertTrue(equals(makeBignum("0"), - makeComplex(0, 0))); - assertTrue(equals(makeBignum("91326"), - makeComplex(makeBignum("91326")))); - assertFalse(equals(makeBignum("00000"), - makeComplex(makeBignum("91326")))); - assertTrue(equals(makeBignum("90210"), - makeComplex(makeFloat(90210)))); - assertTrue(equals(makeBignum("90210"), - makeComplex(makeFloat(90210), makeFloat(0)))); - assertFalse(equals(makeBignum("90210"), - makeComplex(makeFloat(90210), makeFloat(0.1)))); + assertTrue(equals(makeBignum("0"), + makeComplex(0, 0))); + assertTrue(equals(makeBignum("91326"), + makeComplex(makeBignum("91326")))); + assertFalse(equals(makeBignum("00000"), + makeComplex(makeBignum("91326")))); + assertTrue(equals(makeBignum("90210"), + makeComplex(makeFloat(90210)))); + assertTrue(equals(makeBignum("90210"), + makeComplex(makeFloat(90210), makeFloat(0)))); + assertFalse(equals(makeBignum("90210"), + makeComplex(makeFloat(90210), makeFloat(0.1)))); }, 'fixnum / rational': function() { @@ -2245,13 +2245,35 @@ describe('sqr', { }, 'rationals': function() { - // FIXME: we're missing this + assertTrue(eqv(sqr(makeRational(1, 2)), + makeRational(1, 4))); + + assertTrue(eqv(sqr(makeRational(-1, 7)), + makeRational(1, 49))); + assertTrue(eqv(sqr(makeRational(makeBignum("-1297684398542133568912839"), + 5)), + makeRational(makeBignum("1683984798219658952314406790914015952992379039921"), + 25))); + }, 'floats': function() { - // FIXME: we're missing this + assertTrue(eqv(sqr(makeFloat(.25)), + makeFloat(0.0625))); + assertTrue(eqv(sqr(makeFloat(-.25)), + makeFloat(0.0625))); + + + assertTrue(eqv(sqr(nan), nan)); + assertTrue(eqv(sqr(inf), inf)); + assertTrue(eqv(sqr(negative_inf), inf)); + assertTrue(eqv(sqr(negative_zero), makeFloat(0.0))); }, + 'complex': function() { - // FIXME: we're missing this + assertTrue(eqv(sqr(negative_i), + -1)); + assertTrue(eqv(sqr(i), + -1)); } }); @@ -2408,6 +2430,7 @@ describe('toString', { makeBignum("239856325892398441"))); }, 'floats': function() { + assertEquals('0.0', makeFloat(0).toString()); assertEquals('0.25', makeFloat(0.25).toString()); assertEquals('1.2354e+200', makeFloat(1.2354e200).toString()); assertEquals('1.2354e-200', makeFloat(1.2354e-200).toString()); @@ -2420,8 +2443,8 @@ describe('toString', { }, 'complex': function() { - assertEquals("1+0i", makeComplex(1, 0).toString()); - assertEquals("-1+0i", makeComplex(-1, 0).toString()); + assertEquals("1+0.0i", makeComplex(1, makeFloat(0)).toString()); + assertEquals("-1+0.0i", makeComplex(-1, makeFloat(0)).toString()); assertEquals("0+1i", makeComplex(0, 1).toString()); assertEquals("0-1i", makeComplex(0, -1).toString()); assertEquals("0-0.0i", makeComplex(0, negative_zero).toString());