Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tests on sqr
  • Loading branch information
dyoo committed Mar 15, 2010
1 parent 3e9162b commit 6f708ad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
18 changes: 9 additions & 9 deletions src/js-numbers.js
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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();
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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);
};

Expand Down
57 changes: 40 additions & 17 deletions test/tests.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}
});

Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 6f708ad

Please sign in to comment.