[Prev][Next][Index][Thread]

Re: A85: 16-bit integer Square Root (few changes)




The routine below isn't mine, so I would like if you would use my name...
I admit that my square root routine isn't as small as that, but the
routine is faster than 0.7 seconds. (I haven't measured it but you can see
it with your own eyes when you run it.) The worse case is any value that
doesn't have an integer suare root. It rounds down because the "trend"
with integer operations tend to be that, ie. divisions usually round down.

The ultimately fastest way of calculating an integer square root is a
table, in my routine (Earlier in this list) the call MUL would be replaced
by looking the square from a table. If I have the time I'll put such
routine on this list tomorrow and correct the claimed bug. (I haven't had
the time nor the will to program anything lately :)


On Mon, 16 Mar 1998, Erik L Gillespie wrote:


> 
> I keep trying to take the square root of 54564 and each time I try to
> display the answer the number254 is displayed.  Here's what the program
> looks like, can anyone tell me what's wrong: (I made all of the changes
> except pushing and popping hl)
> 
> #include "ti-85.h"
> 
> .org
> .db "SQRT by Jarno Rajala",0
> 
>    ld hl,0
>    ld (CURSOR_ROW),hl
>    ld hl,54564
>    CALL_(Sqrt16)
>    ROM_CALL(D_HL_DECI)
> KeyWait:
>    call GET_KEY
>    or a
>    jr z, KeyWait
>    ret
> Sqrt16:
>   push de
>   push bc
>   ld de, 0
>   ld bc, 0
>   call CP_HL_DE
>   jr z, Sqrt16done  ;0 is a special case.
>   ld de, 65280
>   ld bc, 256
>   sbc hl,de
>   jr nc, Sqrt16done
>   dec bc           ;bc=255
>   rr l
>   rr h         ;shift hl
>   jr nc, noSet
>   set 7, l
> noSet:
>   call Sqrt16loop
> Sqrt16done:
>   ld h, b
>   ld l, c
>   pop bc
>   pop de
>   ret
> Sqrt16loop:
>   add hl, bc
>   ret c
>   dec c
>   jp Sqrt16loop
> .end
> 
> 
> Mordant
> egillespie@juno.com
> erik_gillespie_1096@gwgate.kvcc.edu
> 
> "Just because you're paranoid don't mean they're not after you." -Kurt
> Cobain
> 
> _____________________________________________________________________
> You don't need to buy Internet access to use free Internet e-mail.
> Get completely free e-mail from Juno at http://www.juno.com
> Or call Juno at (800) 654-JUNO [654-5866]
> 


Follow-Ups: References: