• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

special math operators in C++

JB3DG

Resource contributor
Messages
1,325
Country
southafrica
Hi guys

I was wondering if there is any conversion operator like rddg or dgrd in C++ and how to access it cuz i need it for my sin/cosin operations....any tips?
 
#define PI 3.14159265358979323
#define ARCUS (PI / 180.0)
#define D2A(a) ((a) * ARCUS)
#define A2D(a) ((a) / ARCUS)

usage:

double deg = 270.0;
double radians;

radians = D2A(deg );

deg = A2D(radians );
 
The include file math.h defines pi as M_PI.

Best practice now deprecates using #define for constants and prefers the const keyword instead.

Code:
const double ARCUS =  M_PI / 180.0;
 
Can someone tell me how to use the min/max operators in C++ when incrementing/decrementing?
 
Are you using the default gauges.h file or the latest from Dai Griffiths?
 
Can someone tell me how to use the min/max operators in C++ when incrementing/decrementing?

Why? It takes minimal code as well as minimal CPU time to do a quick test for a min or max limit either before or after the value change and is far much easier to 'read' a year after you've last seen the code.
 
Be careful because max/min are not standard C/C++ functions. In C they are often implemented as macros

Code:
#define max(a, b)(((a) > (b)) ? (a) : (b))

So

Code:
xMyVar = max(++xMyVar,xMax);

will increment xMyVar twice if it's the maximum but only once if it isn't
 
will increment xMyVar twice if it's the maximum but only once if it isn't

???

The way i recall it in XML is if i wanted to decrement something but have it stop at 0 i would use "0 max".

Vice versa if i wanted to stop incrementing at 10 i would use "10 min".

Now how does it differ in C++ aside from the RPN?
 
Last edited:
???

The way i recall it in XML is if i wanted to decrement something but have it stop at 0 i would use "0 max".

Vice versa if i wanted to stop incrementing at 10 i would use "10 min".

Now how does it differ in C++ aside from the RPN?

It differs, indeed.

My example was taken from a couple of standard macros defined in windef.h, which are max(a,b) and min(a,b)

The example given by mgh is for max():

Code:
[FONT="Courier New"]#define max(a, b) (((a) > (b)) ? (a) : (b))[/FONT]

and he is right in his observation to be careful, because if ++a (or ++b) is used like I did, the result would be wrong depending on which one of the values is returned.

So, the best solution is to code a function to specially handle this:

Code:
template <class T> const T tMax_mine( const T a, const T b ) 
{
  return (a>b)? a: b;    
}
Now you can use this one as you wish, for example:

Code:
int iMyVar = 2;

iMyVar = tMax_mine(++iMyVar, 1);  returns 3

iMyVar = tMax_mine(1,--MyVar); returns 1

iMyVar = tMax_mine(1,MyVar); returns 2


float fMyVar = 1.5;

float fMax= 2.2;

fMyVar = tMax_mine(++fMyVar, fMax); returns 2.5
etc, etc.

And the same for a min() equivalence.

Tom
 
And neither of those functions returns a value that's 'capped' at a max or a min value... which is what the '10 min' in XML would do... if your value exceeded 10... it would return 10 anyway.

Now, if one of the values passed to the min function was 10... it would either return 10 or your value that was less than 10... but it wouldn't prevent your variable from being incremented.
 
If max is implemented as a macro then

Code:
xMyVar = max(++xMyVar,xMax);

expands to

xMyVar =  (((++xMyVar) > (xMax)) ? (++xMyVar) : (xMax))

++xMyvar is always evaluated and incremented once when it's tested against xMax and again if it's the maximum when it's assigned to XMyVar

EDIT

This is dealt with in the JOINT STRIKE FIGHTER AIR VEHICLE C++ CODING STANDARDS FOR THE SYSTEM DEVELOPMENT AND DEMONSTRATION PROGRAM by Lockheed Martin.

AV Rule 29
Inline functions do not require text substitutions and are well-behaved when called with arguments (e.g. type-checking is performed).
Example: Compute the maximum of two integers.

#define max (a,b) ((a > b) ? a : b) // Wrong: macro

inline int32 maxf (int32 a, int32 b) // Correct: inline function
{
return (a > b) ? a : b;
}

y = max (++p,q); // Wrong: ++p evaluated twice

y=maxf (++p,q) // Correct: ++p evaluated once and type
// checking performed. (q is const)
 
Last edited:
If max is implemented as a macro then

Code:
xMyVar = max(++xMyVar,xMax);

expands to

xMyVar =  (((++xMyVar) > (xMax)) ? (++xMyVar) : (xMax))

++xMyvar is always evaluated and incremented once when it's tested against xMax and again if it's the maximum when it's assigned to XMyVar

Exactly. But the template function lets you use the inc/dec risk free.

And neither of those functions returns a value that's 'capped' at a max or a min value... which is what the '10 min' in XML would do... if your value exceeded 10... it would return 10 anyway.

Now, if one of the values passed to the min function was 10... it would either return 10 or your value that was less than 10... but it wouldn't prevent your variable from being incremented.

I guess I should post both XML and C++ versions of Naruto-kun's own examples:

Var receives a number greater or equal than 0

Code:
In XML

[B][FONT="Courier New"]Var -- 0 max (>Var)[/FONT][/B]

in C++ (with template function example)

[B][FONT="Courier New"]Var = tMax_mine(--Var,0); [/FONT][/B]

Var receives a number lower or equal than 10

Code:
in XML

[B][FONT="Courier New"]Var ++ 10 min (>Var)[/FONT][/B]

in C++ (a similar template function returning (a<b) ? a: b )

[B][FONT="Courier New"]Var = tMin_mine(++Var,10); [/FONT][/B]

Hope this illustrates better both situations.

Tom
 
C generally doesn't support templates, only C++ does that.

Of course, and we're talking about C++ functions precisely as per Naruto-kun references. Other variants of C and/or languages will difer, but I guess that's beyond the scope of this thread.

Tom
 
Back
Top