Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate Euler constant or Euler powered in C++?

I am trying to find the more 'natural' way to use the number e in C/C++. I am focused on calculating the function e^n.

I think that 'cmath', by default, does not provide support for both (function and constant). However, it can be enabled to include constants defined by the compiler, in this case, M_E. This can be done by including the statement #define _USE_MATH_DEFINES.

On the other hand, e can be defined as a constant:

#define E 2.71828182845904523536;

or

const double EULER = 2.71828182845904523536;

Said this. Which one is the most 'standard' way to approach to this mathematical constant? Is it any other library?

like image 329
Antonio Avatar asked Sep 12 '13 20:09

Antonio


People also ask

How is Euler's constant calculated?

We've learned that the number e is sometimes called Euler's number and is approximately 2.71828. Like the number pi, it is an irrational number and goes on forever. The two ways to calculate this number is by calculating (1 + 1 / n)^n when n is infinity and by adding on to the series 1 + 1/1! + 1/2!

How do you find e power?

e is the base of the natural logarithm. We use e in the natural exponential function (eˣ = e power x). In the eˣ function, the slope of the tangent line to any point on the graph is equal to its y-coordinate at that point. (1 + 1/n)ⁿ is the sequence that we use to estimate the value of e.

How do you calculate Euler's number in C++?

#include <iostream> #include <iomanip> using namespace std; int main () { int A, B; double E, D; for (A=1; A<=15; A++ ) { D=1; for (B=1; B<=A ; B++ ) { D = D * B; } E = E + 1 / D; } cout<<setprecision(10)<<"Estimated Value of e is "<< E + 1 <<endl; system("pause");

What is the constant value of e?

The exponential constant is an important mathematical constant and is given the symbol e. Its value is approximately 2.718. It has been found that this value occurs so frequently when mathematics is used to model physical and economic phenomena that it is convenient to write simply e.


2 Answers

If you can avoid using a preprocessor symbol you should. It will cause you trouble when you least expect it. E is likely going to be a variable.

Proposed solution:

#include <cmath>
const double EulerConstant = std::exp(1.0);

The advantage of calculating the constant instead of assigning a floating point literal is that it will produce a result with precision that matches the precision of the double data type for your particular C++ implementation. And it removes the possibility of introducing an error by accidentally skipping a digit.

As illustrated above, <cmath> does declare std::exp, so there is no need for you to roll your own.

like image 142
IInspectable Avatar answered Oct 01 '22 16:10

IInspectable


C++20 std::numbers::e

C++20 has also added an e constant to the standard library: http://eel.is/c++draft/numbers

I expect the usage to be like:

#include <math>
#include <iostream>

int main() {
    std::cout << std::numbers::e << std::endl;
}

I'll give it a try when support arrives to GCC, GCC 9.1.0 with g++-9 -std=c++2a still doesn't support it.

The accepted proposal describes:

5.0. “Headers” [headers] In the table [tab:cpp.library.headers], a new header needs to be added.

[...]

namespace std {
namespace math { 
template<typename T > inline constexpr T e_v = unspecified;
inline constexpr double e = e_v<double>;

There is also a std::numbers::pi of course :-) How to use the PI constant in C++

These constants use the C++14 variable template feature: C++14 Variable Templates: what is their purpose? Any usage example?

In earlier versions of the draft, the constant was under std::math::e: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0631r7.pdf