3 ms·
Very nice algorithm - no square roots and 1 digit per iteration guaranteed. I converted this into python to understand it better def log10(x): """
by nickcw 3y ago
Very nice algorithm - no square roots and 1 digit per iteration guaranteed.
I converted this into python to understand it better
def log10(x):
"""
Find 10 decimal places of log10(x) using calculator functions only
"""
assert x > 0, "can't log <= 0"
log = 0
while x >= 10:
log += 1
x /= 10
while x < 1:
log -= 1
x *= 10
# x is now [1, 10)
decimal = 0.1
for i in range(10):
x10 = x**10 # or calculate as above so as not to use power function
while x10 >= 10:
log += decimal
x10 /= 10
x = x10
decimal /= 10
print(f"log10(x) = {log:.10f}, x = {x:.10f}")
log10(3)
Which gives
log10(x) = 0.4000000000, x = 5.9049000000
log10(x) = 0.4700000000, x = 5.1537752073
log10(x) = 0.4770000000, x = 1.3220708195
log10(x) = 0.4771000000, x = 1.6313501853
log10(x) = 0.4771200000, x = 1.3349714142
log10(x) = 0.4771210000, x = 1.7977101167
log10(x) = 0.4771212000, x = 3.5253044107
log10(x) = 0.4771212500, x = 2.9646009506
log10(x) = 0.4771212540, x = 5.2439970092
log10(x) = 0.4771212547, x = 1.5726220230
- physPop 3y agoyour print should be x', not x!