pow() and ArraySum() with arrays or structs?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

pow() and ArraySum() with arrays or structs?

Post by spillerrec »

I tried to finish my Bezier curves program which took advantage of the polymorphism of the math operations. But before I knew it, I was trying to add the algorithm for general bezier curves.
Anyway, to do this I needed use pow() on an array like this:

Code: Select all

float bases[5] = { 0, 1, 2, 3, 4 };
float results[];
results = pow( bases, 2 );
This didn't work, however this did:

Code: Select all

asm{ pow results, bases, 2 }
It looks like the compiler uses a temporary float variable which ruins it.

I also tried this:

Code: Select all

task main(){
//	LocationType p[] = { {10,10}, {30,50}, {55, 20}, {80, 20}, {100, 50} };
	LocationType p;// = { 20, 10 };
	p.X = 20;
	p.Y = 10;
	
	LocationType result;
	asm{
		pow result, p, 2
	//	mul result, p, 2
	}
	
	NumOut( 0, LCD_LINE1, result.X );
	NumOut( 0, LCD_LINE2, result.Y );
	for(;;);
}
pow doesn't work, however mul does.


Does ArraySum() support any form for polymorphism? I tried replicating the way arrop is used by the compiler, but I couldn't figure out how to get sensible results with anything else than simple arrays.
I would expect a behavior like this: (In simple terms)

Code: Select all

//function call: return_value = ArraySum( arr );
return_value = 0;
for( unsigned int i=0; i<ArrayLen(arr); i++)
	return_value += arr[i];
In other words, ArraySum ignores the type of the array and just relies on the underlying polymorphism in the math operations. If we have an array of LocationType the result will be a LocationType, if it is an array of an array of int the result will be an array of int and so on.
(It would at least be beneficial for my bezier curves program ; ) )


Anyway, is there anywhere there is info on how polymorphism is supported by these EF functions?
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: pow() and ArraySum() with arrays or structs?

Post by afanofosc »

The vast majority of the opcodes that I added to the enhanced NBC/NXC firmware are not polymorphic like the standard math opcodes are. That could be changed, of course. The compiler is generating bad code when you use pow with an array. It is assuming that you are passing in a float type variable.

From NXCDefs.h:

Code: Select all

inline float pow(float base, float exponent) { asm { pow __FLTRETVAL__, base, exponent } }
I will have to look at why it is not complaining about the type you are passing into the function call.

I'm not sure I understand your question about ArraySum. Are you saying that it does work with arrays containing LocationType structs or that it does not?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: pow() and ArraySum() with arrays or structs?

Post by spillerrec »

My question about ArraySum is, can it accept other types than a simple numeric array and if so, how does it operate on them? I couldn't get anything to work properly, so is this because it simply doesn't support any form for polymorphism or is it because I can't figure out how to use the correct types?
So it does not seem to work with an array of location point, but I would love it if it did work in a similar fashion. (I actually want to work with multidimensional arrays though.)
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: pow() and ArraySum() with arrays or structs?

Post by afanofosc »

ArraySum adds all the elements of a scalar array together. It works for a 1-d array of any scalar type. So it is polymorphic in that it works for different types but it does not have the ability to output a struct. That's not likely to change since the code calculates scalar sums internally for other array operations - specifically standard deviation.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests