# Is the exponential function, e^x, very expensive to compute in Matlab and harmful to my computer?

Is the exponential function problematic and very expensive to compute in Matlab?

When I write a new term for my model of ODEs that has an exponential term in it, the program almost never finishes debugging, and I hear the fan on my computer go crazy. So I quickly stop debugging and remove that term in my model. And then all is well again.

Will using the exp() function even physically harm my computer, say, for instance, burn out my hard drive?

Thanks,

• Do you know what is the argument of the exponent for which this takes time ? In one of my fortran code, I have found that computing $x^y$ when $x$ is close to 1 is VERY expensive. This seems to be known problem with glibc, see stackoverflow.com/questions/9272155/… and entropymine.com/imageworsener/slowpow. clang seems not to have this problem. Commented Nov 1, 2017 at 3:47
• side note: it's funny you are concerned that you are harming your computer. Modern CPUs slow down when they detect it's too hot. Commented Nov 1, 2017 at 13:36
• @PraveenChandrashekar Did you use expm1? Commented Nov 2, 2017 at 20:06
• @PraveenChandrashekar It should optimize to some floating point operand. If it doesn't, what if you would substitute it with logarithm + multiplication?
– user9927
Commented Nov 4, 2017 at 12:55

Computing the term $e^x$ is definitely significantly more expensive than computing a lower-order polynomial -- say $x^4$. But it may be ten to 100 times more expensive at most, not "crazy" expensive. So I suspect that if Matlab takes forever to compute something, then that's because the character of your ODE changes significantly. For example, the presence of that term may make your ODE "stiff" and require a significantly smaller time step than otherwise. That would mean that it's not the evaluation of the right hand side that's expensive, but simply that you need to do many more time steps.
• This is very strange. In C (glibc, to be precise), the exponent is computed in O(1). Not in O(logN), pure constant time, with no loops or subroutine calls. Maybe it can't be used via SIMD and thus its slower for vectors and matrices compared to repeated multiplication. Commented Jan 16 at 18:21
• @PeterZaitcev I said nothing to the contrary. 10-100 times as expensive than a single multiplication is still $O(1)$. Commented Jan 16 at 23:37