// distance between two points using a function // and scaling to avoid overflow // #include #include // Pythagorean formula, sqrt( a*a + b*b); // double pythag( double a, double b) { double c, scale; a = fabs(a); b = fabs(b); // a and b (the function arguments) are local variables here, // same as c and scale, except a and b are initialized by the caller if( a > b) scale = a; else scale = b; // or use conditional operator (see section 3.3): // // scale = (a > b) ? a : b; if( scale == 0) return 0; // avoid divide by zero a /= scale; b /= scale; c = sqrt( a*a + b*b); c *= scale; return c; } int main(void) { double x1=1e200, y1=5e200, x2=4e200, y2=7e200, distance; distance = pythag( x2-x1, y2-y1); printf("The distance between the two points is %g\n",distance); return 0; } /* output: The distance between the two points is 3.60555e+200 */