← Back to team overview

kicad-developers team mailing list archive

Re: Math function call note.

 

--- In kicad-devel@xxxxxxxxxxxxxxx, Wayne Stambaugh <stambaughw@...> wrote:

> being used as and casts them to the appropriate type. I am actually
> surprised that GCC doesn't complain about this as all of the math
> functions defined in math.h in Linux and MinGW have a float, double, and
> long double definition.

Actually I think that behaviour is underspecified in the standard... even gcc treats it differently depending on the version

What we are seeing is a conflict between the type promotion (from C) and function call overloading (in C++ only).

The compiler tries to match a function signature for overloading, USING IMPLICIT TYPE PROMOTION. The trouble is when more than one promotion works andmatches...

Example:

void f(long x);
void f(double x);
f(20);

The 20 is actually an int :P. What function will be called? 
MSVC probably complains about it. gcc has a priority order so that favours the double one.

Of course, type casting operator are tried too (unless being declared explicit!).

IMHO MSVC has prototypes for both double and long double versions or something similar, so it complains...

Just to finish on the issues... operating an int with a double (and just a numeric literal with point or exponent IS a double) promotes the int to a double but only from that point!

int a, b;
double c = a/b*1.2;

The a/b division is made with int semantics (probably wrong), the result promoted to double and multiplied by 1.2.

int a, b;
double c = 1.2*a/b;

Promotes a for multiplication with 1.2, the result forces promotion of b and the whole expression is evaluated with double semantics...

This is actually 50-years-old C behaviour but I still see that error so I thought it was a useful refresher:D
(next lesson: the astonishing priority order of & and >> :D)








References