/* newtdiv.c -- Find 1/R without hardware divide instruction Written October 17, 2012 by Eric Olson for Math 701 */ #include #include typedef union { struct { long long unsigned sigma:52; unsigned n:11; unsigned s:1; } bits; double value; } ieeebits; double invert(double R){ double x,x0; ieeebits guess; guess.value=R; guess.bits.n=2044-guess.bits.n; x0=guess.value; int i; for(i=1;i<100;i++){ x=x0*(2-R*x0); if(x==x0) return x; x0=x; } return NAN; } int main(){ printf( "newtdiv.c -- Find 1/R without hardware divide instruction\n" "Written October 17, 2012 by Eric Olson for Math 701\n"); double R[]={ 1.0 , 2.0, 0.999, 214.32, 0.000128 }; int N=sizeof(R)/sizeof(double); printf("\n%2s %22s %22s %22s\n","n","R","1/R","|1-R(1/R)|"); int n; for(n=0;n