C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
MATH.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* MATH.C illustrates floating-point math functions including:
 *      exp             pow             sqrt            frexp
 *      log             log10           ldexp           modf
 *      ceil            floor           fabs            fmod
 */
 
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
 
void main()
{
    double x, rx, y;
    int n;
 
    printf( "\nEnter a real number: " );
    scanf( "%lf", &x );
 
 
    rx = frexp( x, &n );
    printf( "Mantissa: %2.2lf\tExponent: %d\n", rx, n );
    rx = modf( x, &y );
    printf( "Fraction: %2.2lf\tInteger: %lf\n", rx, y );
 
    printf("\nFunction\tResult for %2.2f\n\n", x );
    if( (rx = exp( x )) && (errno != ERANGE) )
        printf( "exp\t\t%2.2f\n", rx );
    else
        errno = 0;
    if( x > 0.0 )
        printf( "log\t\t%2.2f\n", log( x ) );
    if( x > 0.0 )
        printf( "log10\t\t%2.2f\n", log10( x ) );
    if( x >= 0.0 )
        printf( "sqrt\t\t%2.2f\n", sqrt( x ) );
    printf( "ceil\t\t%2.2f\n", ceil( x ) );
    printf( "floor\t\t%2.2f\n", floor( x ) );
    printf( "fabs\t\t%2.2f\n", fabs( x ) );
 
    printf( "\nEnter another real number: " );
    scanf( "%lf", &y );
    printf("\nFunction\tResult for %2.2f and %2.2f\n\n", x, y );
    printf( "fmod\t\t%2.2f\n", fmod( x, y ) );
    rx = pow( x, y );
    if( (errno != ERANGE) && (errno != EDOM) )
        printf( "pow\t\t%2.2f\n", rx );
    else
        errno = 0;
    rx = _hypot( x, y );
    if( errno != ERANGE )
        printf( "hypot\t\t%2.2f\n", _hypot( x, y ) );
    else
        errno = 0;
 
    printf( "\nEnter an integer exponent: " );
    scanf( "%d", &n );
    rx = ldexp( x, n );
    if( errno != ERANGE )
    {
        printf("\nFunction\tResult for %2.2f to power %d\n\n", x, n );
        printf( "ldexp\t\t%2.2f\n", rx );
    }
}
                                    -♦-