Skip to content

Commit

Permalink
Fix issue and add iterative approach for logarithms
Browse files Browse the repository at this point in the history
  • Loading branch information
degrammer committed Jun 25, 2024
1 parent bc991fe commit 9a4846d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
2 changes: 2 additions & 0 deletions logarithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ Run Benchmarks for the different Algorithms via
```js
node logarithms/benchmark.mjs
```

![recursion-iterative.png](recursion-iterative.png)
32 changes: 29 additions & 3 deletions logarithms/benchmark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,41 @@ function loadBenchmark() {

const suite = benchmark.createSuite("calculateMantisseAndCharacteristic")

suite.add("Recursion (1)", () => {
suite.add("Recursion 1", () => {
calculateMantisseAndCharacteristic(1)
})

for (const n of [2, 3, 100, 500, 1000, 10000, 1000000]) {
const operations = [
2,
3,
100,
500,
1000,
10000,
1000000,
1000000 * 2,
1000000 * 3,
]

for (const n of operations) {
suite.ref(`Recursion ${n}`, () => {
calculateMantisseAndCharacteristic(500)
calculateMantisseAndCharacteristic(n)
})
}
// benchmark.run()

suite.add("Iterative 1", () => {
calculateMantisseAndCharacteristic(1, { useRecursion: false })
})

for (const n of operations) {
suite.ref(`Iterative ${n}`, () => {
calculateMantisseAndCharacteristic(n, {
useRecursion: false,
})
})
}

benchmark.run()
}

Expand Down
25 changes: 23 additions & 2 deletions logarithms/parts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ export function calculatePower(n, power) {
return power - 1
}

export function calculatePowerIterative(n) {
let done = false
let power = 1

if (n <= 0) {
return Number.NaN
}

while (!done) {
const powerCheck = n ** power

if (powerCheck < n) {
power += 1
} else {
done = true
}
}

return power
}

export function calculateRemaining(n, power) {
return n / 10 ** power
}
Expand All @@ -83,8 +104,8 @@ export function printCalculation(n) {
return `Log(${n}) = Log(10^${power}*${remaining})`
}

export function calculateMantisseAndCharacteristic(n) {
const power = calculatePower(n, 1)
export function calculateMantisseAndCharacteristic(n, useRecursion = true) {
const power = useRecursion ? calculatePower(n, 1) : calculatePowerIterative(n)
if (Number.isNaN(power)) {
return "Cannot calculate formula for this value for base 10"
}
Expand Down
28 changes: 27 additions & 1 deletion logarithms/parts.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { describe, it } from "node:test"
import assert from "node:assert/strict"
import {
calculateMantisseAndCharacteristic,
calculatePower,
printCalculation,
calculatePowerIteratively,
} from "./parts.mjs"

describe("Logarithms test suite", () => {
Expand Down Expand Up @@ -62,5 +62,31 @@ describe("Logarithms test suite", () => {
assert.strictEqual(result, 2.6989700043360187)
})
})

describe("When using non-recursive approach", () => {
it("For Log(500) ~ 2,69897", () => {
const { mantisse, characteristic, result } =
calculateMantisseAndCharacteristic(500, false)

assert.deepEqual(
{
mantisse,
characteristic,
},
{
mantisse: 0.6989700043360189,
characteristic: 2,
},
)

assert.strictEqual(result, 2.6989700043360187)
})

it("It calculates properly the power", () => {
for (const n of [2, 3, 100, 500, 1000, 10000, 1000000]) {
assert.strictEqual(calculatePowerIteratively(n), 2)
}
})
})
})
})
Binary file added logarithms/recursion-iterative.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9a4846d

Please sign in to comment.