Menu

Square root

Help
2008-03-05
2013-05-30
  • Nobody/Anonymous

    Hi,
    I dont see any function in GCBASIC that allows us to do square root or sth...
    Any one noe?
    Thanx
    :)

     
    • Hugh Considine

      Hugh Considine - 2008-03-08

      There isn't a function in GCBASIC for square root.

      If you have a look at http://en.wikipedia.org/wiki/Methods_of_computing_square_roots there is bound to be something you can use. There is a C algorithm in section 5.2 Binary (base 2) that I've translated to GCBASIC below. I've not tried it, but it should work:

      Function sqrt(num As Word) As Word
          Dim op As Word
          Dim one As Word
         
          op = num
          sqrt = 0
          one = 0
          Set one.14 On 'The second-to-top bit is set
         
          '"one" starts at the highest power of four <= the argument.
          Do while one > op
              Set Status.C Off
              Rotate one Right
              Set Status.C Off
              Rotate one Right
          Loop
         
          Do while one <> 0
              if op >= sqrt + one Then
                  op -= sqrt + one
                  sqrt += 2 * one
              End If
             
              Set Status.C Off
              Rotate sqrt Right
              Set Status.C Off
              Rotate one Right
              Set Status.C Off
              Rotate one Right
          Loop
         
      End Function

      If that doesn't work, you could also try looking for something written for PICs in assembly. Assembly code can be inserted directly in a GCBASIC subroutine and will compile fine, as long as you declare any variables used.

       
  • marco elther

    marco elther - 2013-05-25

    hello cholmondeley
    i tried, your code works fine but i get just
    an integer..

    it seems no float math in gcbasic

    i understand that in 8 bit pics is not
    an easy task… 2 float digits should be
    sufficient for many apps … (ex. pi = 3.14)

    i'd like to have logarithms too… maybe
    ask too much ? ;-)

     

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.