PRB: Underflow Caused By exp() Function Fails to Set errnoLast reviewed: January 15, 1998Article ID: Q179274 |
The information in this article applies to:
SYMPTOMSWhen using the exp() function and an underflow error occurs, the errno variable is not set to a value.
RESOLUTION
#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 ); } } STATUSThis behavior is by design.
MORE INFORMATIONThe 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.
Steps to Reproduce BehaviorType in and execute the following program [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 ); // 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |