Using double precision, find x for which exp(x) doesn't overflow.

Using double precision, find x for which exp(x) doesn't overflow.

4 Comments

This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Assuming that you are asked to find the largest finite x with that property, as a hint: work in reverse to find a good starting point for your search.
Expanding on Steven's advice:
You could also carefully read the Wikipedia page for IEEE double precision floating point notation. That way you don't even have to empirically find what value is the largest x that will not overflow. Then you can test if that is true in Matlab.
This is what I have. I'm not sure if this is the best way to go though.
expn=0;
x=1:1000;
while expn < realmax
expn=(exp(x));
maxexpn = double(expn);
xx = double(log(maxexpn));
mexp = max(maxexpn(~isinf(maxexpn)));
mx = max(xx(~isinf(xx)))
abserr = abs(mexp-realmax)
relerr = (abs(mexp-realmax))/realmax
end
Is there a shorter way to go about it?
I don't really understand what you're attempting to do with the while loop, but yes, there is a faster way. It requires you to think a bit more.
log2(realmax)
ans = 1024
OK, so we are looking for x where e^x is at most 2^1024.
With a little bit of algebra that should not be too hard to solve. Can you see how?

Sign in to comment.

Asked:

on 24 Mar 2021

Commented:

Rik
on 24 Mar 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!