You have two real numbers and you want to get the greater one. How would you do this? I am going to show you a few variants to solve this problem.
1. Direct comparison
1 |
greaterNumber = (a > b) ? a : b; |
This method just checks: if a is greater than b then returns a else – returns b.
2. Using arithmetic operators and boolean expressions
1 |
greaterNumber = (a > b) * a + (a <= b) * b; |
Actually, this is not working in C#, but in C++. In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. In C#, there is no conversion between the bool type and other types.
3. Using Abs method
1 |
greaterNumber = (Math.Abs(a-b) + a + b)/2; |
I leave this for you to check its truth.