aboutsummaryrefslogtreecommitdiff
path: root/src/benchmarks/cfrac/psqrt.c
blob: 00531a6fd65293121f0af9261b9f32f21d95461f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "precision.h"

/*
 *  Square root
 */
precision psqrt(y)
   precision y;
{
   int i;
   precision x = pUndef, lastx = pUndef;

   i = pcmpz(pparm(y));
   if (i == 0) {				/* if y == 0 */
      pset(&lastx, pzero);
   } else if (i < 0) {				/* if y negative */
      pset(&x, errorp(PDOMAIN, "psqrt", "negative argument"));
   } else {
      pset(&x, y);
      do {
	 pset(&lastx, x);
	 pset(&x, phalf(padd(x, pdiv(y, x))));
      } while (plt(x, lastx));
   }

   pdestroy(x);

   pdestroy(y);
   return presult(lastx);
}