Let me begin by saying that I am a lousy programmer and I am hacking away at something that interests me. I havent used these forums before so I hope the code I post doesnt get corrupted. I am working on a logarithm solver using a method explained by napier and euler. Starting with the numbers 1 and 10 the logs base 10 are 0 and 1. The arithmetic mean of the logs (0 + 1)/2 = 0.5 is the log of the geometric mean of the the two numbers math.sqrt(1*10) = value
Suppose my target number is log(5)
The code below works for the first pass but I want to find a way to pass
the indexes as variables to the gmean function via a loop and probably
exit with something like if (target - lastvalue) < 0.00000005
#!/usr/local/bin/python3.0
import math
a = 1.0
b = 10.0
target = 5.0
def gmean(x,y) :
return math.sqrt(x*y)
mylist = []
mylist.append(a)
mylist.append(b)
n = len(mylist)
np = n - 1
npp = n - 2
print ("index var",np, npp)
ta = float(mylist[npp])
tb = float(mylist[np])
print (ta, tb)
tc = gmean(ta,tb)
mylist.append(tc)
print (mylist)
if (target < tc < tb) :
td = gmean(ta, tc)
print ("from upper",td)
mylist.append(td)
print (mylist)
elif (ta < tc < target) :
td = gmean(tc, tb)
print ("from lower",td)
mylist.append(td)
print (mylist)
else:
print("comp failed")