Talk:Sqrt

From ZDoom Wiki
Jump to navigation Jump to search

In my last update note for this page, I expressed concern about the Newtonian sqrt function using divisions. I really should have checked this out first. Yes, it uses divisions, and yes, divisions are slower than shifts. However, it also reaches a solution in far, far fewer steps than the other two routines, so the speed of division is a non-issue. Here is the output of a test program I wrote specifically to check this:

        1: sqrt = 1 (1 iterations)
          isqrt = 1 (1 iterations)
          nsqrt = 1 (0 iterations)
          fsqrt = 1.0
       10: sqrt = 3 (3 iterations)
          isqrt = 3 (3 iterations)
          nsqrt = 3 (1 iterations)
          fsqrt = 3.2
      100: sqrt = 10 (10 iterations)
          isqrt = 10 (10 iterations)
          nsqrt = 10 (3 iterations)
          fsqrt = 10.0
     1000: sqrt = 32 (32 iterations)
          isqrt = 31 (31 iterations)
          nsqrt = 31 (6 iterations)
          fsqrt = 31.6
    10000: sqrt = 100 (100 iterations)
          isqrt = 100 (100 iterations)
          nsqrt = 100 (8 iterations)
          fsqrt = 100.0
   100000: sqrt = 316 (316 iterations)
          isqrt = 316 (316 iterations)
          nsqrt = 316 (10 iterations)
          fsqrt = 316.2
  1000000: sqrt = 1000 (1000 iterations)
          isqrt = 1000 (1000 iterations)
          nsqrt = 1000 (11 iterations)
          fsqrt = 1000.0
 10000000: sqrt = 3162 (3162 iterations)
          isqrt = 3162 (3162 iterations)
          nsqrt = 3162 (13 iterations)
          fsqrt = 3162.3

In this list are several powers of 10 that I chose to calculate square roots for. sqrt is the original function that appeared on this page. isqrt is the truncating version. nsqrt is the new Newtonanian Method version. fsqrt is the standard C library routine, used to verify correctness of the results.

With the new routine being such a massive win, the other two should probably be deleted from the page.

Spurious if?

I see you added this back:

// performs cut-off check
if(newAns == oldAns)
{
    return newAns;
}
return oldAns;

Is this if supposed to be placed somewhere else? Because right now, it's just a waste of instructions. Returning oldAns is good enough. Randy Heit 05:02, 20 March 2009 (UTC)

Yes, it's redundant. Removed.