PRB: Underflow Caused By exp() Function Fails to Set errno

Last reviewed: January 15, 1998
Article ID: Q179274
The information in this article applies to:
  • The C Run Time (CRT) included with: - Microsoft Visual C++, 32-bit Editions, version 5.0

SYMPTOMS

When 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 );
      }
   }

STATUS

This behavior is by design.

MORE INFORMATION

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.

Steps to Reproduce Behavior

Type 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
Keywords : kbcode
Version : WINNT:5.0
Platform : winnt
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.