Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a domain error

Tags:

c++

stl

in c++, <stdexcept> has a base class for 'domain errors', std::domain_error. i don't understand under what circumstances i should throw a domain error in my code. all of the other exception base classes are pretty self explanatory. i'm pretty sure that std::domain_error has nothing to do with internet domain names, per se, so please explain what class of error a domain error is and provide some examples.

like image 980
rev Avatar asked Mar 13 '09 00:03

rev


1 Answers

Domain and range errors are both used when dealing with mathematical functions.

On the one hand, the domain of a function is the set of values that can be accepted by the function. For example, the domain of the root square function is the set of positive real numbers. Therefore, a domain_error exception is to be thrown when the arguments of a function are not contained in its domain

On the other hand, the range of a function is the set of values that the function can return. For example, the range of a function like this one:

f(x) = -x²

is the set of negative real numbers. So what is the point of the range_error? If the arguments of the function are in its domain, then the result must be in its range, so we shouldn't have any errors wrt the range...However, sometimes the value can be defined, but without being representable. For example, in C, the functions in <math.h> generate errors if the return value is too large (or too small) in magnitude to represent

like image 116
Luc Touraille Avatar answered Sep 24 '22 17:09

Luc Touraille