# Result of exponentials are (sometimes) different from Matlab

**URL:** <https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241>\
**Category:** Numerics\
**Created:** [February 15, 2024, 7:55am UTC](https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241 "2024-02-15T07:55:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonw1](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@simonw1](https://discourse.julialang.org/u/simonw1)\
**Post date:** [February 15, 2024, 7:55am UTC](https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241/1 "2024-02-15T07:55:41Z")

</div>

I’m trying to port an algorithm originally written in matlab to Julia and I’m getting a weird error.  
For some values and some exponents, the results of raising the value to the exponent differs between matlab and Julia. In the example below, I get an error of around 1e-16 which is not huge in absolute terms but large enough that it affects the output of the algorithm.

```julia
val=.93333333333333333
ex = 3
val^ex-mat"$val^double($ex)"

```

Further, for `exp=0,1,2,4,5,7,8,9` the error is 0, but for whatever reason it seems to be identical for the powers 3 and 6.  
I’m looking for an explanation as to why this happens, and ideally a workaround so I can complete my porting with zero deviation.  
I have already tried:  
using BigFloat in Julia and using various methods for computing the exponential (val_val_val, Float64(val)) with (little or) no effect.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [February 15, 2024, 8:19am UTC](https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241/2 "2024-02-15T08:19:26Z")

</div>

Note that 1e-16 is on the order of the machine precision for Float64. If this difference is important for your algorithm you should not be using Float64 in the first place. The difference between Matlab and Julia comes from different rounding but I cannot tell you which of the results is more accurate.

---

<div class="post-metadata">

**Author:** ![simonw1](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@simonw1](https://discourse.julialang.org/u/simonw1)\
**Post date:** [February 15, 2024, 8:21am UTC](https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241/3 "2024-02-15T08:21:06Z")

</div>

Thanks, my bad.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 15, 2024, 8:31am UTC](https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241/4 "2024-02-15T08:31:45Z")

</div>

Implementations of special functions, such as `pow`, tend to have compromises between speed and accuracy. I don’t know, neither for Julia nor for Matlab, what promises they make about accuracy and exact rounding for this function.

My first approach would be to try to determine if either of them is consistently giving more correct results, and if that is the case, see if the other one can use a higher precision workaround to match up.

A second option is to see if either of them is calling some specific library for the special function and see if there is a way to get the other one to call the same library.

If you only need a limited range of integer exponents, write your own function which repeatedly multiplies numbers to give the power and use it in both Julia and Matlab. Those ought to agree.

More difficult options would be to port Julia’s power function to Matlab, or use StaticCompiler to make a small library and call it from a Matlab mex function.

But if this is just a small part of a bigger algorithm, you will likely find that it’s extremely difficult to get two implementations to give exactly the same results when floating point numbers are involved, and accept small numerical deviations. The key is to design your algorithm so that the deviations don’t increase out of control along the way.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [February 15, 2024, 9:58am UTC](https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241/5 "2024-02-15T09:58:31Z")

</div>

This is a great webinar by Avik Sengupta on floating point numbers. He defines a useful function called floatbits which you might try to show how floating point numbers are actually represented

[https://juliahighperformance.com/code/Chapter05.html](https://juliahighperformance.com/code/Chapter05.html)

> **[Everything You Wanted to Know About Floating Point Numbers, And Didn't Know...](https://juliahub.com/company/resources/webinar/everything-you-wanted/)**
>
> Everything You Wanted to Know About Floating Point Numbers, And Didn't Know Who to Ask - JuliaHub

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 15, 2024, 1:43pm UTC](https://discourse.julialang.org/t/result-of-exponentials-are-sometimes-different-from-matlab/110241/6 "2024-02-15T13:43:40Z")

</div>

Could you provide a few examples for those of us who do not have MATLAB readily at hand?
