什么是域错误 [英] what is a domain error

查看:55
本文介绍了什么是域错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中,< stdexcept>具有域错误"的基类std :: domain_error.我不明白在什么情况下应该在代码中抛出域错误.所有其他异常基类都是很容易解释的.我很确定std :: domain_error与互联网域名本身无关,所以请解释一下域名错误是什么类型的错误,并提供一些示例.

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.

推荐答案

在处理数学函数时,都会使用域和范围错误.

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

一方面,函数的域是函数可以接受的一组值.例如,平方根函数的域是正实数集.因此,在以下情况下将引发 domain_error 异常函数的参数不包含在其域中

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²

是一组负实数.那么 range_error 的意义是什么?如果函数的参数在其域中,则结果必须在其范围内,因此在该范围内我们不会有任何错误...但是,有时可以定义,但又没有代表性.例如,在C语言中,如果返回值的大小太大(或太小)无法表示

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

这篇关于什么是域错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆