Logarithm subroutine

Janke
Posts: 29
Joined: Mon Feb 20, 2017 11:48 am

Logarithm subroutine

Unread post by Janke »

I'm working on a color light meter (using a "TCS230/ TCS3200 Color Recognition Sensor" from eBay), and need to calculate some logarithms...

The EzSBC1 doesn't have a log function, but I found an old, classic BASIC subroutine, here modified for the EzSBC1:

---------------------------------

' Calculate the logarithm (base 10) of X with 6 significant digits

FOR X = 1 TO 20

GOSUB calculate_log_x:

PRINT "x = ",X,TAB(10),"log(x) = ",Log_x

NEXT X

END

' ---------------------------------------------

calculate_log_x:

' input: X
' output: Log_x
' uses variables E, L, Y

E=0
Y=X

WHILE Y>=1
Y=0.5*Y
E=E+1
WEND

WHILE Y<0.5
Y=2*Y
E=E-1
WEND

Y = (Y-0.7071068)/(Y+0.7071068)
L = Y*Y
Log_x = INT((((0.598979*L+0.961471)*L+2.88539)*Y+E-0.5)*0.30103E6+0.5)/1E6

RETURN
Post Reply