what is a domain error

后端 未结 7 1594

in c++, 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

7条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 20:03

    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 generate errors if the return value is too large (or too small) in magnitude to represent

提交回复
热议问题