/* * ====================================================================== * Demonstrate Numerical Errors due to Subtractive Cancellation * * Copyright (C) 1998 by Mark Austin. * * 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. * * Written by : Mark Austin * ====================================================================== */ #include #include float Function1 ( float fX ) { return (1.0-cos(fX))/fX/fX; } float Function2 ( float fX ) { return sin(fX)*sin(fX)/fX/fX/(1.0 + cos(fX)); } int main( void ) { float fX = 1.0F; int iCount; for (iCount = 1; iCount <= 8; iCount ++ ) { fX = fX/10.0; printf( "fX = %10.3e : Function1 = %10.8f : Function2 = %10.8f \n", fX, Function1( fX ), Function2( fX )); } }