Square-root based on CORDIC

We explained the CORDIC basics for trig-functions earlier. The solution of exercise 2 of that page will be shown here. But some preliminary explanations.

Perhaps you know the following card-game:

You tell a candidate to select and remind a number from 1 to 31. Then you show him the following five cards one by one. He must answer the question whether the number is yes or no written on that card. By miracle you can tell him the number he chose. The card-order is irrelevant.

The trick is to mentally add the first number of each card where he answered YES.

Let's take an example: the candidate chooses 23

• card 1: Yes, so mind 1
• card 2: Yes, so add 2 -->3
• card 3: Yes, add 4 -->7
• card 4: No, do nothing
• card 5: Yes, add 16 -->23

How does this game work?

By answering yes or no, the candidate is simply converting the decimal number 23 in a binary number:

2310 = 111012 = [Yes, Yes, Yes, No, Yes], where Yes=1 and No=0

Each card shows all the numbers with the same binary-digit set to 1.

The quiz-master computes the reconversion to decimal by calculating the base-polynomial:

1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 1 x 20

= 1 x 16 + 0 x 8 + 1 x 4 + 1 x 2 + 1 x 1 = 23

In fact CORDIC-algorithms are based on this sort of computing. The interest is of course the proximity of the binary-system to computer-systems. Multiplying by 2 is equivalent of shifting the binary number 1 digit to the left. Dividing by 2 is the same as rotating 1 digit to the right:

111012 x 210 = 1110102

111012 DIV 210 = 1110.12

These shift-operations are extremely quick.

NOTE: in only one of our examples this shift-trick is used, for only few higher computer-languages allow access to these low-level functions. But CORDIC has another speed-advantage which comes from the exponential approach. Multiplying and dividing may be reduced to additions and substractions.

To compute a square-root with CORDIC the number is yielded by multiplying, adding and testing.

 L 2^L y x= 12056 0 initial value 7 128 0 128 x 128 > 12056 do nothing 6 64 64 64 x 64 < 12056 add 64 to yinitial --> 64 5 32 96 (64 + 32)2 < 12056 add 32 to last y --> 96 4 16 96 (96 + 16)2 > 12056 do nothing 3 8 104 (96 + 8)2 < 12056 add 8 to last y --> 104 2 4 108 (104 + 4)2 < 12056 add 4 to last y --> 108 1 2 108 (108 + 2)2 > 12056 do nothing 0 1 109 (108 + 1)2 < 12056 add 1 to last y --> 109 -1 0.5 a.s.o. and so on and so on

Here a C-routine for integer-square-rooting for numbers between 0 and 65536:

 int sqrt (int x) { int base, i, y ;        base = 128 ;        y = 0 ;        for (i = 1; i <= 8; i++)        {                y + =  base ;                if  ( (y * y) > x )                {                        y -= base ;  // base should not have been added, so we substract again                }                base >> 1 ;      // shift 1 digit to the right = divide by 2         }         return y ; }

Here a Robolab-version: (you may use our text-based modifiers or use the variable numbers of your choice)

One computation needs about 0.2 seconds.

Here a test-program:

The program continuously shows a random number and its integer square-root.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.convict.lu/Jeunes/Math/square_root_CORDIC.htm", "fetch_time": 1569229633000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2019-39/segments/1568514576345.90/warc/CC-MAIN-20190923084859-20190923110859-00151.warc.gz", "warc_record_offset": 788870029, "warc_record_length": 3853, "token_count": 946, "char_count": 3166, "score": 4.59375, "int_score": 5, "crawl": "CC-MAIN-2019-39", "snapshot_type": "longest", "language": "en", "language_score": 0.9016869068145752, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00060-of-00064.parquet"}