Inline error

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: Inline error

Post by gloomyandy »

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)
I think there is an error here, pretty sure it should be:

Code: Select all

#define min3(a, b, c) (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c)
Andy
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Inline error

Post by afanofosc »

muntoo wrote: 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.
Actually, since they are macros the nested min version (which I would recommend using instead of a min3 or min7 or custom min function for some other number of parameters) expands to exactly the same code as the custom min macro.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Inline error

Post by mattallen37 »

What is all this about "min", "max", "min3", and "max3"? Can you please explain it?

Also, please explain what the different parts of those macros do (like the "?" and ":").

Sorry for being so OT, but I like to learn new things.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Inline error

Post by mightor »

@ Andy, I will test your code. I just found these ones online. I did only marginal testsing, hehe

@ Muntoo, I wish I could use nested #defines, but the ROBOTC compiler barfs when I do that

@ Matt, this is the conditional expression. Consider the following:

Code: Select all

int foo = (a > b) ? a : b;
1 ---------^^^^^
2 ------------------^
3-----------------------^  
Part 1: the condition, in this case "is a greater than b"
Part 2: the value the expression will be evaluated to should the condition be true
Part 3: the value the expression will be evaluated to should the condition be false

It's functionally equivalent to

Code: Select all

if (a > b)
  foo = a;
else
  foo = b;
Regards,
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)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Inline error

Post by HaWe »

[OT]
@John:
Actually, since they are macros the nested min version (which I would recommend using instead of a min3 or min7 or custom min function for some other number of parameters) expands to exactly the same code as the custom min macro.
sry but what means "nested min (max) versions"? Already built-in? where? how? or how exactly to use min3...min7 or what exactly instead of this?
Don't understand what you actually mean with your posting...
[/OT]
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Inline error

Post by muntoo »

doc-helmut wrote:sry but what means "nested min (max) versions"? Already built-in? where? how? or how exactly to use min3...min7 or what exactly instead of this?
What he means is, min3(a,b,c) will compile to the same code as min(a,min(b,c)), pertaining to the code in this post.

Code: Select all

min3(a,b,c)
min(a,min(b,c))
min(a,(b<c?b:c))
(a<(b<c?b:c)?a:(b<c?b:c))
Optimized: t=b<c?b:c; a<t?a:t
But the "handcoded" min3(a,b,c) is this:

Code: Select all

(a<b?(a<c?a:c):(b<c?b:c))
So I'm not entirely sure about this, unless the compiler is good enough to optimize that.
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc_99
Posts: 15
Joined: 26 Sep 2010, 18:18

Re: Inline error

Post by afanofosc_99 »

Yeah, I don't know what I was thinking. The hand coded min3 would be more optimal than the nested macro version. I would still use the nested macro version rather than writing multiple "min" functions that take various numbers of parameters.

John Hansen
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 24 guests