Thread: Doing maths only on part of a variable

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    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!
    Last edited by stylistically; 05-06-2011 at 01:41 AM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    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. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    Aren't you already doing that by the "if" statement? I mean do that complicated maths on Y if Y < 0.2.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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;
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by chameleons View Post
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stylistically View Post
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. maths and c++
    By mkeisu in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2008, 03:59 PM
  2. Maths in C
    By username101 in forum C Programming
    Replies: 16
    Last Post: 05-09-2008, 08:51 AM
  3. Maths
    By AcerN30 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-03-2008, 01:13 PM
  4. Printing part of a local variable
    By Phan in forum C Programming
    Replies: 10
    Last Post: 09-26-2005, 05:51 PM
  5. maths???
    By nerdyneo in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2003, 01:04 PM