home *** CD-ROM | disk | FTP | other *** search
- /*
- // Sample application to demonstrate the use of the math coprocessor
- // emulation in an embedded application.
- */
-
- #include <stdlib.h>
- #include <signal.h>
- #include <setjmp.h>
- #include <math.h>
-
- #define ERR_LIMIT 0.000001
-
- void MyFPHandler(int, int) ;
- jmp_buf jbuf ;
-
- void main()
- {
- float f1, f2 ;
- double d1, d2 ;
- unsigned errors = 0 ;
-
- /* Check that sin(x)^2 + cos(x)^2 = 1 where x is PI/2 */
- f1 = M_PI_2 ;
- f2 = pow(sin(f1), 2.0) + pow(cos(f1), 2.0) ;
- if (fabs(f2 - 1.0) > ERR_LIMIT)
- errors++ ;
-
- /* Check that ln(e^2) = 2 */
- d1 = M_E ;
- d2 = log(pow(d1, 2.0)) ;
- if (fabs(d2 - 2.0) > ERR_LIMIT)
- errors++ ;
-
- /* Force an error and see that matherr() catches and fixes it */
- d1 = -4.0 ;
- d2 = sqrt(d1) ;
-
- /* Install a new floating point exception handler */
- signal(SIGFPE, MyFPHandler) ;
-
- /* Force an intrinsic error and verify it is intercepted */
- if (setjmp(jbuf) == 0)
- raise(SIGFPE) ;
- else
- errors++ ; /* This code should also execute */
-
- /* Force an intrinsic error and verify it is intercepted */
- if (setjmp(jbuf) == 0) {
- d1 = 0.0 ;
- d2 = 1.0 ;
- d2 = d2 / d1 ;
- }
- else {
- d2 = M_PI ; /* This code should also execute */
- errors++ ;
- }
-
- /* Return the number of errors (should be 2) */
- exit(errors) ;
- }
-
- #pragma warn -par /* Turn off the unused parameter warning */
- void MyFPHandler(
- int sig, /* SIGFPE */
- int type /* Floating point subtype */
- )
- {
- /* Restore the intercept for this signal */
- signal(SIGFPE, MyFPHandler) ;
- longjmp(jbuf, type) ;
- }
- #pragma warn +par /* Turn unused parameter warnings on again */