# COnstants

• 09-01-2007
shuo
COnstants
I have a file where I defined all my constants. Why can't i use trig Identities in this files

#include <math.h>

//Define constants

#define gamma 1.4
#define Pr 0.71
#define mu_ref 1.7894e-5 // kg/(m*s)
#define T_ref 288.16 // K
#define R_gas 287
#define incidence 11

#define P1 0.714 // N/(m^2)
#define P2 P1*2.153 // N/(m^2)
#define rho1 1.225 // kg/(m^3)
#define rho2 rho1*1.707 // kg/(m^3)
#define Mach 2.9 // m/s
#define u1 sqrt(gamma*R_gas*T_ref) //m/s
#define u2 u1*1.707*sin(incidence) m/s */
#define u_ratio sqrt(1.707)
#define Theta atan(u_ratio) - atan(1/u_ratio)
#define v1 0 // m/s
#define v2 u2*sin(Theta) // m/s
• 09-01-2007
matsp
I don't see why can't use trig functions in this code - what problem are you seeing with it.

By the way, if you are using C++ (which is the forum you posted in), then the recommended form for new code is #include <cmath> instead of <math.h>.

I would also prefer to see constants in this form, again, if you are using C++ rather than traditional C:
Code:

```const double gamma =1.4; const double Pr=0.71; const double mu_ref=1.7894e-5; // kg/(m*s) ...```
This makes the constants have a type as well as a value, which means that the compiler has a better chance to help you get the code working right in the way that it gives warnings and uses function calls.

I'm not entirely sure if you can do this by assigning the result of functions, you may have to resort to one of two things: keep it as a define, or make it a const function that has inline prefix. For example:
Code:

```inline const double uratio() {   return sqrt(1.707); }```
If the compiler is good, it will calculate sqrt of 1.707 as a constant, and replace the whole function with that constant value. If the compiler isn't that clever, it probably won't help to have the macro replacement with sqrt(1.707), but you need to calculate yourself the result of sqrt(1.707).

--
Mats
• 09-01-2007
CornedBee
You can assign the result of functions to constants in C++, but they become dynamically initialized, which presents some problems. Mostly when referring to them from other files.

They are also no longer eligible for a number of situations where a constant is required, but this problem applies mostly to integers, because these situations are things like case constants, non-type template parameters and array sizes.