找到你要的答案

Q:How to deal with overflow and underflow?

Q:如何处理溢出和下溢吗?

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.

For example:

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

any help is appreciated.

我是新来的MATLAB和试图找出如何处理溢出和下溢运算时,答案是范围内。

例如:

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

任何帮助是赞赏。

answer1: 回答1:

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

The main problem behind this is first to detect overflow/underflows

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.

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 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 simillary 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 floating point divider 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

  • * 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 operation that increase the used bits of the result like:

  • 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 ...

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.

What to do if overflow/underflow is detected:

  1. change equation

    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.

  2. use bigger variable data type

    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 increasing numeric Integration precision

  3. stop iterating

    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.

我不是一个MATLAB用户所以要记住。

这背后的主要问题是首先检测溢出/下溢

这有时是很困难的因为他们也出现在其他情况下,当计算不归零或信息例如在数值积分溢出/下溢可能导致的结果是错的但仍然是一个非零的数。

以我的经验,我的帐篷里去看他们的进制表示的数字是有用的(除非你的硬件/软件计算使用十进制的基地内部的变量,这是罕见的因为大多数硬件/软件是二进制)。所以看到十六进制的形式和检测模式一样??????????。????fffffffffff?当你看?进制小数部分,发现许多fffff是附近最低的数字你数是最有可能的下溢或很近这一点。零点的个数或什么都结束时,通常是在每一次迭代饱和降低??????????。???fffffffffff妖术?。过饱和simillary而另一边是这样的:FFFFFFFFFFF.FFFFFF?????某些算法的十六进制在下一次迭代之前更精确地将这些数字循环起来,但在应用未知的时候,你总是需要检查一些已知的计算实例是否是这样的…这里看浮点分频器是一个很好的例子,使用这种技术的算法

另一种方法来检测溢出/下溢是结果数量的预测。例如

  • * sums the exponents together
  • / substract the exponents
  • sqrt halves the exponent
  • +,- can result in +1/-1 of the bigger exponent

所以,如果你处理大/小指数,你知道哪些操作可能导致溢出问题。

最重要的是下溢时可能出现的结果的精度不符合尾数。所以你需要小心的操作,增加所使用的结果:

  • 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位的尾数。

如果溢出检测:

  1. 变化方程

    如上所述,您可以切换到日志,可以轻松处理更大范围,但有其他问题。通常,算法的细微变化会导致不同因素缩放的结果,但子结果仍然在安全范围内,所以你只需要最后的结果,以恢复到危险范围。在改变方程时,应始终考虑结果的精确性和有效性。

  2. 使用较大的变量数据类型

    如果我记得正确的matlab有任意精度的数字,所以使用它们,如果需要的话。您还可以使用标准的浮点/双变量,并将值存储到更多的变量,如增加数字集成精度

  3. 停止迭代

    例如,一些算法使用系列如1 / 1!1 / 2!1 / 3!+…1 /氮!在某些情况下,如果你发现你的命中溢出/下溢subresult时停止迭代你还有计算相对准确的结果。别忘了不包括溢出subresults到最终的结果。

matlab  math  rounding  computer-science