2015-10-07 22:27:19 8 Comments

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.

### Related Questions

#### Sponsored Content

#### 17 Answered Questions

#### 32 Answered Questions

#### 47 Answered Questions

#### 3 Answered Questions

#### 2 Answered Questions

### [SOLVED] Frobenius norm overflow issue with my implementation in MATLAB

**2017-10-13 22:16:33****Brainpower2049****380**View**0**Score**2**Answer- Tags: matlab math computer-science

#### 4 Answered Questions

#### 3 Answered Questions

### [SOLVED] Overflow vs Inf

**2014-10-27 03:03:36****Dante****392**View**4**Score**3**Answer- Tags: c matlab exception floating-point floating-point-exceptions

#### 1 Answered Questions

### Dealing with under flow while calculating GMM parameters using EM

**2014-07-01 12:39:41****Hillel****204**View**0**Score**1**Answer- Tags: algorithm matlab signal-processing

## 1 comments

## @Spektre 2015-10-08 04:30:18

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

The main problem behind this is first to detect overflow/underflowsThat 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:

when you look at the fractional part and detect that many

`FFFFF`

's are present near the lowest digits then you number ismost likelyunderflowing or is very near that point. The number of zeros or what ever at the end is usually decreasing with each iteration saturating to:The overflows are saturated similarly but on the other side like this:

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

`*`

sums the exponents together`/`

substract the exponents`sqrt`

halves the exponent`+`

,`-`

can result in`+1/-1`

of the bigger exponentSo 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`

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:change equationAs 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.use bigger variable data typeIf 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:stop iteratingFor example some algorithms use series like:

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.

## @Andras Deak 2015-10-08 07:46:38

Very nice answer (and +1 for the lack of extensive bullets:). Just a matlab note: matlab works with doubles by default, but there's a symbolic math toolbox which allows variable-precision arithmetic, using the command

`vpa`

(to which @LuisMendo) referred in his comment. That would allow to represent`x=vpa('2e160')`

exactly, but note that this would not be equal to`x=vpa(2e160)`

, the latter having been already represented as a double (losing precision).## @Adriaan 2015-10-08 16:28:32

Nice work! apparently I just answered a duplicate where you can find the MATLAB codes for this one.