user796530
user796530

Reputation:

calculating constant library functions at compile time

I want to use boltzmann constant in my functions. I am using the following code to declare the boltzmann constant

const double boltzmann_constant = 1.3806503 * pow (10,-23);

Will this get calculated at the compile time itself? If now, how should i ensure that it does get calculated at compile time? Any other method to declare the constant?

Upvotes: 3

Views: 1706

Answers (1)

wallyk
wallyk

Reputation: 57764

The pow() function is very unlikely to be calculated at compile time. However, the operation requested is directly expressible in scientific notation, a standard aspect of floating point numbers:

const double boltzmann_constant = 1.3806503e-23;

For a more complex situation, like sin(M_PI / 3), it can be useful to write a program to calculate and display such values so they can be edited into a program. If you do this, do everyone a favor and include a comment explaining what the constant is:

const double magic_val = 0.8660254037844385965883; // sin(M_PI / 3);

Upvotes: 4

Related Questions