C Library Macro -ERANGE

C Standard Library - <errno.h>

Description

C Library MacroERANGEIndicates a range error, which occurs when the input argument exceeds the range defined by the mathematical function, and errno is set to ERANGE.

ERANGEis a macro in the C standard library, defined in<errno.h>in the header file. It is used to indicate an error where the result exceeds the range defined by the function, usually occurring when the result of a mathematical function or conversion function exceeds the representable range.

Declaration

Below is the declaration of the ERANGE macro.

#define ERANGE some_value

Parameter

  • NA

Return Value

  • NA

Example

The following are several uses ofERANGEexample demonstrating how to handle an out-of-range result error.

Mathematical Functions

In mathematical calculations, such as theexporlogfunction, an error may occur.ERANGEerror. For example, when calculating a very large exponent, the result may exceed the representable range of floating-point numbers.

Example

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

int main() {
    double x = 1000.0;
    double result;

    // Clear previous error
    errno = 0;

    // Call exp function, may cause result out of range
    result = exp(x);

    if (errno == ERANGE) {
        printf("Error: exp() result out of range for input value %f\n", x);
    } else {
        printf("The exp of %f is %f\n", x, result);
    }

    return 0;
}

In this example, computeexp(1000.0)may cause the result to go out of range, in which caseerrnowill be set toERANGE。

Let us compile and run the above program, which will produce the following result:

The exp of 1000.000000 is inf

Conversion Functions

In numerical conversions, such as thestrtolorstrtodfunction, if the conversion result exceeds the range of the target type,errnowill also be set toERANGE。

Example

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

int main() {
    const char *str = "99999999999999999999999999";
    long result;

    // Clear previous error
    errno = 0;

    // Call strtol function, may cause result out of range
    result = strtol(str, NULL, 10);

    if (errno == ERANGE) {
        printf("Error: strtol() result out of range for input string %s\n", str);
    } else {
        printf("The converted long value is %ld\n", result);
    }

    return 0;
}

In this example, the string"99999999999999999999999999"Converted tolongwhen the value is of type, the result exceedslongthe representable range oferrnowill be set toERANGE。

Let us compile and run the above program, which will produce the following result:

Error: strtol() result out of range for input string 99999999999999999999999999

Usage Precautions

  1. Check the return value: After calling a function that may produce a range error, the return value should be checked. Typically, these functions return a special value on error (such asHUGE_VAL、HUGE_VALF、HUGE_VALLor0)。
  2. Initializeerrno: before calling the function, you shoulderrnoset to 0, to ensure that the error code was produced by this call, rather than left over from a previous call.
  3. Handling Range Errors: when detectedERANGEWhen an error occurs, it should be handled appropriately according to the context, for example by printing an error message, using an alternative value, or retrying the calculation.

Related Functions and Error Codes

  • Mathematical Functions: such asexp, log, pow, sin, cos, tan, they may set when the calculation result exceeds the representable range.errnoisERANGE。
  • Conversion Functions: such asstrtol, strtoll, strtoul, strtoull, strtof, strtod, strtold, they may set when the conversion result exceeds the representable range.errnoisERANGE。

Other Related Error Codes

  • EDOM: Indicates a domain error in a mathematical function, i.e., the argument is outside the domain of definition.
  • EILSEQ: Indicates an illegal byte sequence error, which typically occurs when processing multibyte characters or wide characters.

Through the proper use oferrno、ERANGEand its related functions, you can write more robust and reliable code, ensuring that the program can still handle and respond properly under various abnormal conditions.

C Standard Library - <errno.h>

other extensions