CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   C programme issue for CFD (https://www.cfd-online.com/Forums/main/12623-c-programme-issue-cfd.html)

john November 29, 2006 08:12

C programme issue for CFD
 
I am writing my own CFD code using c language. I want to create a function which can deal with both single and double precision arrays such that

grad_cal1(fi1) where fi1 is an array of float

grad_cal2(fi2) where fi2 is an array of double

void grad_cal1(float *fi){a.h}

void grad_cal2(double *fi){a.h}

Inside the function, the programme is exactly the same. Is there any way to call the same function instead of two?

pc November 29, 2006 08:34

Re: C programme issue for CFD
 
Why not simplify things by making the whole code double precision? Why do you really need to mix variable types?


Markus Lummer November 29, 2006 09:15

Re: C programme issue for CFD
 
Hello John,

in C it is possible to compile the function with appropriate preprocessor macros, e.g.

#define REAL float

or

#define REAL double

and the corresponding definitions in the source, e.g.

void grad_cal1(REAL *fi){a.h}

Another (and much better) possibility would be using C++ templates.

Regards, Markus


Mani November 30, 2006 12:27

Re: C programme issue for CFD
 
Markus' preprocessor approach lets you create either version independently at compile-time. To have both options simultaneously at run-time you are basically talking about object oriented programming: This is a case for C++, not C.


All times are GMT -4. The time now is 14:40.