Re: A85: Square roots
[Prev][Next][Index][Thread]
Re: A85: Square roots
Erik L Gillespie wrote:
>
> I believe if you use Newton's method of finding roots you can do this
> easily
>
> If you don't know Newton's method here it is:
> Let X equal the number you want to find the square root of.
> Let TOLERANCE be the accuracy of the root (1.0 * 10 ^ -7)
> When programming, multiply by the inverse, don't divide (it's
> slow)
> Multiply X by 0.5 and let this equal the current value of the
> root (call it ROOT)
> Make a label here.
> ROOT = (ROOT + (X / ROOT)) * 0.5
> Put (ROOT * ROOT) - X in another spot for reference (call it
> TEMP)
> Get absolute value of TEMP (if you don't know how ask me)
> Compare TEMP and TOLERANCE
> While TEMP > TOLERANCE continue jumping to your label.
> Other wise you got your root
>
> I'm definitely not the best at assembly but I can visualize how this
> would look, if you have any questions or corrections let me know.
>
> Erik Huizing's method will work also but I'm sure this is much more
> efficient code and you won't have to calculate all the mod crap. Besides
> this is Z-80 not BASIC
That's right... so how are you going to do X / ROOT ???
Assembly is not suited for decimals (you can write a routine, but it
gets tedious).
I think though that the original poster just wanted to find out if a
number was prime or not, so i think Erik's method would have worked just
fine. and btw mod is very easy:
;output: a=a%b
;usage: call &mod
; or CALL_(MOD)
mod:
cp b
ret c
sub b
jr mod
--
Terry Peng <tpeng@geocities.com>
References: