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
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
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
If b <= x and x <= c, the result will be (c - x) / (c - b). Otherwise, the result will be 0.Originally Posted by nuray
[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.*
Thank you.
Yours
Nuray
> (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.
Definitely not my arena, but I've heard of avoiding branches for valid reasons.Originally Posted by SalemThe 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.*
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.
Potentially saves a comparison, and a number of arithmetic operations, through short-circuit evaluation.Code:if ( (b<=x) && (x<=c) ) { result = (c-x)/(c-b); } else { result = 0; }
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.