Skip to content

Commit

Permalink
Remove square root functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jessealama committed Sep 11, 2023
1 parent 35a173c commit f0d8f69
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 111 deletions.
5 changes: 0 additions & 5 deletions src/decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ function multiplyAndAdd(x: string, y: string, z: string): string {
.toString();
}

function squareRoot(x: string): string {
return new RationalDecimal128(x).squareRoot().toString();
}

export const Decimal128 = {
isInteger: isInteger,
abs: abs,
Expand All @@ -166,5 +162,4 @@ export const Decimal128 = {
divide: divide,
remainder: remainder,
multiplyAndAdd: multiplyAndAdd,
squareRoot: squareRoot,
};
73 changes: 0 additions & 73 deletions src/rationalDecimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -297,53 +297,6 @@ function nextDigit(p: bigint, c: DigitPair, r: bigint): bigint {
return x - 1n;
}

function* nextDigitForSquareRoot(s: string): Generator<Digit> {
let leftDigits = prepareLeftHandSideForSquareRoot(s);
let rightDigits = prepareRightHandSideForSquareRoot(s);
let numDigitsEmitted = 0;

let p: bigint = 0n;
let c = 0n;
let r = 0n;
for (let digitPair of leftDigits) {
let x = nextDigit(p, digitPair, r);
let y = x * (20n * p + x);
let d = parseInt(x.toString()) as Digit;
yield d;
numDigitsEmitted++;
p = 10n * p + x;
c = 100n * r + valueOfDigitPair(digitPair);
r = c - y;
}

yield -1;

for (let digitPair of rightDigits) {
let x = nextDigit(p, digitPair, r);
let y = x * (20n * p + x);
let d = parseInt(x.toString()) as Digit;
yield d;
numDigitsEmitted++;
p = 10n * p + x;
c = 100n * r + valueOfDigitPair(digitPair);
r = c - y;
}

// we may still be able to emit more digits
while (numDigitsEmitted < MAX_SIGNIFICANT_DIGITS + 1) {
let x = nextDigit(p, [0, 0], r);
let y = x * (20n * p + x);
let d = parseInt(x.toString()) as Digit;
yield d;
numDigitsEmitted++;
p = 10n * p + x;
c = 100n * r + valueOfDigitPair([0, 0]);
r = c - y;
}

return;
}

interface Decimal128Constructor {
significand: string;
exponent: bigint;
Expand Down Expand Up @@ -767,30 +720,4 @@ export class RationalDecimal128 {
): RationalDecimal128 {
return this.multiply(x).add(y);
}

squareRoot(): RationalDecimal128 {
if (this.isNegative) {
throw new RangeError(
"Cannot compute square root of negative numbers"
);
}

let digitGenerator = nextDigitForSquareRoot(this.toString());

let digit = digitGenerator.next();
let result = "";

while (!digit.done) {
let v = digit.value;
if (-1 === v) {
result = result + ".";
} else {
result = result + `${v}`;
}

digit = digitGenerator.next();
}

return new RationalDecimal128(result);
}
}
33 changes: 0 additions & 33 deletions tests/sqrt.test.js

This file was deleted.

0 comments on commit f0d8f69

Please sign in to comment.