C++ 什么是域错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/641064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is a domain error
提问by rev
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.
在 C++ 中,<stdexcept> 有一个用于“域错误”的基类 std::domain_error。我不明白在什么情况下我应该在我的代码中抛出域错误。所有其他异常基类都是不言自明的。我很确定 std::domain_error 本身与互联网域名无关,所以请解释域错误是哪一类错误并提供一些示例。
回答by Luc Touraille
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
一方面,函数的域是函数可以接受的值的集合。例如,平方根函数的域是正实数集。因此,domain_error
当函数的参数不包含在其域中时,将抛出异常
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) = -x2
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
是一组负实数。那么这有什么意义range_error
呢?如果函数的参数在它的域中,那么结果必须在它的范围内,所以我们不应该在范围内有任何错误......但是,有时可以定义值,但不能表示。例如,在 C 中,<math.h>
如果返回值的大小太大(或太小)而无法表示,则 中的函数会产生错误
回答by John Feminella
A domain errorrefers to issues with mathematical domainsof functions. Functions are sometimes defined only for certain values. If you try to invoke such a function with an argument that is not part of its domain, that is a domain error.
一个域误差指的是与问题的数学领域的功能。有时只为某些值定义函数。如果您尝试使用不属于其域的参数来调用这样的函数,那就是域错误。
For example, trying to invoke sqrt()
with a negative argument is a domain error, since negative numbers are not part of the domain of sqrt()
.
例如,尝试sqrt()
使用负参数调用是域错误,因为负数不是 的域的一部分sqrt()
。
回答by greyfade
It's for logic errors of the class of "domain" errors. This could apply to any situation where the input to a function exceeds the allowable domain for it to operate on. That's its stated purpose in the standard.
它用于“域”错误类的逻辑错误。这适用于函数的输入超出其操作允许的域的任何情况。这就是它在标准中声明的目的。
For example, you have a function that takes only positive floats, so it throws a domain_error for negative numbers.
例如,您有一个仅接受正浮点数的函数,因此它会为负数引发 domain_error。
回答by sth
Pretty good explanation form cplusplus.com:
很好的解释形式cplusplus.com:
Generally, the domain of a mathematical function is the subset of values that it is defined for. For example, the square root function is only defined for non-negative numbers. Thus, a negative number for such a function would be a domain error.
通常,数学函数的域是为其定义的值的子集。例如,平方根函数仅针对非负数定义。因此,这种函数的负数将是域错误。
回答by sth
Well, this is all the guidance you get from the C++ standard:
好吧,这就是您从 C++ 标准中获得的所有指导:
The class domain_error defines the type of objects thrown as exceptions by the implementation to report domain errors.
类 domain_error 定义了由实现作为异常抛出的对象类型,以报告域错误。
Domain here means "problem domain", nothing to do with the internet. For example, a square root function might throw a domain error if passed a negative number.
这里的域是指“问题域”,与互联网无关。例如,如果传递负数,平方根函数可能会引发域错误。
回答by sth
"Detailed Description Thrown by the library, or by you, to report domain errors (domain in the mathmatical sense). "
“详细描述由图书馆或您抛出,以报告域错误(数学意义上的域)。”
From: http://www.aoc.nrao.edu/~tjuerges/ALMA/STL/html/classstd_1_1domain__error.html
来自:http: //www.aoc.nrao.edu/~tjuerges/ALMA/STL/html/classstd_1_1domain__error.html
according to this, it should be used if you are given input that does not comply with the constraints you put on your interface. Like say, a function that is supposed to receive a positive value and is given a negative one.
根据这一点,如果您的输入不符合您对界面施加的约束,则应该使用它。比如说,一个函数应该接收一个正值而被赋予一个负值。
回答by Michael
It refers to mathematical domains.
它指的是数学领域。
float MySqrRoot(float x)
{
// sqrt is not valid for negative numbers.
if (x < 0) throw new domain_error;
//...
}