1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * All recipients should regard themselves as participants in an ongoing 18 * research project and hence should feel obligated to report their 19 * experiences (good or bad) with these elementary function codes, using 20 * the sendbug(8) program, to the authors. 21 */ 22 23 #ifndef lint 24 static char sccsid[] = "@(#)cbrt.c 5.5 (Berkeley) 09/22/88"; 25 #endif /* not lint */ 26 27 /* kahan's cube root (53 bits IEEE double precision) 28 * for IEEE machines only 29 * coded in C by K.C. Ng, 4/30/85 30 * 31 * Accuracy: 32 * better than 0.667 ulps according to an error analysis. Maximum 33 * error observed was 0.666 ulps in an 1,000,000 random arguments test. 34 * 35 * Warning: this code is semi machine dependent; the ordering of words in 36 * a floating point number must be known in advance. I assume that the 37 * long interger at the address of a floating point number will be the 38 * leading 32 bits of that floating point number (i.e., sign, exponent, 39 * and the 20 most significant bits). 40 * On a National machine, it has different ordering; therefore, this code 41 * must be compiled with flag -DNATIONAL. 42 */ 43 #if !defined(vax)&&!defined(tahoe) 44 45 static const unsigned long 46 B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 47 B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 48 static const double 49 C= 19./35., 50 D= -864./1225., 51 E= 99./70., 52 F= 45./28., 53 G= 5./14.; 54 55 double cbrt(x) 56 double x; 57 { 58 double r,s,t=0.0,w; 59 unsigned long *px = (unsigned long *) &x, 60 *pt = (unsigned long *) &t, 61 mexp,sign; 62 63 #ifdef national /* ordering of words in a floating points number */ 64 const int n0=1,n1=0; 65 #else /* national */ 66 const int n0=0,n1=1; 67 #endif /* national */ 68 69 mexp=px[n0]&0x7ff00000; 70 if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */ 71 if(x==0.0) return(x); /* cbrt(0) is itself */ 72 73 sign=px[n0]&0x80000000; /* sign= sign(x) */ 74 px[n0] ^= sign; /* x=|x| */ 75 76 77 /* rough cbrt to 5 bits */ 78 if(mexp==0) /* subnormal number */ 79 {pt[n0]=0x43500000; /* set t= 2**54 */ 80 t*=x; pt[n0]=pt[n0]/3+B2; 81 } 82 else 83 pt[n0]=px[n0]/3+B1; 84 85 86 /* new cbrt to 23 bits, may be implemented in single precision */ 87 r=t*t/x; 88 s=C+r*t; 89 t*=G+F/(s+E+D/s); 90 91 /* chopped to 20 bits and make it larger than cbrt(x) */ 92 pt[n1]=0; pt[n0]+=0x00000001; 93 94 95 /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 96 s=t*t; /* t*t is exact */ 97 r=x/s; 98 w=t+t; 99 r=(r-t)/(w+r); /* r-t is exact */ 100 t=t+t*r; 101 102 103 /* retore the sign bit */ 104 pt[n0] |= sign; 105 return(t); 106 } 107 #endif 108