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

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

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.

• `*` 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