NXC: functions with different numbers of parameters?
NXC: functions with different numbers of parameters?
hi,
how is it possible to define a macro or to declare a function with optionally different numbers of parameters passed to it?
e.g., like to mimic TextOut where it's possible to have optionally 3 or 4 parameters:
TextOut(0,0, "hello");
vs.
TextOut(0,0, "hello", DRAW_OPT_INVERS);
I want to use this feature e.g., to pass diffenent numbers of variables to a max function (or macro)
float max(float f1, float f2)
vs.
float max(float f1, float f2, float f3)
or to pass different numbers of values to print to screen
#define printf_(fmtstr, val1)
vs.
#define printf_(fmtstr, val1, val2)
how is it possible to define a macro or to declare a function with optionally different numbers of parameters passed to it?
e.g., like to mimic TextOut where it's possible to have optionally 3 or 4 parameters:
TextOut(0,0, "hello");
vs.
TextOut(0,0, "hello", DRAW_OPT_INVERS);
I want to use this feature e.g., to pass diffenent numbers of variables to a max function (or macro)
float max(float f1, float f2)
vs.
float max(float f1, float f2, float f3)
or to pass different numbers of values to print to screen
#define printf_(fmtstr, val1)
vs.
#define printf_(fmtstr, val1, val2)
-
- Posts: 220
- Joined: 23 Jan 2012, 17:07
- Location: Round Rock, TX
Re: NXC: functions with different numbers of parameters?
This is from the help for "function":
In your case of "max", you could make the default values very large negative numbers.NXC supports specifying a default value for function arguments that are not struct or array types. Simply add an equal sign followed by the default value. Specifying a default value makes the argument optional when you call the function. All optional arguments must be at the end of the argument list.
McSummation aka James
http://www.mcsummation.com/Mindstorms/
http://www.mcsummation.com/Mindstorms/
Re: NXC: functions with different numbers of parameters?
I know that this is the default setting,
but as I wrote: e.g., TextOut already allows different numbers of parameters, and what I want is : to mimic this feature.
So how is this done by TextOut (or NumOut, or maybe others) and how can use this trick for my own functions?
but as I wrote: e.g., TextOut already allows different numbers of parameters, and what I want is : to mimic this feature.
So how is this done by TextOut (or NumOut, or maybe others) and how can use this trick for my own functions?
-
- Posts: 48
- Joined: 12 Jan 2011, 18:40
- Contact:
Re: NXC: functions with different numbers of parameters?
Code: Select all
float max(float f1, float f2, float f3 = FLOAT_MIN, float f4 = FLOAT_MIN, float f5 = FLOAT_MIN);
You could call this function with two, three, four or five parameters:
Code: Select all
float max(20.5, 3.8);
float max(7.1, 12.4, 5.9);
float max(1.25, 33.1, 2.8, 2.9, -4.0);
link to my youtube channel under my avatar!
Re: NXC: functions with different numbers of parameters?
hi,
thx, it works - more or less - but sometimes with side effect errors:
Now I'm curious if there is a different trick also for #define macros....?
thx, it works - more or less - but sometimes with side effect errors:
Code: Select all
#define FLT_MIN 1E-37
#define FLT_MAX 1E+37
float max(float f1, float f2, float f3 = FLT_MIN, float f4 = FLT_MIN, float f5 = FLT_MIN) {
float flarr[5];
flarr[0]=f1; flarr[1]=f2; flarr[2]=f3; flarr[3]=f4; flarr[4]=f5;
return ArrayMax( flarr, 0, 5 );
}
task main() {
NumOut(0,8, max(7.1, 12.4, 5.9, 18.6)); // works, but NumOut does not display floats, just ints!
NumOut(0,0, max(7.1, 12.4, 5.9, 1E+9)); // compiler error (',' expected)
for(;;);
}
but apart from that, the trick seems to be working# Error: ',' expected
File "C:\Programme\BricxCC\Untitled1.nxc" ; line 15
# NumOut(0,0, max(7.1, 12.4, 5.9, 1E+
#----------------------------------------------------------
Now I'm curious if there is a different trick also for #define macros....?
Code: Select all
#define printf_(...?,...?,...?..) {.?.?.?.?.?. }
int i=3, j=12000;
float f=2.345;
printf_("%3d", i); getchar();
printf_("%9.3f", f); getchar();
printf_("%3d %7d", i, j); getchar();
printf_("%8.3f %4d", f, j); getchar();
printf_("%3d %8.2f %7d", i,f,j);
-
- Posts: 220
- Joined: 23 Jan 2012, 17:07
- Location: Round Rock, TX
Re: NXC: functions with different numbers of parameters?
I think FLT_MIN should be -1E+37.
1E-37 is the smallest float, not the minimum. You want the 2 numbers to be the farthest from zero.
1E-37 is the smallest float, not the minimum. You want the 2 numbers to be the farthest from zero.
McSummation aka James
http://www.mcsummation.com/Mindstorms/
http://www.mcsummation.com/Mindstorms/
Re: NXC: functions with different numbers of parameters?
yes, sure, you're right, thx!
(indeed C libraries use FLT_MIN as the smallest number close to zero, not the absolute max negative value what I actually wanted)
But if correct it, I get a new error: Invalid parameter syntax with default values
having instead
the compiler of course produces the same error...
so what's going wrong here?
(indeed C libraries use FLT_MIN as the smallest number close to zero, not the absolute max negative value what I actually wanted)
But if correct it, I get a new error: Invalid parameter syntax with default values
Code: Select all
// faulty FLT_MIN
#define FLT_MIN -1E37
#define FLT_MAX 1E37
float max(float f1, float f2, float f3 = FLT_MIN, float f4 = FLT_MIN, float f5 = FLT_MIN) {
float flarr[5];
flarr[0]=f1; flarr[1]=f2; flarr[2]=f3; flarr[3]=f4; flarr[4]=f5;
return ArrayMax( flarr, 0, 5 );
}
task main() {
NumOut(0,8, max(7.1, 12.4, 5.9, 18.6)); // if it works, then NumOut does not display floats!
//NumOut(0,0, max(7.1, 12.4, 5.9, 1E+9)); // compiler error
for(;;);
}
Code: Select all
#define FLT_MAX 1E37
float max(float f1, float f2, float f3 = -FLT_MAX, float f4 = -FLT_MAX, float f5 = -FLT_MAX) {
\\...
# Error: Invalid parameter syntax with default values
File "c:\Temp\temp.nxc" ; line 5
# float max(float f1, float f2, float f3 = -9.99999999999999954E36, f
#----------------------------------------------------------
# Error: Not valid for a prototype
File "c:\Temp\temp.nxc" ; line 6
# f
#----------------------------------------------------------
# Error: Undefined Identifier f4
so what's going wrong here?
Last edited by HaWe on 12 Jul 2012, 14:32, edited 1 time in total.
Re: NXC: functions with different numbers of parameters?
ps
I checked the values again, and IMO the constant(s) are correct:
so -1E37 actually should work
(in NXC the float max/min constants are not defined yet)
I checked the values again, and IMO the constant(s) are correct:
Code: Select all
FLT_MIN = 1.17549435e-38F
FLT_MAX = 3.40282347e+38F
DBL_MIN = 2.2250738585072014e-308
DBL_MAX = 1.7976931348623157e+308
(in NXC the float max/min constants are not defined yet)
Re: NXC: functions with different numbers of parameters?
compiler bug or programmer's bug?^^
-
- Posts: 358
- Joined: 01 Oct 2010, 06:37
- Location: Denmark
- Contact:
Re: NXC: functions with different numbers of parameters?
I just installed Windows on this laptop so can't check, but it seems like it is the compiler which easily messes up with scientific notification. I haven't had issues with default values before either.
With macro functions you can use "..." as the last parameter to accept variable input. Notice that this only works if "..." is used in opcodes which accepts a variable amount of inputs. (So while it is possible to use "..." in functions in C, this wouldn't be possible on the NXT firmware.) I cannot remember how to use it and I'm not sure if it is documented (so you might have to look in the header files). But you could make your macro like this:I don't know how to do "return" in macro functions but I think it is possible. I might you have to pass it to some internal variable or something... But if you can get it working it will be much more efficient than your current code.
With macro functions you can use "..." as the last parameter to accept variable input. Notice that this only works if "..." is used in opcodes which accepts a variable amount of inputs. (So while it is possible to use "..." in functions in C, this wouldn't be possible on the NXT firmware.) I cannot remember how to use it and I'm not sure if it is documented (so you might have to look in the header files). But you could make your macro like this:
Code: Select all
#define max( ... ) { float f[]; \
ArrayBuild( f, ... ); \
return ArrayMax( f, NA, NA ); }
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
Who is online
Users browsing this forum: No registered users and 7 guests