| ??? 11/29/00 15:52 Read: times |
#6838 - RE: square root (8751) |
Let's see if I can write this coherently...
If you are working with floating point numbers, find out the format of the floating point numbers in your system. In many cases, the high-order bit of the first byte, or the high-order byte, indicates the sign of the number, and the remainder of the byte holds the high-order bits of the exponent. Mask out the sign, add either 41 hex (for the exponent of the value 1.0) or 40 hex (for the exponent of the value 0.5), and divide the sum by 2. This will provide an initial estimate that is within a factor of 2 of your target number. Your estimate will then converge fairly quickly using Newton's Method. (By the way, thanks, Bruce, for that link.) I used only six iterations to converge numbers with sixteen-bit mantissas. If you are working with positive, non-zero integer values, convert the number to a log, divide by 2, and covert back to an integer. How do you convert to and from logs? Try the following pseudo-code to convert N-bit integers to K-bit log_base_2 values, 1. Set CounterSize to log_base_2(N) 2. Set Counter to N 3. While number < 2^N, 3a. Decrement Counter 3b. Set number to 2*number 4. Append the (K-CounterSize) high-order bits of (number - 2^N) to the (CounterSize) low-order bits of Counter. Voila! WARNING!! IF K < (N + CounterSize), LOSS OF PRECISION WILL OCCUR!! (But the square-root operation should still work.) Try the following pseudo-code to convert the K-bit log_base_2 values back into N-bit integer values: 1. Set CounterSize to log_base_2(N) 2. Set the (CounterSize) low-order bits of Counter to the (CounterSize) high-order bits of the log_base_2 value. 3. Append the (Counter) high-order bits of the (K-CounterSize) low-order bits of the log_base_2 value to the value 1. Good luck! BE CAREFUL!! THE LOSS OF PRECISION CAN SNEAK IN ON YOU IF YOU TRY TO USE THIS METHOD FOR MULTIPLICATIONS!! (Reference an issue of Embedded Systems Journal from the late 80's or early 90's for the conversion to logarithm. I'll try to track it down when I can.) Everyone, please verify my work. I may have made a mistake somewhere. |
| Topic | Author | Date |
| square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
| RE: square root (8751) | 01/01/70 00:00 | |
RE: square root (8751) | 01/01/70 00:00 |



