Re: Programming question

From: Zonn <zonn_at_zonn.com>
Date: Wed Oct 27 1999 - 17:51:10 EDT

On Wed, 27 Oct 1999 15:09:00 -0600, you wrote:

>I have everything figured out except how to get the square root of
>lengthsqr.
>Is there any easy or fast way to calculate the square root of a 16 bit
>number on a 6502?

Sure a 65k lookup table would be easy and fast. ;^)

>Or is there another way to calculate the pull of gravity, separated into x
>and y components, that still follows the rule of the inverse of the square
>of the distance?

Ok, how about a 512 byte lookup table?

Since the square root of 65535 is 255 (nearest integer), there can
only be 255 possible square root results.

Precalculate all the possible squares from 0 to 255 and place them in
a table.

Too find a square root of a 16 bit value, search the table. When the
nearest value is found, the placement in the table will be the square
root.

By using a binary search you can find the square root of a 16 bit
value in 8 or less compares, and no multiplies or divides.

Depending on the complexity of a standard square root approximation
algorithm, the 512 byte lookup table (and associate code) may actually
be less code space.

-Zonn
Received on Wed Oct 27 16:50:01 1999

This archive was generated by hypermail 2.1.8 : Fri Aug 01 2003 - 00:32:47 EDT