NXC: functions with different numbers of parameters?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: functions with different numbers of parameters?

Post by HaWe »

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)
:?:
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: NXC: functions with different numbers of parameters?

Post by mcsummation »

This is from the help for "function":
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.
In your case of "max", you could make the default values very large negative numbers.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: functions with different numbers of parameters?

Post by HaWe »

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?
hergipotter
Posts: 48
Joined: 12 Jan 2011, 18:40
Contact:

Re: NXC: functions with different numbers of parameters?

Post by hergipotter »

Code: Select all

float max(float f1, float f2, float f3 = FLOAT_MIN, float f4 = FLOAT_MIN, float f5 = FLOAT_MIN);
Where FLOAT_MIN is the smallest possible float number (you would have to define this first of course).

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!
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: functions with different numbers of parameters?

Post by HaWe »

hi,
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(;;);

}
# Error: ',' expected
File "C:\Programme\BricxCC\Untitled1.nxc" ; line 15
# NumOut(0,0, max(7.1, 12.4, 5.9, 1E+
#----------------------------------------------------------
but apart from that, the trick seems to be working

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);
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: NXC: functions with different numbers of parameters?

Post by mcsummation »

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.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: functions with different numbers of parameters?

Post by HaWe »

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

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(;;);

}
having instead

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) {
\\...
the compiler of course produces the same error...
# 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
File "c:\Temp\temp.nxc" ; line 7
# flarr[0]=f1; flarr[1]=f2; flarr[2]=f3; flarr[3]=f4;
#----------------------------------------------------------
# Error: Undefined Identifier f5
File "c:\Temp\temp.nxc" ; line 7
# flarr[0]=f1; flarr[1]=f2; flarr[2]=f3; flarr[3]=f4; flarr[4]=f5;
#----------------------------------------------------------
# Error: Undefined Identifier f5
File "c:\Temp\temp.nxc" ; line 9
# r
#----------------------------------------------------------
# Error: Too many arguments
File "c:\Temp\temp.nxc" ; line 14
# NumOut(0,8, max(7.1, 12.4, 5.9, 18.6)
#----------------------------------------------------------
# Error: ")" expected
File "c:\Temp\temp.nxc" ; line 14
# NumOut(0,8, max(7.1, 12.4, 5.9, 18.6)
#----------------------------------------------------------
# Error: ')' expected
File "c:\Temp\temp.nxc" ; line 14
# NumOut(0,8, max(7.1, 12.4, 5.9, 18.6)
#----------------------------------------------------------
# Error: ';' expected
File "c:\Temp\temp.nxc" ; line 14
# NumOut(0,8, max(7.1, 12.4, 5.9, 18.6))
#----------------------------------------------------------


so what's going wrong here?
Last edited by HaWe on 12 Jul 2012, 14:32, edited 1 time in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: functions with different numbers of parameters?

Post by HaWe »

ps
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
so -1E37 actually should work
(in NXC the float max/min constants are not defined yet)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: functions with different numbers of parameters?

Post by HaWe »

compiler bug or programmer's bug?^^
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: functions with different numbers of parameters?

Post by spillerrec »

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:

Code: Select all

#define max( ... ) { float f[]; \
  ArrayBuild( f, ... ); \
  return ArrayMax( f, NA, NA ); }
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.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests