Post Reply 
Square Root: Digit by Digit Algorithm
11-25-2022, 06:02 AM
Post: #1
Square Root: Digit by Digit Algorithm
The following program for the WP-34S is inspired by the this snippet of the HP-45 ROM source code:
Code:
 501   L01250:  .1111...1.                      sqt15:  c + 1 -> c[p]
 502   L01251:  11.1..111.                      sqt16:  a - c -> a[w]
 503   L01252:  1.1.1...11  -> L01250                   if no carry go to sqt15
 504   L01253:  1111..111.                              a + c -> a[w]
 505   L01254:  .1....111.                              shift left a[w]
 506   L01255:  .....111..                              p - 1 -> p
 507   L01256:  1..1.1..1.                      sqt17:  shift right c[wp]
 508   L01257:  ....1.11..                              if p # 0
 509   L01260:  1.1.1..111  -> L01251                        then go to sqt16
 510   L01261:  ..11.1.111  -> L01065                   go to tnm12

The registers are mapped to the stack as follows:

Z: p
Y: c
X: a

While the program could easily be translated to HP calculators, the INC and SDL commands make it shorter:
Code:
001 INC Y
002 RCL- Y
003 x≥0?
004 BACK 003
005 RCL+ Y
006 SDL 002
007 x⇆ Y
008 IP
009 SDL 001
010 # ½
011 +
012 x⇆ Y
013 DSZ Z
014 BACK 012
015 x⇆ Y
016 RTN
017 LBL A
018 # ½
019 # 012
020 x⇆ Z
021 RCL× Y
022 BACK 020
023 END

Example

41 A

6.4031242374 12

It is recommended to use a number in the range [1, 100), although it works outside of it as well.
However, it is more efficient to apply a decimal shift first.
So use 47.11 instead of 4711 or 0.4711.
Make sure to shift an even number of digits.

The program can be helpful to understand the algorithm if you go through an example step by step.

References

By now you may have noticed that I didn't pay attention to the exponent.
But that's not so interesting in my opinion.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Square Root: Digit by Digit Algorithm - Thomas Klemm - 11-25-2022 06:02 AM



User(s) browsing this thread: 1 Guest(s)