Created
January 8, 2015 22:26
-
-
Save soundlake/67b6a6e1373ac7664040 to your computer and use it in GitHub Desktop.
Calculate log 3 base 2 using Euler Method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
step = 0.1 | |
########## calculate log(2) | |
''' | |
log(1.1) ~= log(1.0) + log'(1.05) * 0.1 | |
log(1.2) ~= log(1.1) + log'(1.15) * 0.1 | |
... | |
log(2.0) ~= log(1.9) + log'(1.95) * 0.1 | |
''' | |
arg_prv = 1 | |
val_prv = 0 | |
while arg_prv < 2: | |
arg_nxt = arg_prv + step | |
arg_mid = (arg_prv + arg_nxt) / 2 | |
val_nxt = val_prv + step / arg_mid # log'(x) == 1/x | |
arg_prv = arg_nxt | |
val_prv = val_nxt | |
log2 = val_nxt | |
print('log 2 :', log2) # 0.6928353604099601 | |
print('math.log(2):', math.log(2)) # 0.6931471805599453 | |
print('defference :', abs(log2 - math.log(2))) # 0.00031182014998520913 | |
print('') | |
########## calculate log(3) | |
''' | |
log(2.1) ~= log(2.0) + log'(2.05) * 0.1 | |
log(2.2) ~= log(2.1) + log'(2.15) * 0.1 | |
... | |
log(3.0) ~= log(2.9) + log'(2.95) * 0.1 | |
''' | |
while arg_prv < 3: | |
arg_nxt = arg_prv + step | |
arg_mid = (arg_prv + arg_nxt) / 2 | |
val_nxt = val_prv + step / arg_mid # log'(x) == 1/x | |
arg_prv = arg_nxt | |
val_prv = val_nxt | |
log3 = val_nxt | |
print('log 3 :', log3) # 1.098242634663974 | |
print('math.log(3):', math.log(3)) # 1.0986122886681098 | |
print('defference :', abs(log3 - math.log(3))) # 0.00036965400413579985 | |
print('') | |
########## calculate log 3 base 2 | |
''' | |
log 3 base 2 == log(3) / log(2) | |
''' | |
log32 = log3 / log2 | |
print('log 3 base 2 :', log32) # 1.585142297030176 | |
print('math.log(3,2):', math.log(3, 2)) # 1.5849625007211563 | |
print('defference :', abs(log32 - math.log(3, 2)))# 0.00017979630901976407 | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment