Skip to content

Commit

Permalink
Fix typo "radicant" -> "radicand"
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Dec 28, 2023
1 parent 22950de commit e08c4aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/approximation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ describe('Convergent calculator', () => {

describe('Radical approximator', () => {
it("finds Ramanujan's approximation to pi", () => {
const {index, radicant} = approximateRadical(Math.PI);
const {index, radicand} = approximateRadical(Math.PI);
expect(index).toBe(4);
expect(radicant.toFraction()).toBe('2143/22');
expect(radicand.toFraction()).toBe('2143/22');
});

it('works with a random value without crashing', () => {
const value = Math.random() * 1000 - 100;
const {index, radicant} = approximateRadical(value);
expect(radicant.valueOf() ** (1 / index) / value).toBeCloseTo(1);
const {index, radicand} = approximateRadical(value);
expect(radicand.valueOf() ** (1 / index) / value).toBeCloseTo(1);
});
});
10 changes: 5 additions & 5 deletions src/approximation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,16 @@ export function continuedFraction(value: number) {
* Approximate a value with a radical expression.
* @param value Value to approximate.
* @param maxIndex Maximum index of the radical. 2 means square root, 3 means cube root, etc.
* @param maxHeight Maximum Benedetti height of the radicant in the approximation.
* @returns Object with index of the radical and the radicant. Result is "index'th root or radicant".
* @param maxHeight Maximum Benedetti height of the radicand in the approximation.
* @returns Object with index of the radical and the radicand. Result is "index'th root or radicand".
*/
export function approximateRadical(
value: number,
maxIndex = 5,
maxHeight = 50000
) {
let index = 1;
let radicant = new Fraction(1);
let radicand = new Fraction(1);
let bestError = Math.abs(value - 1);
for (let i = 1; i <= maxIndex; ++i) {
const cf = continuedFraction(value ** i);
Expand All @@ -339,11 +339,11 @@ export function approximateRadical(
const error = Math.abs(candidate.valueOf() ** (1 / i) - value);
if (error < bestError) {
index = i;
radicant = candidate;
radicand = candidate;
bestError = error;
}
}
}

return {index, radicant};
return {index, radicand};
}

0 comments on commit e08c4aa

Please sign in to comment.