Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixing FloatPoint.prototype.integerSqrt

  • Loading branch information...
commit 5b15b1d412d25e52ca27d5341694994c19603a0c 1 parent cf1ad13
Danny Yoo authored
Showing with 16 additions and 11 deletions.
  1. +10 −6 src/js-numbers.js
  2. +6 −5 test/tests.js
16 src/js-numbers.js
View
@@ -684,6 +684,10 @@ if (typeof(exports) !== 'undefined') {
// integerSqrt: scheme-number -> scheme-number
var integerSqrt = function(x) {
+ if (! isInteger(x)) {
+ throwRuntimeError('integer-sqrt: the argument ' + x.toString() +
+ " is not an integer.", x);
+ }
if (typeof (x) === 'number') {
if(x < 0) {
return Complex.makeInstance(0,
@@ -692,7 +696,6 @@ if (typeof(exports) !== 'undefined') {
return Math.floor(Math.sqrt(x));
}
}
-
return x.integerSqrt();
};
@@ -1869,14 +1872,15 @@ if (typeof(exports) !== 'undefined') {
FloatPoint.prototype.integerSqrt = function() {
+ if (this === NEGATIVE_ZERO) { return this; }
if (isInteger(this)) {
if(this.n >= 0) {
- return FloatPoint.makeInstance(floor(sqrt(this.n)));
- }else {
- return Complex.makeInstance(0,
- FloatPoint.makeInstance(floor(sqrt(-this.n))))
+ return FloatPoint.makeInstance(Math.floor(Math.sqrt(this.n)));
+ } else {
+ return Complex.makeInstance(
+ INEXACT_ZERO,
+ FloatPoint.makeInstance(Math.floor(Math.sqrt(-this.n))));
}
-
} else {
throwRuntimeError("integerSqrt: can only be applied to an integer", this);
}
11 test/tests.js
View
@@ -3069,7 +3069,7 @@ describe('integerSqrt', {
assertFails(function() { integerSqrt(makeRational(makeBignum("1"),makeBignum("4"))) });
assertFails(function() { integerSqrt(makeRational(makeBignum("1"),makeBignum("5"))) });
assertFails(function() { integerSqrt(makeRational(makeBignum("5"),makeBignum("2"))) });
-
+
assertTrue(eqv(makeBignum("0"),
integerSqrt(makeRational(makeBignum("0"),makeBignum("-1")))));
assertTrue(eqv(makeBignum("2"),
@@ -3092,15 +3092,16 @@ describe('integerSqrt', {
assertFails(function() { integerSqrt(makeFloat(-0.1))});
assertFails(function() { integerSqrt(makeFloat(9999999999.000001))});
assertFails(function() { integerSqrt(makeFloat(negative_zero))});
-
assertTrue(eqv(makeFloat(0.0),
integerSqrt(makeFloat(0.0))));
- assertTrue(eqv(makeFloat(1.0),
+ assertTrue(eqv(makeFloat(1.0),
integerSqrt(makeFloat(1.0))));
assertTrue(eqv(makeFloat(111.0),
- integerSqrt(makeFloat(12345.0))));
+ integerSqrt(makeFloat(12345.0))));
assertTrue(eqv(makeComplex(0, makeFloat(111)),
- integerSqrt(makeFloat(-12345.0))));
+ integerSqrt(makeFloat(-12345.0))));
+ assertTrue(eqv(integerSqrt(negative_zero),
+ negative_zero));
},
'complex': function() {
Please sign in to comment.
Something went wrong with that request. Please try again.