Menu

Logarithms for GCB

Anonymous
2014-05-01
2014-05-01
  • Anonymous

    Anonymous - 2014-05-01

    Hello all,

    I know I owe several of you responses, and thanks for being patient. I have just put in the most grueling two weeks working up the newest contribution, logarithms for Great Cow Basic.

    I'm not looking for sympathy or slaps on the back, but let me say that this has been the singlemost difficult GCB project I've ever undertaken. The mathematics is trivial, but the side-effects in the actual implementation have driven me crazy for some fourteen very full days now. Anyway, I believe it's done, and would appreciate feedback.

    Here's what we've got: base-2, base-e and base-10 logarithms, to two decimal places. Approximating transcendental functions like these always requires trade-offs, typically speed versus accuracy or speed versus memory usage. I tried to balance all three, giving a slight emphasis to accuracy. The results are very usable: all three logarithms are accurate to two decimal places, plus or minus 0.01 (1 one-hundredth) worst case, and that only happens very infrequently due to the table method and linear interpolation I used. This should be great for dealing with logarithmic phenomena like light, sound and pH.

    For all three functions, the inputs are words and the outputs are words. As usual, the source code for the include file is heavily commented with lots of descriptions.

    The three functions are log2(x), loge(x) and log10(x). I mentioned side-effects above. They drove me nuts! I wanted to call loge(x) its usual name, ln(x), but for whatever reason GCB won't allow it. I wasted over 24 hours in continuous testing before I figured that out. It's a good thing the compiler doesn't wear out from over-usage...Hugh, is "ln" some sort of reserved word? (That's an "ell-enn.")

    Anyway, we'll call it loge(x) for now. Here's a demo program you can try. As usual, I'll attach the include file in the next posting.

    ;Demo: Print common logarithms to two decimal places
    ;Thomas Henry -- 5/1/2014
    
    ;----- Configuration
    
    #chip 16F88, 8                  ;PIC16F88 running at 8 MHz
    #config mclr=off                ;reset handled internally
    #config osc=int                 ;use internal clock
    #include "Logarithms.h"
    
    ;----- Constants
    
    #define LCD_IO      4           ;4-bit mode
    #define LCD_RS      PortB.2     ;pin 8 is LCD Register Select
    #define LCD_Enable  PortB.3     ;pin 9 is LCD Enable
    #define LCD_DB4     PortB.4     ;DB4 on pin 10
    #define LCD_DB5     PortB.5     ;DB5 on pin 11
    #define LCD_DB6     PortB.6     ;DB6 on pin 12
    #define LCD_DB7     PortB.7     ;DB7 on pin 13
    #define LCD_NO_RW   1           ;ground the RW line on LCD
    
    ;----- Variables
    
    dim i as word
    
    ;----- Program
    
    dir PortB out                   ;all outputs to the LCD
    
    for i = 1 to 65535      
      cls
      print "log10("
      print i                       ;print argument
      print ")="
      locate 1,0
      print2Places(log10(i))        ;and its logarithm
      wait 2 S
    next   
    
    ;----- Subroutines
    
    sub print2places(in p2p_arg as word)
      ;print decently formatted results, to 2 decimal places
      dim valStr, outStr as string
    
      valStr = str(p2p_arg)
      select case len(valStr)
        case 1:
          outStr = "0.0" + valStr
        case 2:
          outStr = "0." + valStr
        case 3:
          outStr = left(valStr,1) + "." + right(valStr,2)
        case 4:
          outStr = left(valStr,2) + "." + right(valStr,2)      
      end select    
      print outStr
    end sub
    
     

    Last edit: Anonymous 2014-05-01
  • Anonymous

    Anonymous - 2014-05-01

    Here is the include file for the logarithm functions, base 2, base e and base 10.

     
  • Anobium

    Anobium - 2014-05-01

    OK. No sympathy but well done - a slap on the back! Very good.

    We need to pull this all together into MATHS.H!!!

     
  • Anonymous

    Anonymous - 2014-05-01

    Sounds good. Have you noticed that the sin(), cos(), tan(), sqrt(), log2(), loge() and log10() functions I've written all return values to two decimal places? This is on purpose. With the addition of the print routines that format the results with decimal points (see the various sample demo programs), we're starting to get a pretty good consistent set of fixed point decimal routines.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.