C library - ERANGE Macro



The C library ERANGE Macro represents a range error, which occurs if an input argument is outside the range, over which the mathematical function is defined and errno is set to ERANGE.

The ERANGE associated with mathematical functions that set to input ranges. By setting errno to ERANGE, it represents the result of operation which is not representable within the valid range.

Syntax

Following is the C library syntax of the ERANGE Macro.

#define ERANGE some_value

Parameters

  • This function is known for macros. So, it doesn't accept any parameter.

Return Value

  • This function doesn't return any value.

Example 1

Following is the C library program that shows the usage of ERANGE Macro.

#include <stdio.h>
#include <errno.h>
#include <math.h>

int main () {
   double x;
   double value;

   x = 2.000000;
   value = log(x);
   
   if( errno == ERANGE ) {
      printf("Log(%f) is out of range\n", x);
   } else {
      printf("Log(%f) = %f\n", x, value);
   }

   x = 1.000000;
   value = log(x);
   
   if( errno == ERANGE ) {
      printf("Log(%f) is out of range\n", x);
   } else {
      printf("Log(%f) = %f\n", x, value);
   }
   
   x = 0.000000;
   value = log(x);
   
   if( errno == ERANGE ) {
      printf("Log(%f) is out of range\n", x);
   } else {
      printf("Log(%f) = %f\n", x, value);
   }
   
   return 0;
}

Output

On compiling of above code, it will produce the following result −

Log(2.000000) = 0.693147
Log(1.000000) = 0.000000
Log(0.000000) is out of range

Example 2

Here, we are building the program for the calculation of logarithm number using the mathematical functions log() and exp() which encounter the range errors.

#include <stdio.h>
#include <math.h>
#include <errno.h>

int main() {
    double x = 1000; 

    double result = exp(x);
    if (errno == ERANGE) {
        printf("exp(%f) is out of range\n", x);
    } else {
        printf("exp(%f) = %f\n", x, result);
    }

    return 0;
}

Output

On execution of above code, we get the following result −

exp(1000.000000) is out of range
Advertisements