**Al momento, stai seguendo questa domanda**

- Vedrai gli aggiornamenti nel tuofeed del contenuto seguito.
- Potresti ricevere delle e-mail a seconda delle tuepreferenze per le comunicazioni.

# Why Euler's number e = 2.71828... is not a built-in constant in MATLAB?

##### 24 Commenti

### Risposte (2)

##### 28 Commenti

- users who like to use exist at the start of their script or function to check for optional arguments typically do not check the return value (an observation based on this forum).
- any code which relies on assignin, load, etc. to dynamically create an en variable could quite by interpreted by the MATLAB engine as referring to the function/builtin, e.g.:https://www.mathworks.com/matlabcentral/answers/338434-how-do-i-force-matlab-treat-my-variable-range-as-a-variable-instead-of-a-function

- When I initially created this post, it was never my intention to replace the exponential function exp(x) with Euler’s number E, or en, etc., for values of x ~= 1. In fact, what I thought of was to use a simple notation like E to call Euler’s number rather than having to type exp(1), which as a mathematician I didn’t like much 😊. What I initially was thinking of was to use a symbol like E, for example, to do simple arithmetic operations on Euler’s number, operations like addition, subtraction, multiplication, and division. I also prefer to type E rather than typing exp(1) to invoke Euler’s number via MATLAB Command Window. If we define a function called E to provide Euler’s number, we can write something like (20*E+63)/(100*E-50) rather than writing (20*exp(1)+63)/(100*exp(1)-50), which doesn’t look much appealing to me as a mathematician. Of course, it would be of greater appealing if I could write (20*e+63)/(100*e-50), which is not possible as e is booked for 10. We can verify that this is fine through the following code:

- For more accuracy on arithmetic computations involving Euler’s number one can use the same algorithm used by MATLAB for computing exp(1). How MATLAB computes exp(1)? In fact, I don’t know.

- E^x should never replace exp(x), for x~=1. This never crossed my mind when I created this post.

- Generally speaking, any constant when raised to some general power would produce an error in a floating-point system. Consider pi for example and compare it with its exact value when raised to some general power:

- I taught MATLAB training courses for like 10 years now, and each time I teach I receive a few questions from students asking me what is the built-in constant notation for Euler’s number, and they would never become satisfied when I say we use the exponential function exp(1).

- When @John mentioned a list of constants to support his viewpoint, and gave an example on the golden ratio phi = (1 + sqrt(5))/2, I had a feeling from what I read that all the constants he mentioned were easy to compute according to my understanding of what he wrote. I did not make a research on this list but instead I was referring to his examples. So it turns out that one or two, etc. of this list were not easy to compute… this was not the point anyway. The point is, and let me rephrase it given the previous argument: “either MATLAB defines important constants all together, or don't, as some of these constants could easily be calculated using a huge library of valuable built-in functions already written by expert programmers.”

- When @John wrote “suppose the Mathworks were to suddenly introduce a variable with the name en? At that point, ALL legacy code that employs a variable named en that is something else will now potentially start to see bugs.” Correct… But would that mean that MATLAB should now never introduce new built-in functions to avoid conflict with legacy codes! I have to disagree with this opinion. MATLAB should continuously introduce new built-in functions to help researchers do their works with minimal programming efforts. The choice of new names must of course be reasonable and undergoes several checks and smart investigation and revisions before they could be introduced in future releases. Choosing just any new name randomly is a mistake that should be avoided.

- @John: “If you want some named constant, feel free to create it yourself.” Good advice from you. I did 😊. See https://www.mathworks.com/matlabcentral/fileexchange/77046-euler-s-number.

- @John: “e as a named constant in MATLAB will have only one common purpose - to compute e^x”. I largely agree with you on this point. Most well-known applications involving e would include e raised to some power x~=1. But for theoretical purposes this is not a restriction. As I mentioned before, one may do the 4 standard arithmetic operations on e (addition, subtraction, multiplication, and division) conveniently using a nice notation like E, or en, rather than exp(1). Imagine we had a differential equation and we want to solve it, then plot its solution at a large set of data points. Say, the solution of the differential equation was something like exp(1)*sin(x)/10, and we want to plot its values against x=linspace(0,2*pi,10000). Here is what I found when I compared the speed of coding using exp(1) and the simple function en that I created; see https://www.mathworks.com/matlabcentral/fileexchange/77046-euler-s-number:

- @Bjorn: Apparently my second point was misunderstood. My intention, and probably I didn’t phrase my words well enough when I wrote that point, is that calculating Euler's number using the very same method the usual exp(1) is calculated in MATLAB as a built-in function would produce the same accuracy and speed when exp(1) is involved in an operation. I did not intend when I mentioned these words to use E^x though for the reasons stated above. I meant usual arithmetic operations like addition, subtraction, multiplication, and division. Perhaps what caused the confusion was that I started my second point with a sentence that included exp(2.5) and E^2.5. So, I hope now my argument is clearer. To confirm, it never even crossed my mind to replace exp(x) with E^x for x~=1… never since the moment I was born and forever till the moment I die 😊.

- @Steven Lord: “As for pi being necessary, I think students the world around would expect to find pi in MATLAB.” Time for MATLAB Team to think of us as researchers as well. I, as an applied and computational mathematician, need valuable constants to be defined as well. Things like Euler’s number, or the golden ratio, and others would be great to have them all defined in MATLAB. I recall I developed an optimization method during my master studies and the work was published in Applied Mathematics and Computation. In this work I used the golden ratio to locate a minimizer of a function, but unfortunately this significant constant was not defined in FORTRAN—I did the work using FORTRAN back then-- so I had to calculate it and store it in the code. Having a list of important constants arising in various areas well defined in MATLAB can indeed be very convenient and efficient also, because now we can simply invoke them quickly as stored constant values without having to compute them.

- @Steven Lord: “I can ask Cleve if you feel it important though.” Thank you. No need for this. The subject is not about pi; it’s about other important constants that require further attention from MATLAB Development Team. I would be curious though if you could ask Cleve about why Euler’s number and other important constants were never introduced in MATLAB until now, and whether there is any intention to consider this subject in the future.

- To conclude, I’d generally say the following:

- Invoking E to work with is faster than having to calculate exp at x=1.
- I never thought of using E^x instead of exp(x) for x~=1. Not for a single moment. My 2ndpoint I mentioned before was unfortunately misunderstood. But I hope now it’s understood.
- Apparently most, if not all, real-life applications would involve exp(x), for some general power of x~=1. This could be a major reason for neglecting the idea of defining E for exp(1). However, one may still find at least theoretical problems that would involve only E, so using E would be more convenient and faster; see my earlier example on this matter.
- This is a fantastic forum… you made me love MATLAB more and more through your feedbacks and contributions. I used to be an expert of FORTRAN till I started to learn about MATLAB in 2010. I dropped FORTRAN the moment I discovered the beauty of this piece of art called MATLAB. One of the reasons that MATLAB is proud of is having a huge library of built-in functions. This is one of the greatest reasons that attracted me towards this giant software. I hope MATLAB Development Team would someday in the future listen to researchers like me and start defining valuable constants that are common in scientific disciplines and applications to add more and more power to its arsenal of built-in functions and constants.

- You are trying to show that a function that requires calculations is faster in practice than a function defined by a stored value!
- When you presented your code you made a huge mistake. You didn't account for function precedence order that MATLAB exercises when it searches/calls a function. Calling a built-in function would generally be faster than calling a user-defined function in large loops, even if the user-defined function was fater than the built-in function-- the reason is simple: MATLAB looks for the built-in function first. So by the time the large loop ends the execution of the operation that requires the built-in function would be relatively smaller than the one for the user-defined function. That's because of the function precedence exercised by MATLAB which is biased to built-in functions I suppose. To verify this point further, I know that variables is probably one of the first things, if not the first, that MATLAB looks for when calling a certain name in the MATLAB Command Window. Before assuming that a name matches a function, MATLAB checks for a variable with that name in the current workspace. So suppose I assign en to a variable called y. Let's check the timing again using your own example:

- If you check Wikipedia at https://en.wikipedia.org/wiki/E_(mathematical_constant), you'll see there that, and I'm quoting, 'The numerical value of e truncated to 50 decimal places is 2.71828182845904523536028747135266249775724709369995...'
- If you are interested to see the digits representation of Euler's number to 10,000 digits, see https://www.math.utah.edu/~pa/math/e.html.
- I used MATHEMATICA to display the digits of Euler's number to 21 digits precision and here's what I discovered
- If we display Euler's number to 16 accurate significant digits using MATLAB symbolic toolbox we get

##### 14 Commenti

### Vedere anche

### Tag

### Prodotti

### Community Treasure Hunt

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

Start Hunting!**Si è verificato un errore**

Impossibile completare l'azione a causa delle modifiche apportate alla pagina. Ricarica la pagina per vedere lo stato aggiornato.

Seleziona un sito web

Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .

Puoi anche selezionare un sito web dal seguente elenco:

Come ottenere le migliori prestazioni del sito

Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.

Americhe

- América Latina (Español)
- Canada (English)
- United States (English)

Europa

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia-Pacifico

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)