Page 1 of 2

Inline error

Posted: 20 May 2011, 20:12
by fuzzball27
I'm running a couple of inline functions to determine minimum and maximum values, and I'm getting an error that says "variable name expected" for the functions...any ideas as to why it isn't working? (I got these functions from Power Programming)

Code: Select all

inline int min(int v1, intv2) {
	return (v1 < v2) ? v1 : v2;
}

inline int max(int v1, int v2) {
	return (v1 > v2) ? v1 : v2;
}
# Status: NXC processing global declarations
# Status: NXC processing pr# Error: Variable name expected
File "../build/PROGRAM.nxc" ; line 15
# inline void min(int v1, intv2) {
#----------------------------------------------------------

Re: Inline error

Posted: 20 May 2011, 20:23
by HaWe
a typo: intv2 => int v2.
And then maybe try it like this way (int or long instead of float):

Code: Select all

inline float min(float a, float b) {
  return (a<b? a: b);
}

inline float max(float a, float b) {
  return (a>b? a: b);
}

Re: Inline error

Posted: 20 May 2011, 20:45
by fuzzball27
Thanks! It's working.

Re: Inline error

Posted: 21 May 2011, 02:59
by muntoo
I'd rather use #defines, as we don't have voids as function parameters yet. This ensures "compatibility" (call it what you will) between all types which support the < and > operators:

Code: Select all

#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
IMHO, I think this should be built into NXCDefs.h. (Or NXTDefs.h, NBCCommon.h, take your pick.)

Re: Inline error

Posted: 21 May 2011, 07:12
by HaWe
muntoo,
I don't understand what should be better with your #inline definition and *our* inline int or inline float? Can you give me some programming examples?

Re: Inline error

Posted: 21 May 2011, 08:04
by muntoo

Code: Select all

float x = min(20.2, 20.1);
// x = 20;
If min() was:

Code: Select all

inline long min(long a, long b)
{
    return(a < b ? a : b);
}
It wouldn't give you the expected result.

If we'd used inline float min(float a, float b) instead, min() would be limited in range whenever you wanted to use a long.

-----

If we had C++, I could use templates.

Re: Inline error

Posted: 21 May 2011, 08:39
by HaWe
I don't know if I understand you correctly, but I guess what you might mean is:
if we use
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)

we will be not limited to any pre-defined varible types like int, long, float, this macro will work with each variable type for each macro call - correct?

Re: Inline error

Posted: 21 May 2011, 09:57
by muntoo
doc-helmut wrote:we will be not limited to any pre-defined varible types like int, long, float, this macro will work with each variable type for each macro call - correct?
Yeah, that's what I mean.

Re: Inline error

Posted: 21 May 2011, 18:33
by mightor
Here are some #defines for min and max for 3 numbers:

Code: Select all

#define min3(a, b, c) (a < b) ? ((a < b) ? a : c) : ((b < c) ? b : c)
#define max3(a, b, c) (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)
I use these and the ones in the earlier post in my driver suite.

- Xander

Re: Inline error

Posted: 21 May 2011, 21:08
by muntoo
mightor wrote:Here are some #defines for min and max for 3 numbers:

Code: Select all

#define min3(a, b, c) (a < b) ? ((a < b) ? a : c) : ((b < c) ? b : c)
#define max3(a, b, c) (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)
For reference, the exact equivalents are:

Code: Select all

#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)

#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
Although they're less optimized.