log10 function


Results 1 to 8 of 8

Thread: log10 function

Threaded View

  1. #8
    dampf's Avatar
    dampf Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks everyone for all of your help.

    Using the integer LOG10 program from http://www.emesys.com/BS2math3.htm, The ln(x) series from http://www.dattalo.com/technical/theory/logs.html and a bunch of log10/ln/log2 relationships I came up with something that I am happy with.

    If anyone wants to use this, feel free. And I am sure some of you smarter folks out there will be able to simplify this greatly. This was fast, dirty, commented poorly, but very rewarding haha.

    To use this you must have the floating point routines installed.

    y = 16bit integer (I left y=12013 in this program for testing purposes)

    there is a loop that goes from 2 to 10. The more times through the loop, the more accurate you get.

    Any questions, feel free to ask.

    Thanks again for all of your help!!

    ' LOG10 using LOG2 and LN
    y var word ' to hold an input number from 1 to 65535
    x var word ' for processing the number
    log var word ' to hold the log base 10
    j var byte ' loop variable
    k var byte ' loop variable
    cc var byte ' characteristic
    z1 var byte[4] ' to store results
    z2 var byte[4] ' to store results

    LOG10:
    ' lg(y) = lg(y/2^cc * (y/(y/2^cc))) = lg(y/2^cc) + lg(y/(y/2^cc))
    ' = cc + ln(y/2^cc) / ln(2) = cc + ln(y/2^cc) * 1.442695

    ' starting value y, will be sum from a to d converter
    y = 12013 ' log10(y) = 4.07965

    cc = ncd y - 1 ' find the characteristic

    aint = 1 ' now find 2^cc
    Gosub itofa ' a holds the results
    bint = 2
    Gosub itofb
    for i = 1 to cc ' multiply by 2 cc times
    gosub fpmul
    next i

    bint = y
    gosub itofb
    ' now barg equals y
    ' swap a and b
    swap AEXP, BEXP
    Swap AARGB0, BARGB0
    Swap AARGB1, BARGB1
    Swap AARGB2, BARGB2

    gosub fpdiv ' a = a/b = y / 2^cc

    ' now for the fun stuff. We know y / 2^cc < 2 and > 0, so
    ' calculate ln(a) where -1 < x < 1 using
    ' ln(1+a) = a - a^2/2 + a^3/3 - a^4/4 + ... - (-a)^n/n
    ' or ln(a) = (a-1) - (a-1)^2/2 + (a-1)^3/3 - (a-1)^4/4 + ... - (-(a-1))^n/n

    bint = 1
    gosub itofb
    gosub fpsub ' a = a - 1
    z2[0] = AEXP
    z2[1] = AARGB0
    z2[2] = AARGB1
    z2[3] = AARGB2
    ' z2 will hold the results

    BEXP = AEXP
    BARGB0 = AARGB0
    BARGB1 = AARGB1
    BARGB2 = AARGB2
    aint = 0
    gosub itofa
    gosub fpsub ' a = - a

    z1[0] = AEXP
    z1[1] = AARGB0
    z1[2] = AARGB1
    z1[3] = AARGB2
    ' z1 = -a
    for j = 2 to 10 ' 10 times is accurate enough for us
    aint = 1
    gosub itofa
    BEXP = z1[0]
    BARGB0 = z1[1]
    BARGB1 = z1[2]
    BARGB2 = z1[3]
    for k = 1 to j
    gosub fpmul ' a = a * b .. a starts as a .. b starts as a
    next k
    bint = j
    gosub itofb
    gosub fpdiv ' a should now = (-(a-1))^n/n
    BEXP = AEXP
    BARGB0 = AARGB0
    BARGB1 = AARGB1
    BARGB2 = AARGB2 ' now b = a
    AEXP = z2[0]
    AARGB0 = z2[1]
    AARGB1 = z2[2]
    AARGB2 = z2[3]
    gosub fpsub ' a = results - b
    z2[0] = AEXP
    z2[1] = AARGB0
    z2[2] = AARGB1
    z2[3] = AARGB2
    next j

    ' now multiply by 1.442695
    AEXP = z2[0]
    AARGB0 = z2[1]
    AARGB1 = z2[2]
    AARGB2 = z2[3]
    BEXP = $7F
    BARGB0 = $38
    BARGB1 = $AA
    BARGB2 = $3B
    gosub fpmul
    bint = cc
    gosub itofb
    gosub fpadd ' a = a + cc = lg(y)

    'log(y) = lg(y) / lg(10) = lg(y) * 0.301029996
    BEXP = $7D
    BARGB0 = $1A
    BARGB1 = $20
    BARGB2 = $9B
    gosub fpmul

    ' results in a
    Serout2 PORTC.6,16468,[hex AEXP, " ", hex AARGB0, " ", hex AARGB1, " ", hex AARGB2, 13, 10]
    pause 500
    return
    Last edited by dampf; - 15th October 2004 at 19:37.

Similar Threads

  1. Single button function
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 40
    Last Post: - 4th April 2020, 19:33
  2. Hash function
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 7th September 2007, 11:09
  3. Making a function to set the DS1337 Alarm in C
    By trw01 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 12th February 2006, 18:45
  4. ATAN2(,) function
    By vk2tds in forum PBP Wish List
    Replies: 0
    Last Post: - 25th November 2005, 03:52
  5. Random function - How to limit it ??
    By martarse in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 30th November 2004, 15:05

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts