[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 804
  • Last Modified:

Max Function using only + - / abs

Someone bet me you could make a Max(a,b) function using only the 4 operators +, -, divide and absolute value.
I came up with

int max(a,b){
      return ((a>=b)*a + (a<b)*b);
}

But I cannot use comparators.
0
Ryan
Asked:
Ryan
  • 2
1 Solution
 
ozoCommented:
(a+b+abs(a-b))/2
0
 
RyanProject Engineer, ElectricalAuthor Commented:
Wow. How did you solve that?
I seriously just spent an hour trying to figure it out.
0
 
ozoCommented:
abs is like max(a,-a)
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now