Thread: HELP:Matematical Expression

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    HELP:Matematical Expression

    Hi,

    Could you explain how th eexpression below works

    (c-x)/(c-b)*(b<=x)*(x<=c)

    It is difficult for me to understand there is a multiplication with (b<=x) or (x<=c)

    Thank you in advance

    Nuray

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    In programming x<=c means x is less than or equal to c. There is a symbol the < with a _ under it for math(which means the same thing) but <= is its "computer equivalent". Please correct me if that's not what you mean. What's the equation for, btw?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by nuray
    Could you explain how th eexpression below works

    (c-x)/(c-b)*(b<=x)*(x<=c)

    It is difficult for me to understand there is a multiplication with (b<=x) or (x<=c)
    If b <= x and x <= c, the result will be (c - x) / (c - b). Otherwise, the result will be 0.

    [edit]Unless c - b is zero.
    Last edited by Dave_Sinkula; 10-14-2005 at 08:45 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Thank you.

    Yours
    Nuray

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > (c-x)/(c-b)*(b<=x)*(x<=c)
    What (or who) thought this was a good idea?
    What's wrong with the use of an if() statement?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Salem
    > (c-x)/(c-b)*(b<=x)*(x<=c)
    What (or who) thought this was a good idea?
    What's wrong with the use of an if() statement?
    Definitely not my arena, but I've heard of avoiding branches for valid reasons.
    The downside of a long pipeline is when a program branches, the entire pipeline must be flushed, a problem that branch predicting helps to alleviate. Branch predicting itself can end up exacerbating the problem if branches are predicted poorly. In certain applications, such as supercomputing, programs are specially written to rarely branch and so [...]
    Last edited by Dave_Sinkula; 10-15-2005 at 02:09 PM. Reason: Changed 'avoiding comparisons' to 'avoiding branches'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I see a lot of potential for division by zero going on here.

    I don't think there's anything in C which would guarantee that the sub-expression (b<=x)*(x<=c) would always compile down to something without any branches at all. It's certainly assuming a lot about the architecture of the underlying machine.

    Code:
    if ( (b<=x) && (x<=c) ) {
      result = (c-x)/(c-b);
    } else {
      result = 0;
    }
    Potentially saves a comparison, and a number of arithmetic operations, through short-circuit evaluation.
    A comparison and branch is usually a lot less expensive than a pair of multiplies, and could well save the cost of pointless division as well (if the test fails).

    With the added bonus that it's much easier to figure out what is going on
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 10:34 AM