1*24580Szliu /* 2*24580Szliu * Copyright (c) 1985 Regents of the University of California. 3*24580Szliu * 4*24580Szliu * Use and reproduction of this software are granted in accordance with 5*24580Szliu * the terms and conditions specified in the Berkeley Software License 6*24580Szliu * Agreement (in particular, this entails acknowledgement of the programs' 7*24580Szliu * source, and inclusion of this notice) with the additional understanding 8*24580Szliu * that all recipients should regard themselves as participants in an 9*24580Szliu * ongoing research project and hence should feel obligated to report 10*24580Szliu * their experiences (good or bad) with these elementary function codes, 11*24580Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 12*24580Szliu */ 13*24580Szliu 14*24580Szliu #ifndef lint 15*24580Szliu static char sccsid[] = "@(#)cbrt.c 1.1 (ELEFUNT) 09/06/85"; 16*24580Szliu #endif not lint 17*24580Szliu 18*24580Szliu /* kahan's cube root (53 bits IEEE double precision) 19*24580Szliu * for IEEE machines only 20*24580Szliu * coded in C by K.C. Ng, 4/30/85 21*24580Szliu * 22*24580Szliu * Accuracy: 23*24580Szliu * better than 0.667 ulps according to an error analysis. Maximum 24*24580Szliu * error observed was 0.666 ulps in an 1,000,000 random arguments test. 25*24580Szliu * 26*24580Szliu * Warning: this code is semi machine dependent; the ordering of words in 27*24580Szliu * a floating point number must be known in advance. I assume that the 28*24580Szliu * long interger at the address of a floating point number will be the 29*24580Szliu * leading 32 bits of that floating point number (i.e., sign, exponent, 30*24580Szliu * and the 20 most significant bits). 31*24580Szliu * On a National machine, it has different ordering; therefore, this code 32*24580Szliu * must be compiled with flag -DNATIONAL. 33*24580Szliu */ 34*24580Szliu #ifndef VAX 35*24580Szliu 36*24580Szliu static unsigned long B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 37*24580Szliu B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 38*24580Szliu static double 39*24580Szliu C= 19./35., 40*24580Szliu D= -864./1225., 41*24580Szliu E= 99./70., 42*24580Szliu F= 45./28., 43*24580Szliu G= 5./14.; 44*24580Szliu 45*24580Szliu double cbrt(x) 46*24580Szliu double x; 47*24580Szliu { 48*24580Szliu double r,s,t=0.0,w; 49*24580Szliu unsigned long *px = (unsigned long *) &x, 50*24580Szliu *pt = (unsigned long *) &t, 51*24580Szliu mexp,sign; 52*24580Szliu 53*24580Szliu #ifdef NATIONAL /* ordering of words in a floating points number */ 54*24580Szliu int n0=1,n1=0; 55*24580Szliu #else 56*24580Szliu int n0=0,n1=1; 57*24580Szliu #endif 58*24580Szliu 59*24580Szliu mexp=px[n0]&0x7ff00000; 60*24580Szliu if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */ 61*24580Szliu if(x==0.0) return(x); /* cbrt(0) is itself */ 62*24580Szliu 63*24580Szliu sign=px[n0]&0x80000000; /* sign= sign(x) */ 64*24580Szliu px[n0] ^= sign; /* x=|x| */ 65*24580Szliu 66*24580Szliu 67*24580Szliu /* rough cbrt to 5 bits */ 68*24580Szliu if(mexp==0) /* subnormal number */ 69*24580Szliu {pt[n0]=0x43500000; /* set t= 2**54 */ 70*24580Szliu t*=x; pt[n0]=pt[n0]/3+B2; 71*24580Szliu } 72*24580Szliu else 73*24580Szliu pt[n0]=px[n0]/3+B1; 74*24580Szliu 75*24580Szliu 76*24580Szliu /* new cbrt to 23 bits, may be implemented in single precision */ 77*24580Szliu r=t*t/x; 78*24580Szliu s=C+r*t; 79*24580Szliu t*=G+F/(s+E+D/s); 80*24580Szliu 81*24580Szliu /* chopped to 20 bits and make it larger than cbrt(x) */ 82*24580Szliu pt[n1]=0; pt[n0]+=0x00000001; 83*24580Szliu 84*24580Szliu 85*24580Szliu /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 86*24580Szliu s=t*t; /* t*t is exact */ 87*24580Szliu r=x/s; 88*24580Szliu w=t+t; 89*24580Szliu r=(r-t)/(w+r); /* r-s is exact */ 90*24580Szliu t=t+t*r; 91*24580Szliu 92*24580Szliu 93*24580Szliu /* retore the sign bit */ 94*24580Szliu pt[n0] |= sign; 95*24580Szliu return(t); 96*24580Szliu } 97*24580Szliu #endif 98