Menu

Square Root Function for GCB

Anonymous
2014-04-19
2014-05-02
  • Anonymous

    Anonymous - 2014-04-19

    Here's a square root routine for GCB. It only involves bit shifting, addition and subtraction, which makes it pretty fast and efficient. It expects a word in and a word out, and will handle arguments of up to 4294, which should cover most applications.

    More importantly, the results are accurate to 2 decimal places, which again should handle most situations.

    Here is the code, below, for a demo program using it. If you don't feel like dragging out an LCD, just run it on a simulator to see what it can do.

    I'll attach the include file in the next post, since we don't seem to be able to do attachments otherwise.

    If tested and confirmed by others, this could perhaps make a nice addition to the MATHS.H file.

    ;Demo: Show the first 100 square roots to 2 decimal places.
    ;This uses the Sqrt.h include file.
    
    ;----- Configuration
    
    #chip 16F88, 8                  ;PIC16F88 running at 8 MHz
    #config mclr=off                ;reset handled internally
    #config osc=int                 ;use internal clock
    #include "Sqrt.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 length as byte
    dim i as word
    dim valStr, outStr as string
    
    ;----- Program
    
    dir PortB out                   ;all outputs to the LCD
    
    for i = 0 to 100                ;print first 100 square roots
      cls
      print "sqrt("
      print i
      print ")="
    
      valStr = str(sqrt(i))         ;format decimal nicely
      length = len(valStr)
    
      select case length
       case 1:
          outStr = "0.00"           ;zero case
       case 3:
          outStr = left(valStr,1)+ "."+right(valStr,2)
        case 4:
          outStr = left(valStr,2)+ "."+right(valStr,2)
        case 5:
          outStr = left(valStr,3)+ "."+right(valStr,2)
      end select      
    
      print outStr                  ;display results
      wait 2 S
    next i
    
     
  • Anonymous

    Anonymous - 2014-04-19

    And here is the square root include file.

     
  • MBB

    MBB - 2014-04-30

    Thomas,

    I finally got around to trying your square root code. It worked great!

    I have two questions:

    1. What limits the maximum value to 4294?

    2. In your square root Function, you have this statement:
      "set sqrt_bitFlag.30 on"

    What does the ".30" refer to?

     
  • Anonymous

    Anonymous - 2014-05-01

    Hello MBB,

    Thanks for the report.

    1. The input value is scaled up by a million to provide three places of accuracy. (After rounding, this falls back to two places in the final result). 4294 times a million is the limit for the Long data type.

    2. Bit 30 of the variable sqrt_bitFlag is set On.

     
  • MBB

    MBB - 2014-05-02

    Thanks for clarifying this.

     

Log in to post a comment.