Maths trick: doing fewer comparisons

Note: this is not an optimisation. It is just one more tool you should have in your toolbox when looking for optimisations. It may be useful.

This is the trick:

\[\min(x,y) = \dfrac{x + y - |x - y|}{2}\]
\[\max(x,y) = \dfrac{x + y + |x - y|}{2}\]

You can check for yourself that it is always true: when x > y, |x - y| is the same as x - y, etc.

What good is it for? There is often an implicit comparison in min or max. It might be interesting to replace it with a call to the branchless fabs.

Example usage

Consider the following code:

float a, b, c, d;
/* ... */
return (a > b) && (c > d);

That kind of code is often used eg. in collision checks, where a lot of tests can be done. This code does two comparisons. On some architectures, this means two branches. Not always something you want.

The test condition is equivalent to:

(a - b > 0) && (c - d > 0)

Now when are two given numbers both positive? That is if and only if the smallest is positive:

min(a - b, c - d) > 0

We may now use our trick:

(a - b) + (c - d) - |(a - b) - (c + d)| > 0

And so the code could be rewritten as such:

float a, b, c, d;
/* ... */
return (a - b) + (c - d) > fabsf((a - b) - (c - d));

We basically replaced the additional test with a call to fabsf and some additions/subtractions. It may be possible to reorganise the input data so that this second version performs better.


1. anonymous -- 2013-02-09 21:42

Thanks for this nice math trick. I am using something very similar in Matlab.. Tom

3. online -- 2016-12-05 15:00

So , soft tabs generic don t blame unbelievers for how they feel.

24. cialis -- 2017-01-17 11:00

Must have a prescription for , .

25. JoshuaSom -- 2017-07-14 01:01

<a href=>buy generic vardenafil </a> vardenafil buy online <a href=" ">buy vardenafil </a> vardenafil review

26. Willielep -- 2017-07-14 07:51

<a href=>buy propecia uk </a> where can i buy cheap propecia <a href=" ">buy generic propecia online </a> buy propecia

27. JeffreyHaitA -- 2017-07-14 08:02

<a href=>buy metformin online </a> where to buy metformin <a href=" ">buy metformin 500mg </a> can i buy metformin online

28. Danielliaks -- 2017-07-14 08:16

<a href=>where to buy nolva and clomid </a> clomid buy online usa <a href=" ">where to buy clomid </a> buy clomid with no prescription

29. ScottLof -- 2017-07-18 02:11

<a href=>play blackjack for free </a> play blackjack online for free <a href=" ">play free blackjack </a> play blackjack online free

34. Jordonkal -- 2017-07-20 16:11

<a href=>vegas online slots </a> free online slots with bonuses <a href=" ">double down free online slots </a> free slots online

35. Williamgiz -- 2017-07-20 21:50

<a href=>order modafinil online </a> order modafinil online india <a href=" ">order modafinil from india </a> order modafinil from india

36. Robertethef -- 2017-07-20 21:54

<a href=>order xanax from canada </a> what happens to you if you order xanax online <a href=" ">order xanax bars online overnight </a> order xanax online

37. JerryMup -- 2017-07-20 22:11

<a href=>buy xanax online with a prescription </a> buy xanax online no perscription <a href=" ">buy xanax no prescription </a> buy 2mg xanax online

38. MarvinBiags -- 2017-07-21 08:30

<a href=>tramadol order by mail </a> order tramadol free shipping <a href=" ">order tramadol online without script </a> order tramadol online without prescription

39. Williamgiz -- 2017-07-21 08:30

<a href=>order modafinil online usa </a> order modafinil online risk <a href=" ">order online modafinil mexico </a> order modafinil from india

40. Robertethef -- 2017-07-21 08:31

<a href=>order xanax bars online </a> xanax order canada <a href=" ">buy xanax with money order </a> order xanax

41. Alfredsag -- 2017-07-21 08:31

<a href=>buy cheap xanax overnight shipping online </a> cheap xanax pill press <a href=" ">cheap xanax for sale online </a> cheap xanax

42. BryanMisee -- 2017-07-21 08:31

<a href=>best place to buy modafinil </a> how to buy modafinil <a href=" ">buy modafinil australia </a> smart drugs modafinil buy online

43. LarryPsync -- 2017-07-21 08:31

<a href=>buy tramadol online uk </a> buy tramadol online <a href=" ">buy tramadol 50mg </a> best place to buy tramadol online

44. JerryMup -- 2017-07-21 08:32

<a href=>where can i buy xanax </a> buy xanax online <a href=" ">how to buy real xanax online </a> buy xanax without doctor consultation

45. BobbyMeept -- 2017-07-21 08:32

<a href=>where can i buy phentermine online </a> where to buy phentermine online <a href=" ">buy real phentermine online </a> where can i buy phentermine on line

46. Donaldmag -- 2017-07-21 21:00

<a href=>can i order prednisone online </a> order prednisone online <a href=" ">order prednisone overnight </a> order prednisone for dogs

47. Michaelunlok -- 2017-07-21 21:01

<a href=>xenical 120 mg price uk </a> cost of xenical pills <a href=" ">xenical price </a> buy xenical online cheap

48. Larrycus -- 2017-07-21 23:31

<a href=>buy clomid online </a> where to buy nolva and clomid <a href=" ">clomid buy </a> clomid buy

67. Georgepenue -- 2017-08-19 10:38

how to order levitra online <a href="">levitra price</a> buy levitra australia <a href=>levitra online</a> cheap generic levitra online

68. anonymous -- 2018-03-17 08:16

I enjoyed over read your blog post. Your blog has nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again

69. shawnjasper -- 2018-05-03 19:52

Be the precise blog if you have wants to learn about this topic. You comprehend considerably its nearly onerous to argue to you . You undoubtedly put a new spin for a topic thats been discussing for some time. Nice stuff, simply nice!

70. anonymous -- 2018-07-31 14:34

Hi, I am a marketing manager at All Assignment Help; it is an online platform which offer Assignment help for all university students. We have a team of professional writers & provide best solution according to given guideline.

71. Luke009 -- 2018-08-01 11:44

Cool math trick! Thank you for sharing!! I wish I know it when I was in school. imgrum

72. anonymous -- 2018-08-02 17:30

UNT is the flagship institution of the University of North Texas System, which includes additional universities in Dallas and Fort Worth

Add New Comment