C Library Macros - ERANGE
Description
C Library Macros ERANGE represents a range error, which occurs when an input argument is outside the range defined by a mathematical function; errno is set to ERANGE.
ERANGE is a macro in the C standard library, defined in header. It is used to indicate an error where the result is outside the function's defined range, usually occurring when the computation result of a math or conversion function exceeds the representable range.
Declaration
The following is the declaration of the ERANGE macro.
#define ERANGE some_value
Parameters
- NA
Return Value
- NA
Example
The following are a few examples using ERANGE that demonstrate how to handle result-out-of-range errors.
Mathematical functions
In mathematical computations, such as exp or log functions, a ERANGE error may occur. For example, when computing a very large exponent, the result may exceed the representable range of a floating-point number.
Example
#include
#include
#include
int
main
(
)
{
double
x
=
1000.0
;
double
result
;
// Clear previous errors
errno
=
0
;
// Call the exp function; the result may exceed the representable 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, computing exp(1000.0) may cause the result to exceed the range, in which case errno is set to ERANGE.
Let's compile and run the above program, which will produce the following result:
The exp of 1000.000000 is inf
Conversion functions
In numeric conversions, such as strtol or strtod functions, if the conversion result exceeds the range of the target type,errno is also set to ERANGE.
Example
#include
#include
#include
#include
int
main
(
)
{
const
char
str
=
"99999999999999999999999999"
;
long
result
;
// Clear previous errors
errno
=
0
;
// Call the strtol function; the result may exceed the representable 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" Converts to long numeric value, the result exceeds the long representable range oferrno is set to ERANGE.
Let's compile and run the above program, which will produce the following result:
Error: strtol() result out of range for input string 99999999999999999999999999
Usage notes
- Check the return value: after calling a function that may produce a range error, check the return value; typically these functions return a special value on error (such as
HUGE_VAL,HUGE_VALF,HUGE_VALLor0). - Initialization
errno: before calling the function, seterrnoto 0 to ensure the error code comes from this call and not a leftover from a previous one. - Handling range errors: when an
ERANGEerror is detected, it should be handled appropriately according to the context, such as printing an error message, using an alternative value, or retrying the computation.
Related functions and error codes
- Mathematical functions: Such as
exp,log,pow,sin,cos,tan, which may set errno when the computation result exceeds the representable range,errnoisERANGE. - Conversion functions: Such as
strtol,strtoll,strtoul,strtoull,strtof,strtod,strtold, which may set errno when the conversion result exceeds the representable range,errnoisERANGE.
Other related error codes
EDOM: indicates a math function argument domain error, i.e., the argument is outside the defined domain.EILSEQ: indicates an illegal byte sequence error, usually occurring when handling multibyte or wide characters.
By using properly errno, ERANGE and its related functions, more robust and reliable code can be written, ensuring the program still handles and responds normally under various exceptional conditions.
C Standard Library -