0

Can anyone suggest how can I find the largest value for which the function exp in Matlab doesn't overflow, and the smallest for which it overflows.
Thank You!

Iulian Rosca
  • 1,005
  • 4
  • 15
  • 30

2 Answers2

1

Try looking at the function realmin() or intmin(). These also have max partners for example:

realmin('double')
realmax('single')
intmin('int8')
intmax('int64')

This tells you the smallest value that that can be represented in the MATLAB type. Any number below this will be set to this min value for the given type.

CatsLoveJazz
  • 829
  • 1
  • 16
  • 35
  • 2
    I can see how one could use this to answer what I think is OP's question. But I can also see how OP might not be able to figure out the rest of the solution for herself. – High Performance Mark Apr 02 '14 at 10:59
  • You are correct, I had not fully answered the question, only provided information that would set OP on the right track. – CatsLoveJazz Apr 02 '14 at 11:55
1

Trying

T = log(realmax('double'));

exp(T+255*eps)

gives

1.7977e+308

and

exp(T+256*eps)

gives

inf
Drake
  • 857
  • 5
  • 10
  • can you explain where did you get 255 from , and the logic behind it, why did you calculate log of realmax? – Iulian Rosca Apr 02 '14 at 11:11
  • Using "bisection". That is, I started with `exp(T+i*eps)`, where `i=1000`, and checked that it leads, indeed, to overflow. Then set `i=500`; overflows again. Set `i=250`; it doesn't overflow. Then `i=375` and so on. In this case, a quick and dirty approach is, of course, to just increment `i` in a loop until it overflows, but, in my humble opinion, the philosophy of "bisection" is useful to know in general. I hope that helps. – Drake Apr 02 '14 at 11:19
  • When you have an equation of the form `exp(x)=y`, taking the `log` of both sides gives you `x = log(y)`. Both `exp` and `log` are (strictly) increasing functions, so, since you want the largest `x` you can use in `exp(x)`, you want the largest `y`. Now, to trace the logic backwards, we found the largest `y` by asking Matlab the answer to `realmax('double')` (assuming we will be using `double` precision). Call this largest value y_M. For what x_M we have `exp(x_M)=y_M`? We take the `log` of both sides. I hope that helps. If I may suggest something, do it on a piece of paper. – Drake Apr 02 '14 at 11:35
  • Thanks, it makes perfect sense! – Iulian Rosca Apr 02 '14 at 11:41