screenres 640,480,32
declare function logbx(byval n as double,byval bx as double)as double
dim as integer j,l=100,steps=2
dim as double entropymax1
'output from example 1 and 2 should be the same but is not?
'---------------------------------------------
'example 1
for j=1 to l-steps
entropymax1+=logbx(1/(l-steps),2)*(1/(l-steps))
next j
entropymax1=abs(entropymax1)
print entropymax1
'---------------------------------------------
'example 2
entropymax1=(l-steps)*abs(logbx(1/(l-steps),2)*(1/(l-steps)))
print entropymax1
'---------------------------------------------
sleep
function logbx(byval n as double,byval bx as double)as double
return log(n)/log(bx)
end function
They are the same, though---the same to the 14th decimal place in fact! That's incredibly good for repeated summations of IEEE floating point numbers! I suspect if you did the same calculation by hand you'd get the same results.
Always remember that floating point numbers are binary fractions and thus subject to certain limitations and rounding errors, and also remember that in any kind of calculation like that, significant digits is important. How many significant digits do you need?
I don't think this is a rounding error problem, consider the following code, now example 1 and 2 are the same, but unremark "print t1" from example 1 and notice that the values are different again. How is the "print t1" here affecting the calculation?
I'm not sure how many digits my algorithm needs but something like 12 digits should probably be good.
screenres 640,480,32
declare function logbx(byval n as double,byval bx as double)as double
dim as integer j,l=100,steps=2
dim as double entropymax1
dim as double t1=abs(logbx(1/(l-steps),2)*(1/(l-steps)))
'output from example 1 and 2 should be the same but is not?
'---------------------------------------------
'example 1
for j=1 to l-steps
'print t1 '<----------------------------------------
entropymax1+=t1
next j
entropymax1=abs(entropymax1)
print entropymax1
'---------------------------------------------
'example 2
entropymax1=(l-steps)*abs(logbx(1/(l-steps),2)*(1/(l-steps)))
print entropymax1
'print abs(logbx(1/(l-steps),2)*(1/(l-steps)))
'---------------------------------------------
sleep
function logbx(byval n as double,byval bx as double)as double
return log(n)/log(bx)
end function
Here is the gmp answer as a check:
6.6147098441152082148839386344636616172820532519322815673545834481406416977243859729957219983404215702147210037786511460
i.e.
(l-steps)*abs(logbx(1/(l-steps),2)*(1/(l-steps)))
and the loop:
dim as string t2=mult(d,logbx(d,"2"))
for j=1 to l-steps
entropymax1=plus(entropymax1,t2)
next j
entropymax1=absolute(entropymax1)
print entropymax1
6.6147098441152082148839386344636616172820532519322815673545834481406416977243859729957219983404215702147210037786511460