# How to share one math function with many subsets?

Hi.

I have one essential math function that I want to share with many subsets
I shall programming a coordinate function in arduino MEGA2560 and it shall have trigonometry function to share with about 15 same function in different purposes.
If you look at my earlier posts, you will observe that I shall building a home cockpit.

I think solution is the functions to modify integers outside setup and loop and rerun the trigonometry functions and modify memory address so each function shall go to correct integer to save bytes and might kilobytes. Maybe it is called inside assembly or something?

you can make a small library for the function so it can be used multiple times within one sketch and even in other sketches.

What do you mean by "subset"?

Aha... So the library is one of some solution..hmm... But that takes a few bytes to write call and callback. No any better solution?

I mean with subsets is a bad dictionary to choose. I mean call and callback function.

What would be wrong with just putting the maths calculation in a function - something like

``````int myAddValues(int valA, int valB) {
totalVal = valA + valB;
}
``````

which could be called from different parts of your program

...R

I said share With one same function on some subset With {} that share same calculation trigonometry Method. Is it also possible to modify the memory so I not need to to use Library solution?

As you mentioned: int myAddValues(int valA, int valB) { totalVal = valA + valB; return totalVal; }

It shows that valA and valB is just accessible to one variable, not share With many functions that using the same Method. Maybe using goto function ?

Imagine if you have four computers that calculate the trigonometry functions and it is unnecessary to programming four exactly same trigonometry functions With val1, val2, val3, val 4, and so on; do you now understanding it?

Is there maybe someone in the Scandinavia section of the forum who can help you? Your question doesn't make a lot of sense.

Qwseyvnd: It shows that valA and valB is just accessible to one variable, not share With many functions that using the same Method.

First thought ... why not use global variables that can be accessed everywhere in your program?

Second thought ... I don't know what you mean by "is just accessible to one variable". In my example there are two variables and you could have more if you need them.

Third thought ... I don't know what you have in mind when you say "many functions that using the same Method". In my mind that simple function I suggested can be called by as many other functions as you want

Fourth thought ... post a piece of "pseudo code" that illustrates what you are trying to achieve.

...R

AWOL:
Is there maybe someone in the Scandinavia section of the forum who can help you?
Your question doesn’t make a lot of sense.

AWOL:
Is there maybe someone in the Scandinavia section of the forum who can help you?
Your question doesn’t make a lot of sense.

I think that the answer is no.

Robin2: First thought ... why not use global variables that can be accessed everywhere in your program?

Second thought ... I don't know what you mean by "is just accessible to one variable". In my example there are two variables and you could have more if you need them.

Third thought ... I don't know what you have in mind when you say "many functions that using the same Method". In my mind that simple function I suggested can be called by as many other functions as you want

Fourth thought ... post a piece of "pseudo code" that illustrates what you are trying to achieve.

...R

It is possible to use global variables but I am searching a Method that many variables share the same function without need to program each function to each variable. Maybe I need to modify the CPU register to run many global variables to same Math function... It is called hacking; but I do not want to be an potentially illegal hacker.

Tell us what you want to do, not how you think you think it should be implemented.

Qwseyvnd: It is possible to use global variables but I am searching a Method that many variables share the same function without need to program each function to each variable.

You did not respond to my Fourth Thought so we still don't know what you are trying to achieve

Maybe I need to modify the CPU register to run many global variables to same Math function... It is called hacking; but I do not want to be an potentially illegal hacker.

The Arduino code is Open Source so you can hack it to your heart's content - if you are competent to do so.

...R

Fourth thought:

void trigonometry(){

float result1 = pointx * pointx; float result2 = pointy * pointy; double result3 = result1 + result2; double result4 = result3 ^ 0.5; But the function ^ is not supported in Arduino? Or restriction in Atmel AVR-GCC?

}

The pointx and pointy share with many subset in { and } to save bytes. Maybe using goto function? Pointer function to share pointx and pointy ?

Maybe using goto function?

goto is not a function. It is an abomination.

Pointer function

Pointers are not functions. They are divine.

The pointx and pointy share with many subset in { and } to save bytes.

I don't understand this, and, I suspect, neither do you.

this is the euclidean distance function, can be written in C++ as follows

``````double trigonometry(double pointx, double pointy)
{
return sqrt((pointx * pointx) + (pointy*pointy));
}

double points[3][2] = { {1,2}, {3,5}, {5, 8} };
...
for (int i=0; i<3; i++)
{
float value = trigonometry(points[i][0], points[i][1]);
Serial.println( value);
}
``````

the function ^ is not supported in Arduino?

It is, but in C++ that does a bitwise xor. See bitwise functions The function that you probably want is pow()

I mean it with sqrt as ^0.5.

Maybe this void trigonometry() {} can be used each calculation as this.

instruction a calculate trigonometry for a do the next instruction instruction b calculate same trigonometry for b do the next instruction instruction c calculate same trigonometry for c

instruction trigonometry

a, b, and c share the same function.

That's I mean and I should have explained better in the first topic.

robtillaart:
this is the euclidean distance function, can be written in C++ as follows

``````double trigonometry(double pointx, double pointy)
``````

{
return sqrt((pointx * pointx) + (pointy*pointy));
}

double points[3][2] = { {1,2}, {3,5}, {5, 8} };

for (int i=0; i<3; i++)
{
float value = trigonometry(points[i][0], points[i][1]);
Serial.println( value);
}

Is that this to use for statements repeat block to share same trigonometry function?
I see that {1,2}, {3,5}, {5,8} have not any variable… Do you propose me to put the variable there?

points is an 2 dimensional array of doubles. every 2 values represent a point.
These can be accessed by using the index

a for loop is an easy mechanism to process every element from an array

Still not making sense.

A function in C must always return a value. Most of the functions you see in Arduino return void, which is a special keyword which means "I don't ever want to use this return value."

``````float squared(float a) {
//this function takes one floating-point value and returns the square of that value
return a * a;
}

//here's how you use it, inside any other function, such as loop()
float SomeVariable, SomeResult;
Serial.print("Enter a value");
SomeVariable = Serial.parseFloat();
Serial.println();

SomeResult = squared(SomeVariable);

Serial.print("The square of ");
Serial.print(SomeValue);
Serial.print(" is: ");
Serial.println(SomeResult);
``````

So using that pattern, can you see how you might calculate some trigonometry for different input variables?

You may need to return more than one value, for example X and Y. There's a few different techniques to do that. Let us know if that's what you think you need.