# Thread: HELP:Matematical Expression

1. ## 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. 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?

3. 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.

Unless c - b is zero.

4. Thank you.

Yours
Nuray

5. > (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?

6. 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 [...]

7. 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

Popular pages Recent additions