home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / math / math.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-31  |  1.8 KB  |  62 lines

  1. /* MATH.C illustrates floating point math functions including:
  2.  *      exp             pow             sqrt            frexp
  3.  *      log             log10           ldexp           modf
  4.  *      ceil            floor           fabs            fmod
  5.  */
  6.  
  7. #include <math.h>
  8. #include <float.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. main()
  13. {
  14.     double x, rx, y;
  15.     int n;
  16.  
  17.     printf( "\nEnter a real number: " );
  18.     scanf( "%lf", &x );
  19.  
  20.     printf( "Mantissa: %2.2lf\tExponent: %d\n", frexp( x, &n ), n );
  21.     printf( "Fraction: %2.2lf\tInteger: %lf\n", modf( x, &y ), y );
  22.  
  23.     printf("\nFunction\tResult for %2.2f\n\n", x );
  24.     if( (rx = exp( x )) && (errno != ERANGE) )
  25.         printf( "exp\t\t%2.2f\n", rx );
  26.     else
  27.         errno = 0;
  28.     if( x > 0.0 )
  29.         printf( "log\t\t%2.2f\n", log( x ) );
  30.     if( x > 0.0 )
  31.         printf( "log10\t\t%2.2f\n", log10( x ) );
  32.     if( x >= 0.0 )
  33.         printf( "sqrt\t\t%2.2f\n", sqrt( x ) );
  34.     printf( "ceil\t\t%2.2f\n", ceil( x ) );
  35.     printf( "floor\t\t%2.2f\n", floor( x ) );
  36.     printf( "fabs\t\t%2.2f\n", fabs( x ) );
  37.  
  38.     printf( "\nEnter another real number: " );
  39.     scanf( "%lf", &y );
  40.     printf("\nFunction\tResult for %2.2f and %2.2f\n\n", x, y );
  41.     printf( "fmod\t\t%2.2f\n", fmod( x, y ) );
  42.     rx = pow( x, y );
  43.     if( (errno != ERANGE) && (errno != EDOM) )
  44.         printf( "pow\t\t%2.2f\n", rx );
  45.     else
  46.         errno = 0;
  47.     rx = hypot( x, y );
  48.     if( errno != ERANGE )
  49.         printf( "hypot\t\t%2.2f\n", hypot( x, y ) );
  50.     else
  51.         errno = 0;
  52.  
  53.     printf( "\nEnter an integer exponent: " );
  54.     scanf( "%d", &n );
  55.     rx = ldexp( x, n );
  56.     if( errno != ERANGE )
  57.     {
  58.         printf("\nFunction\tResult for %2.2f to power %d\n\n", x, n );
  59.         printf( "ldexp\t\t%2.2f\n", ldexp( x, n ) );
  60.     }
  61. }
  62.