home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / STARTUP.ZIP / EXAMPLES / FPMATH / FPDEMO.C next >
Encoding:
C/C++ Source or Header  |  1990-09-01  |  1.6 KB  |  73 lines

  1. /*
  2. //        Sample application to demonstrate the use of the math coprocessor
  3. //        emulation in an embedded application.
  4. */
  5.  
  6. #include    <stdlib.h>
  7. #include    <signal.h>
  8. #include    <setjmp.h>
  9. #include    <math.h>
  10.  
  11. #define    ERR_LIMIT        0.000001
  12.  
  13. void        MyFPHandler(int, int) ;
  14. jmp_buf    jbuf ;
  15.  
  16. void    main()
  17. {
  18.     float        f1, f2 ;
  19.     double    d1, d2 ;
  20.     unsigned    errors = 0 ;
  21.  
  22.     /* Check that sin(x)^2 + cos(x)^2 = 1 where x is PI/2 */
  23.     f1 = M_PI_2 ;
  24.     f2 = pow(sin(f1), 2.0) + pow(cos(f1), 2.0) ;
  25.     if (fabs(f2 - 1.0) > ERR_LIMIT)
  26.         errors++ ;
  27.  
  28.     /* Check that ln(e^2) = 2 */
  29.     d1 = M_E ;
  30.     d2 = log(pow(d1, 2.0)) ;
  31.     if (fabs(d2 - 2.0) > ERR_LIMIT)
  32.         errors++ ;
  33.  
  34.     /* Force an error and see that matherr() catches and fixes it */
  35.     d1 = -4.0 ;
  36.     d2 = sqrt(d1) ;
  37.  
  38.     /* Install a new floating point exception handler */
  39.     signal(SIGFPE, MyFPHandler) ;
  40.  
  41.     /* Force an intrinsic error and verify it is intercepted */
  42.     if (setjmp(jbuf) == 0)
  43.         raise(SIGFPE) ;
  44.     else
  45.         errors++ ;                            /* This code should also execute */
  46.  
  47.     /* Force an intrinsic error and verify it is intercepted */
  48.     if (setjmp(jbuf) == 0)   {
  49.         d1 = 0.0 ;
  50.         d2 = 1.0 ;
  51.         d2 = d2 / d1 ;
  52.     }
  53.     else   {
  54.         d2 = M_PI ;                            /* This code should also execute */
  55.         errors++ ;
  56.     }
  57.  
  58.     /* Return the number of errors (should be 2) */
  59.     exit(errors) ;
  60. }
  61.  
  62. #pragma    warn    -par                        /* Turn off the unused parameter warning */
  63. void        MyFPHandler(
  64.                 int        sig,                /* SIGFPE */
  65.                 int        type                /* Floating point subtype */
  66.             )
  67. {
  68.     /* Restore the intercept for this signal */
  69.     signal(SIGFPE, MyFPHandler) ;
  70.     longjmp(jbuf, type) ;
  71. }
  72. #pragma    warn    +par                        /* Turn unused parameter warnings on again */
  73.