# Doing maths only on part of a variable

This is a discussion on Doing maths only on part of a variable within the C Programming forums, part of the General Programming Boards category; I have a variable Y that i pass through lots of maths functions. Y in graphics is a line of ...

1. ## Doing maths only on part of a variable

I have a variable Y that i pass through lots of maths functions.

Y in graphics is a line of length 1, ranging from 0 to 1, an axis of a graph.

I only need to do lots of maths functions on a small part of Y, a segment of the line going from 0 to 0.2

at the moment i am doing all the maths for the complete variable, and then multiplying the parts i dont need by zero. How can i instead do maths on only the part of the variable in the range 0 to 0.2?

help i am really stuck!!! i dont understand C processing

I suppose the maths looks like this:
Code:
`if (Y<0.2) y=y*4*tan(y)*acos(sin(y*pi))^4/X+var1+var2+var3*sin..... (ten lines later) *tan(y)*acos(sin(y*pi))^4`
so a long equation, that is calculated 100 times, so alot of maths!

2. Can you please post the segment of the program that you have problem with? I have difficulty understanding how Y represents the line segment, and thus could not think of any idea to solve your problem.

3. Aren't you already doing that by the "if" statement? I mean do that complicated maths on Y if Y < 0.2.

4. Well, the easy solution is to clamp "Y" between 0 and 0.2 like this:
Code:
```if (Y > 0.2)
Y = 0.2;
else if (Y < 0)
Y = 0;```

5. Originally Posted by chameleons
Aren't you already doing that by the "if" statement? I mean do that complicated maths on Y if Y < 0.2.
I didnt actually try the above because it would have been too difficult to write for me, the complicated maths is at least 10 lines, I am not sure how to make an if statement followed by 10 lines of vars and equations.

how do i do that?

@Sypher
Thanks that works very well, Although i am not sure it would reduce the CPU rate... when the program draws the graph i guess it asks Y to return X for all values from 0 to 1, so in the instance of the "clamp" it would still do all the maths from 0 to 1 except that all values outside the clamp range would be calculated with y=0 and x=0, therefore still taking up as much processor?

my code is so novice i cant put it up at the moment!

6. Originally Posted by stylistically
I didnt actually try the above because it would have been too difficult to write for me, the complicated maths is at least 10 lines, I am not sure how to make an if statement followed by 10 lines of stacked vars and equations.

how do i do that?
Put your math in a function, or wrap it in braces:
Code:
```if( inrange( x, y, z ) )
{
domath();
}```
I'm not sure what you are having trouble with here.

Quzah.