/* * ====================================================================== * math.c -- demonstrate simple math library functions and application of * errno.h for checking range and domain errors. * * Math Functions used here : fabs(x), sin(x), tan(x), atan2(y,x), * floor(x), and log(x). * User defined functions : trunc(x) -- truncate small values of x. * ====================================================================== * * Copyright (C) 1994-96 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * ====================================================================== */ #include #include #include /* to allow checking of int errno */ static const float kfEpsilon = 1E-5; /* precision for loops & truncation */ /* Define a function to truncate small values to 0 */ double trunc( double dValToTruncate ) { if( fabs( dValToTruncate ) < kfEpsilon ) return 0.0; else return dValToTruncate; } int main( void ) { double dAngle; /* for angles in this example */ double dTang; /* for the tangent of a test angle */ double dLimit; /* limit for angles */ double dNum; /* for PI fraction calculation */ double dY1, dY2, dY3; /* [a] : Find and print the sin of angles from 0 to PI in PI/6 increments */ dLimit = M_PI + kfEpsilon; for( dAngle = 0.0; dAngle < dLimit; dAngle += M_PI/6 ) printf( "sin(%4.2g) = %-4.2g\n", dAngle, trunc( sin( dAngle ) ) ); /* [b] : Find the tangent of -3 PI / 4 */ dTang = tan( -3*M_PI_4 ); printf( "The tangent of %g (-3PI/4) radians is %g\n", -3*M_PI_4, dTang); /* [c] : Find the angle with tangent -root2 / -root2 in the third quadrant */ dAngle = atan2( -M_SQRT2, -M_SQRT2 ); printf( "The angle with tangent %g/%g is %g\n", -M_SQRT2, -M_SQRT2, dAngle ); printf( "Checking, this value should be %g\n", -3*M_PI_4 ); /* [d] : Find a rational expression in terms of PI (we know its a fraction of 4). */ dNum = floor( dAngle*4/M_PI ); printf( "Expressed as a PI fraction this is %g PI / %g\n", dNum, 4.0 ); /* [e] : test for domain and range errors in math functions */ /* [e.1] : domain error: for negative argument to log(x) */ printf( "\nCurrent value of errno: %i", errno ); dY1 = log( -1 ); printf( "\nValue of dY1 after log(-1) is %f : errno = %i", dY1, errno ); if( EDOM == errno ) { /* EDOM #defined in errno.h above */ printf( "\nDomain error occured in log function." ); errno = 0; /* reset error status */ } /* [e.2] : range error: for log( 0 ) */ dY2 = log( 0 ); printf( "\nValue of dY2 after log(0) is %f : errno = %i", dY2, errno ); if( ERANGE == errno || EDOM == errno ) { printf( "\nRange or domain error occured in log function." ); errno = 0; /* reset error status */ } printf( "\n" ); /* [e.3] : range/overflow error: for 10^1000 */ dY3 = pow( 10.0, 1000.0 ); printf( "\nValue of dY3 after pow(10, 1000) is %f : errno = %i", dY3, errno ); if( ERANGE == errno ) { printf( "\nRange error occured in pow() function." ); errno = 0; /* reset error status */ } printf( "\n" ); /* [f] : Simulate sequence of calculations containing error conditions */ dY1 = 1.0/0.0; dY2 = 2.0*dY1; dY3 = dY2/dY1; printf( "Value of dY1 = 1.0/0.0 = %f\n", dY1 ); printf( " dY2 = 2.0*dY1 = %f\n", dY2 ); printf( " dY3 = dY2/dY1 = %f\n", dY3 ); }