PRB: Underflow Caused By exp() Function Fails to Set errnoID: Q179274
|
When using the exp() function and an underflow error occurs, the errno variable is not set to a value.
#include <stdio.h>
#include <math.h>
#include <ERRNO.H>
extern int errno;
void main( void )
{
double x = -1.0e+3,y;
y = exp( x );
// The exp function returns the exponential value of the
// floating-point parameter, x, if successful. On overflow,
// the function returns INF (infinite) and on underflow, exp
// returns 0.
// Check for error conditions.
// Underflow error or other.
if(y==0 || (errno!=0))
{
if (y==0)
printf("ERROR: Underflow Error\n");
else
perror("ERROR");
printf("Errno: %i\n",errno);
}
// No Errors--print results.
else
{
printf( "exp( %f ) = %f\n", x, y );
}
}
This behavior is by design.
The errno variable is a global variable, which is given an integer value upon an error. Math functions, such as exp(), set this value to indicate an error state; library math routines set errno by calling _matherr. However, the errno variable is not set when an underflow occurs. According to the ANSI C specification, whether or not errno acquires the value of the macro ERANGE is implementation-defined.
#include <stdio.h>
#include <math.h>
#include <ERRNO.H>
extern int errno;
void main( void )
{
double x = -1.0e+3,y;
y = exp( x );
// Check for error conditions.
// perror() returns the error message associated with errno.
if(errno!=0)
{
perror("ERROR");
}
// No Errors--print results. But error did occur because we have an
// underflow error.
// The underflow goes undetected because errno wasn't set to any
// value.
else
{
printf( "exp( %f ) = %f\n", x, y );
}
}
Additional query words: 5.00
Keywords : kbcode kbVC500 kbVC600
Version : WINNT:5.0
Platform : winnt
Issue type : kbprb
Last Reviewed: July 20, 1999