Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify logarithm discussion #173

Merged
merged 2 commits into from
Mar 8, 2025

Conversation

keiravillekode
Copy link
Contributor

[no important files changed]

@keiravillekode
Copy link
Contributor Author

https://www.efunda.com/math/taylor_series/logarithmic.cfm shows where various series converge.

When people say the Taylor series for log of 1 + z converges for z between +/- 1, that is equivalent to saying the Taylor series for log of x converges for x between 0 and 2. To see this, set x = 1 + z

@keiravillekode
Copy link
Contributor Author

keiravillekode commented Mar 6, 2025

In our instructions, log always refers to the natural (base e) logarithm. There is also a common convention where ln means the natural logarithm and log means the base 10 logarithm, but our instructions are referring to natural logarithms throughout and therefore are not using that convention.

Using log for natural logarithm is certainly common:

If we were interested in base 10 logarithms, the formula at the very top of our instructions could be written as
x ^ y = 10 ^ (y * log10(x))

But this is not what we are interested in.

Another option would be for our instructions to show ln instead of log

[no important files changed]
@keiravillekode
Copy link
Contributor Author

I missed the original point in the part where you switched to using ln

Now updated with a different way to compute logarithms for larger x

@keiravillekode
Copy link
Contributor Author

Changed to use ln

The part I removed gave 10 special status, when that is not necessary. For example, why are we not choosing 2 or exp(1), i.e. e.

If we have a value larger than 1, we can always take the reciprocal to give a value less than 1. I think this is more elegant.

#! /usr/bin/env python3

import math

numbers = [0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000]

def bilinear(z):
    n = 1
    term = (z - 1) / (z + 1)
    ratio = term * term
    previous = 0
    total = term
    while previous != total:
        previous = total
        term = ratio * term
        n += 2
        total += term / n
    return 2 * total

def taylor(x):
    if x > 1:
        return - taylor(1 / x)
    z = x - 1
    n = 1
    power = z
    previous = 0
    total = power
    while previous != total:
        previous = total
        power *= -z
        n += 1
        total += power / n
    return total

for number in numbers:
    actual = math.log(number)
    b = bilinear(number)
    t = taylor(number)
    print(f"{number}: actual {actual}, bilinear {b}, taylor {t}")

@keiravillekode keiravillekode merged commit 047e2f5 into exercism:main Mar 8, 2025
2 checks passed
@keiravillekode keiravillekode deleted the rational-log branch March 8, 2025 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants