如何处理上溢和下溢? [英] How to deal with overflow and underflow?

查看:792
本文介绍了如何处理上溢和下溢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Matlab的新手,试图弄清楚当答案实际上在范围内时如何处理上溢和下溢算法.

I am new to Matlab and trying to figure out how can I deal with overflow and underflow arithmetic when the answer is actually within the range.

例如:

x = 2e+160
x = x*x (which returns inf, an overflow)
x = sqrt(x) (which is in the range)

感谢您的帮助.

推荐答案

我不是Matlab用户,因此请记住这一点.

I am not a Matlab user so take that in mind.

此背后的主要问题是首先检测到上溢/下溢

这有时很难,因为在其他情况下,当计算不返回zeroinf时,它们也会出现.例如,在数值积分过程中,上溢/下溢会导致结果错误,但数值仍非零.

That is sometimes hard because they appear also in other cases when the computation does not return zero or inf. For example during numerical integration overflow/underflows can cause the result to be wrong but still a non zero number.

根据我的经验,我认为以十六进制表示形式查看数字很有用(除非您的HW/SW计算内部使用十进制基数作为变量,这是罕见的,因为大多数HW/SW是二进制的).因此,请以十六进制形式查看数字并检测如下模式:

In my experience I tent to see as useful to look at numbers in their hex representation (unless your HW/SW computations use decadic base internally for variables which is rare because most HW/SW is binary). So see the number in Hex form and detect patterns like:

??????????.????FFFFFFFFFFF?? hex

当您查看小数部分并发现在最低位数附近存在许多FFFFF时,您的数字最有可能下溢或非常接近该点.零的数量或结束时的数量通常随着每次迭代的饱和而减少:

when you look at the fractional part and detect that many FFFFF's are present near the lowest digits then you number is most likely underflowing or is very near that point. The number of zeros or what ever at the end is usually decreasing with each iteration saturating to:

??????????.????FFFFFFFFFFF hex

溢出类似地饱和,但另一方面是这样的:

The overflows are saturated similarly but on the other side like this:

FFFFFFFFFFF.FFFFFF?????? hex

对于某些算法,在下一次迭代之前更精确地对这些数字进行四舍五入是很重要的,但是在应用未知数之前,您始终需要检查某些众所周知的计算示例是否是这种情况……请看这里:

For some algorithms is more precise to round up/down such numbers before next iteration but you need always check if that is the case on some well known example of computations before applying on unknowns ... Look here:

这是使用此技术的算法的一个很好的例子

it is a nice example of algorithm using this technique

检测上溢/下溢的另一种方法是预测结果数量的大小.例如

Another way to detect overflow/underflows is the prediction of the outcome number magnitude. For example

  • *将指数相加
  • /减去指数
  • sqrt将指数减半
  • +-可能导致+1/-1的指数更大
  • * sums the exponents together
  • / substract the exponents
  • sqrt halves the exponent
  • +,- can result in +1/-1 of the bigger exponent

因此,如果您要处理大/小指数,则知道哪些操作可能导致溢出问题.

So if you are dealing with big/small exponents you know which operations could lead to overflowing problems.

最重要的是,当结果精度不适合尾数时,可能会发生下溢.因此,您需要注意增加结果使用位的操作,例如:

On top of that underflows can occur when your results precision does not fit into mantissa. So you need to be careful with operations that increase the used bits of the result like:

  • a*b ab
  • 中已使用位的总和
  • +,-(a,b)的最大使用位-(a,b)的最小使用位
  • /添加一些位来保存分数...
  • a*b sum of used bits in a,b
  • +,- max used bit of (a,b) - min used bit of (a,b)
  • / adds some bits to hold the fractions ...

+,-操作是最糟糕的,例如,如果添加2^100 + 2^-100,则结果需要200位尾数,而操作数本身只有1位尾数.

The +,- operation is the worst for example if you add 2^100 + 2^-100 then the result needs 200 bits of mantissa while the operands itself have booth just 1 bit of mantissa.

如果检测到上溢/下溢该怎么办:

  1. 更改方程式

如前所述,您可以切换到log,它可以轻松处理更大的范围,但还有其他问题.通常,算法中的细微变化也可能导致结果按不同因子进行缩放,但是子结果仍处于安全范围内,因此您只需要最终结果即可缩放回危险范围.在更改方程式时,您应始终考虑结果的准确性和有效性.

As mentioned you can switch to log which can handle bigger ranges with ease, but have other issues. Also usually slight change in algorithm can lead to results scaled by different factor, but with sub-results still in safe range so you need just the final result to scale back to dangerous range. While changing equations you should always take into account the precision and validity of the outcome.

使用更大的变量数据类型

如果我没记错的话,Matlab具有任意精度的数字,因此在需要时使用它们.您还可以使用标准的float/double变量,并将值存储到更多变量中,如下所示:

If I remember correctly Matlab have arbitrary precision numbers so use them if needed. You can also use standard float/double variables and store the value into more variables something like here:

停止迭代

例如,某些算法使用以下序列:

For example some algorithms use series like:

1/1! + 1/2! + 1/3! + ... + 1/n!

在某些情况下,如果您在停止迭代时检测到达到上溢/下溢子结果,则您仍然可以获得相对准确的计算结果.不要忘记不要将溢出的子结果包括在最终结果中.

in some cases if you detect you hit the overflowing/underflowing subresult when stop the iteration you still have relatively accurate result of the computation. Do not forget not to include overflowed subresults to the final result.

这篇关于如何处理上溢和下溢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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