Reputation: 31
Well,
My first question here.
Is it possible to build an "If" statement using only "max" and "min" statements?
The problem that i have Is that I need to compare 2 numbers (A and B)and see if B > 1.1 x A. If that happens I pick B if not, I pick A .
Any idea?
Upvotes: 2
Views: 422
Reputation: 5753
An if statement, whether in pure (order-invariant) logic or in procedural logic, will work on boolean statements only. That means true or false. A min or max function returns a number, not a true/false value. So the short answer to your question is no, it is not possible to build an if statement using max and min return values.
Now, in the details of your question, you shed a little more light on what you want. What's confusing though is how max and min enters into the equation. B > 1.1*A requires no Max and Min processing, so are you using Max and Min functions to arrive at B and at A? If so, then simply process them first, and then plug them into that equation.
And because "greater than" and "less than" comparions DO return true/false values, you're in luck. Just use that in your "if" statement. Here is some pseudo-code.
max1 = 25
max2 = 72
min1 = 95
min2 = 80
A = Max(max1,max2)
B = Min(min1,min2)
O = NULL
if B > 1.1 * A then
set O = B
else
set O = A
end if
When the output of an if statement is a value, as opposed to a command, as you seem to desire, some languages use the "elvis" operator, which makes things prettier. Using it, you would just write:
O = B > 1.1 * A ? B : A
Upvotes: 0
Reputation: 7795
If your logical operators accept non-boolean value, you could do it as follows:
n = Max(B - 1.1*A, 0)
output = Max(B*(n && n), A)
Upvotes: 1