C Library Macro -EDOM

C Standard Library - <errno.h>

Description

C Library MacroEDOMIndicates a domain error, which occurs when an input argument goes outside the domain defined by the mathematical function, and errno is set to EDOM.

EDOMIt is a macro in the C standard library, defined in<errno.h>the header file. It is used to represent a mathematical function argument domain error, that is, the function argument goes beyond the mathematical domain. This usually occurs in math library functions (such assqrt、log、asinWhen (etc.) receives illegal input parameters.

Declaration

The following is a use ofEDOMan example demonstrating how to handle mathematical function argument domain errors:

#define EDOM some_value

Parameter

  • NA

Return Value

  • NA

Example

The following example demonstrates the usage of the EDOM macro.

Example

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

int main() {
    double x = -1.0;
    double result;

    // Clear previous error
    errno = 0;

    // Call sqrt function, passing a negative value will cause a domain error
    result = sqrt(x);

    if (errno == EDOM) {
        printf("Error: sqrt() domain error, input value is %f\n", x);
    } else {
        printf("The square root of %f is %f\n", x, result);
    }

    return 0;
}

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

The square root of -1.000000 is nan

In this example:

  1. We attempt to calculate the square root of -1, which is an operation outside the domain, because negative numbers have no real square root.
  2. sqrtThe function will detect this domain error, and willerrnoSet toEDOM。
  3. We checkerrnothe value of, if it isEDOM, then print the corresponding error message.

related mathematical functions

The following are some that may seterrnoisEDOMmathematical functions:

  • sqrt(x): whenx < 0When ..., it will seterrnoisEDOM。
  • log(x): whenx <= 0When ..., it will seterrnoisEDOM。
  • asin(x)andacos(x): whenxWhen it is not within the range [-1, 1], it will seterrnoisEDOM。
  • pow(x, y): whenx < 0andyWhen it is not an integer, it will seterrnoisEDOM。

Usage notes

  1. Check the return value: After calling a math function that may produce a domain error, you should check the return value, and when the function returns a special value (such asNaN) when checkingerrno。
  2. Initializeerrno: before calling the function, you shoulderrnoset to 0 to ensure that the error code is generated by this call rather than left over from a previous call.
  3. Thread Safety: in multi-threaded programs,errnousually implemented as thread-local storage (TLS), ensuring that each thread has its ownerrnovalue.

Other related error codes

exceptEDOM,<errno.h>There are other error codes related to mathematical functions in:

  • ERANGE: The result is out of range. For example, calculatingexp(1000)may cause overflow, and willerrnoSet toERANGE。
  • EILSEQ: illegal byte sequence. When handling wide-character or multibyte character functions, the input data contains an invalid character sequence.

In short,EDOMis an important macro for handling domain errors of mathematical functions, helping programmers detect and handle illegal input parameters. By reasonably usingerrnoand its related macros, one can write more robust and reliable code.

C Standard Library - <errno.h>

other extensions