Inline error

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
fuzzball27
Posts: 103
Joined: 29 Sep 2010, 17:14
Location: US

Inline error

Post 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) {
#----------------------------------------------------------
fuzzball27 >>-->
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Inline error

Post 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);
}
fuzzball27
Posts: 103
Joined: 29 Sep 2010, 17:14
Location: US

Re: Inline error

Post by fuzzball27 »

Thanks! It's working.
fuzzball27 >>-->
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Inline error

Post 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.)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Inline error

Post 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?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Inline error

Post 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Inline error

Post 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?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Inline error

Post 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Inline error

Post 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
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Inline error

Post 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests