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

73. anonymous -- 2018-09-04 12:31

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.

geometry dash

74. Assignment help online -- 2018-09-11 07:28

Psychology is the most crucial discipline which supports other disciplines as well. Students have to write essay assignments on the several topics of psychology which are assigned by teachers. Students Assignment Help provides the services of customer psychology assignment help to the students.

75. anonymous -- 2018-09-24 04:11
76. anonymous -- 2018-09-29 12:18

Hey guys if you love to watch online movies and tv shows for free then must visit this website to get new movies for free online.

77. anonymous -- 2018-10-02 09:46

I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information.

Refer: free

78. Olivia -- 2018-10-04 06:08

Thank you for sharing the insight, I'm so glad to find this article. It's very helpful for me.

79. Jennifer -- 2018-10-10 12:16

Understudies shy far from Maths, yet in all actuality, Maths is the closest friend of man. Maths ought to be more common sense and more reasonable, however less mechanical.

80. anonymous -- 2018-10-16 19:34

Great article and many valuable insights about performance, scale and the importance of give to customers a product that be useful, regardless some technical debt, that can be addressed later on when it is really necessary. Thank you!

81. anonymous -- 2018-11-01 18:29
82. -- 2018-11-07 08:09

Burger King is an American global chain of hamburger fast food restaurants. It was founded in 1953 in Jacksonville, Florida, as Insta-Burger King.

83. anonymous -- 2018-11-26 09:40

This is really great work. Thank you for sharing such a good and useful information here in the blog for students. <a href=""></a>

84. sunita -- 2018-12-22 16:23

Therefore, it is advisable to learn about a genuine service agency to make your old fashioned paper. Customer Care Number Customer Care Number May want to you need a man to compose my essay we are a wholly commited team that's prepared to offer our providers for you. If you happen to pick up support, it's extremely important to get started browsing for optimum institution. Toll Free Number Thanks for sharing such great information. If you would like to Buy medicine Online you can visit our site: Helpline Number

85. anonymous -- 2018-12-22 16:23

Should the task is done, you own an ability to obtain more expert services and improve the position. Though it supplies expert services, Phone Number the consumers can find premium large excellent data files written by skilled professional. Service Center Contact Address

87. anonymous -- 2019-01-09 18:17

Spend management is the end-to-end management of enterprise wide spend. It includes all aspects of spend analysis, spend categorization/spend classification, spend data management and spend performance management. Spend management is a continuous and ongoing process directed toward controlling and optimizing a company's spend and effectively improving its bottom line.

88. anonymous -- 2019-01-09 18:18

Diamond Name Necklace by LimeLite Jewellery is always in trend as depicts wealth and status. Diamond rings, earrings, and necklace are paired with different types of outfits whether casual or formal. There are various benefits of wearing name jewelry especially name necklace. Though there are some specific reasons for using specially designed jewelry.

89. anonymous -- 2019-01-09 18:18

Purchasing Platform is a specialized B2B marketplace and one stop shop with only the vendors your company wants, delivering 30% average savings on over 10 million items. Unlike Amazon Business, Purchasing platform automated procurement leads to greater productivity & savings. The platform provided you with a buying desk and great control over your spend and user controls.

90. anonymous -- 2019-01-14 08:20

Os doy las gracias por la información! Yo estaba buscando y no podía encontrar. Usted me ayudó!

91. katedaisy -- 2019-01-29 09:01

I am grateful to have opened this discussion. This question is quite interesting to me. Finally the answer was found bullet force

92. Mickey -- 2019-02-21 12:51

Simple tricks to solve various problems. service will give you a great offer.

Add New Comment