|
|
1.1 ! root 1: /* ! 2: * hypot -- sqrt(p*p+q*q), but overflows only if the result does. ! 3: * See Cleve Moler and Donald Morrison, ! 4: * ``Replacing Square Roots by Pythagorean Sums,'' ! 5: * IBM Journal of Research and Development, ! 6: * Vol. 27, Number 6, pp. 577-581, Nov. 1983 ! 7: */ ! 8: double hypot(p, q) ! 9: double p, q; ! 10: { ! 11: double r, s, pfac; ! 12: if(p<0) p = -p; ! 13: if(q<0) q = -q; ! 14: if(p<q){ r=p; p=q; q=r; } ! 15: if(p==0) return 0; ! 16: pfac = p; ! 17: r = q = q/p; ! 18: p = 1.0; ! 19: for(;;){ ! 20: r*=r; ! 21: s=r+4; ! 22: if(s==4) ! 23: return p*pfac; ! 24: r/=s; ! 25: p+=2*r*p; ! 26: q*=r; ! 27: r=q/p; ! 28: } ! 29: } ! 30: double cabs(z) ! 31: struct{ ! 32: double x, y; ! 33: }z; ! 34: { ! 35: return hypot(z.x, z.y); ! 36: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.