COnstants

This is a discussion on COnstants within the C++ Programming forums, part of the General Programming Boards category; I have a file where I defined all my constants. Why can't i use trig Identities in this files #include ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with variables and constants.
    By dave1339 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 08:18 PM
  2. Replies: 7
    Last Post: 11-12-2008, 10:00 AM
  3. Constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2007, 11:51 PM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:29 PM
  5. winsock message constants
    By the Wookie in forum Windows Programming
    Replies: 10
    Last Post: 11-08-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21