Page 2 of 2
Re: Inline error
Posted: 21 May 2011, 21:15
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
Re: Inline error
Posted: 21 May 2011, 23:53
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
Re: Inline error
Posted: 22 May 2011, 00:43
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.
Re: Inline error
Posted: 22 May 2011, 06:28
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
Regards,
Xander
Re: Inline error
Posted: 22 May 2011, 09:02
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]
Re: Inline error
Posted: 22 May 2011, 21:58
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:
So I'm not entirely sure about this, unless the compiler is good enough to optimize that.
Re: Inline error
Posted: 24 May 2011, 17:23
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