Trigonometry Sine, Cosine and Tangent


    byte_variable = sin( word_variable )

    byte_variable = cos( word_variable )

    byte_variable = tan( word_variable )


Great Cow BASIC supports Three Primary Trigonometric Functions

Great Cow BASIC supports the following functions, sin(x), cos(x), tan(x), where x is a signed integer representing an angle measured in a whole number of degrees. The output values are also integers, represented as fixed point decimal fractions.


The sine, cosine and tangent functions are available for your programs simply by including the header file offering the precision you need.

    #INCLUDE <TRIG2PLACES.H> gives two decimal places
    #INCLUDE <TRIG3PLACES.H> gives three decimal places
    #INCLUDE <TRIG4PLACES.H> gives four decimal places

In fixed point representation, the decimal point is assumed. For example, with two places of accuracy, sin(60) returns 87, which you would interpret as 0.87. With three places, 866 is returned, to be interpreted as 0.866, and so on. Another way of thinking of this is to consider the two-place values as scaled up by 100, the three-place values scaled up by 1000 and the four-place values scaled up by 10,000.

Sine and cosine are always defined, but remember that tangent fails to exist at 90 degrees, 270 degrees and all their coterminal angles. It is the responsibility of the calling program to avoid these special values.

Note that the tangent function is not available to four decimal places, since its value grows so rapidly, exceeding what the Integer data type can represent.

These routines are completely general. The input argument may be positive, negative or zero, with no restriction on the size. Further observe that lookup tables are used, so the routines are very fast, efficient and accurate.

Example:_Show the trigonometric values to three decimal places.

    ;----- Configuration
    #chip 16F88, 8                  ;PIC16F88 running at 8 MHz
    #config mclr=off                ;reset handled internally
    #config osc=int                 ;use internal clock
    #include <Trig3Places.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 integer
    dim outStr, valStr as string

    ;----- Program

    dir PortB out                   ;all outputs to the LCD

    for i = -720 to 720             ;arguments from -720 to 720
      print "sin("                  ;print the label
      print i                       ;and the argument
      print ")="                    ;and closing parenthesis
      locate 1,0
      printTrig(sin(i))             ;print value of the sine
      wait 500 mS                   ;pause to view

      cls                           ;do likewise for cosine
      print "cos("
      print i
      print ")="
      locate 1,0
      wait 500 mS                   ;pause to view
      cls                           ;do likewise for tangent
      print "tan("
      print i
      print ")="
      locate 1,0
      wait 500 mS                   ;pause to view
    next i

    sub printTrig(in value as integer)
      ;print decently formatted trig results

      outStr = ""                   ;assume positive (no sign)

      if value < 0 then             ;handle negatives
        outStr = "-"                ;prefix a minus sign
        value = -1 * value          ;but work with positives
      end if

      valStr = str(value)
      length = len(valStr)
      select case length
        case 1:
          outStr = outStr + "0.00" + valStr
        case 2:
          outStr = outStr + "0.0" + valStr
        case 3:
          outStr = outStr + "0." + valStr
        case 4:
          outStr = outStr + left(valStr,1) + "." + right(valStr,3)
        case 5:
          outStr = outStr + left(valStr,2) + "." + right(valStr,3)
      end select
      print outStr
    end sub