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.

93. anonymous -- 2019-03-02 10:39

This is nice. <a href="">content-sea</a>

94. Alyia Sharma -- 2019-03-05 12:15

We are high quality and discrete Chandigarh escorts service. We aim to provider you with the experience of being with a real escorts in Chandigarh call girls.

96. City Night -- 2019-03-06 12:07

We offer you the best high quality and most fulfilling Dehradun escorts service which can satisfy you and revitalize your lifestyle for better performance.

97. Tanu Sharma -- 2019-03-08 08:13

We are the best Chandigarh escorts service provider in Chandigarh. Why go somewhere else when all your desired full fill here.

98. WillardZheng -- 2019-03-13 12:50

You realize considerably it is nearly onerous to argue to you. You surely put a new spin for a topic it truly is been discussing for a while. We essentially changed the additional check with a call and a few additions/subtractions. it is able to be feasible to reorganize the enter records so that this second version performs higher.

99. anonymous -- 2019-03-16 07:43

Instagram is among a breed of smartphone apps which have tapped into the creative, image-based online life of young people. Click instagram video downloader free.

100. anonymous -- 2019-03-18 17:33

I want many blogposts, That i very seriously enjoyed reading, I'd like to see info on it all, seeing that it is quite terrific., Take care available for making. VIP Financing Solutions Reviews

101. anonymous -- 2019-03-24 16:20

I just gain cannot piece of writing. It is nice to observe virtually anyone discuss through written text via the center and also lucidity of this necessary trouble is perhaps fast spotted.

102. zapya for pc -- 2019-03-26 08:38 One of the devices, in fact, will create a private network through its wireless antenna, to which others can connect to send data, using the full power of WiFi without touching the internet.

103. anonymous -- 2019-03-26 17:47

The style make an effort to delightful. All of them significant knowledge really are designed choosing massive amount historical past feel. I would like the whole thing notably.

104. anonymous -- 2019-03-30 06:51

Math is extremely good. Those math hints will blow your thoughts—plus, they'll train you how numbers paintings. Positioned the calculator away—with these simple tricks you will be able to clear up maximum elaborate math issues in a few minutes | Cheap Australian Assignment Help

105. hotmail login -- 2019-04-01 05:04

Thanks for the information on the site. > hotmail login

106. anonymous -- 2019-04-06 09:51

This is a bewildering reestablishing article. I am in each watching sense content with your shocking work. You put enduring data. Keep it up. Continue blogging. Expecting to looking next post. 007카지노

110. anonymous -- 2019-04-11 05:13

This is a particularly radiant blog section. This sort of data stunningly fulfilling for me and looking clear. We are incredibly appreciative for your blog an area. A commitment of appreciation is all together for the offer. portale randkowe

111. Imlaak -- 2019-04-11 18:00

This is enchanting substance! I have completely enjoyed examining your fixations and have accomplished the end that you are straight forwardly about a powerful piece of them. I portray a couple of focal data about your related blog. You find any kind of finding or interest property in DHA or find any data. So visit here.

112. Kitaab -- 2019-04-14 10:21

I get something harder on changed zones standard. It will absolutely simperingly be abandoning to take in web content from different makers what's more practice a little from their page. I saw that you talk around a basic article. I outline a couple of central information about your related blog. You locate any sort of result or use for study reason.

113. Free SEO Solutions -- 2019-04-15 09:05

100% free complete SEO solutions suit, check out search engine optimization tools which often used by SEO expert on the day to day online operations. provides free tools to analyze the website and rank your website.

114. anonymous -- 2019-04-15 14:57

Really great trick to solve the math problems. I was actually finding and trying to make tricks like this so that I can make the trick for to solve their issues.

115. vucutguzellik -- 2019-04-16 10:32

Your blog give an information about Maths trick, you giving an important finding out about this article. So here I give a pleasing data about anything that is related with beauty care products. Which gain a stacks of information about related blog.

116. James Wilson -- 2019-04-16 19:56

Today, there are a lot of online games where you may be asked to purchase something with real life currency if you want to get more out of it. Look over all the benefits that are being offered before you make a purchase. On one hand, there is not much more enjoyment in playing. Or, they might improve it a lot and save you tons of time.

117. anonymous -- 2019-04-17 03:32

Interesting post! This is really helpful for me. I like it! > hotmail login

118. luciham20 -- 2019-04-17 10:58

I'm impressed. I don't believe I've met any person who knows as considerably about this subject as you do! download mobile legend

119. jack jones -- 2019-04-17 15:16

Your work and assertion are promising. Additionally, I foresee that you should keep posting this kind of article. Likewise, if you have to glance through this kind of various point about the congruity of this article. <a href="">e-islamichub</a> By then, you should visit our site to keep looking at this kind of article and satisfying learning.

120. anonymous -- 2019-04-18 16:02

You truly make it look so normal with your execution yet I watch this issue to be truly something which I figure I would never get it. It shows up, evidently, to be a silly extent of dazed and wide for me. I'm looking for after forward down your next post. I should need to get more posts. 오바마카지노

121. Jane -- 2019-04-19 11:35

This math question is so difficult. I lost much of time to finish it.

122. Mailshipnbox -- 2019-04-20 12:26

Your blog shares an article about maths trick, you have an important finding a few solutions concerning this article. So here I give obliging data about anything that is associated with bookkeeping. Which gain loads of information about related blog.

123. anonymous -- 2019-04-21 11:02

Your blog shares an article about maths deceive, you have a vital finding a couple of arrangements concerning this article. So here I give obliging information about anything that is related to freeseosolutions. Which gain heaps of data about related blog.

124. anonymous -- 2019-04-22 16:30

This is an incomprehensible blog part. This sort of data strong for me and taking a gander at to clear. We are appreciative for your blog area. A devotion of gratefulness is all together for the offer. web design in Kuwait

125. darell -- 2019-04-29 08:55

Very useful information, thank you.

126. anonymous -- 2019-05-02 11:27

You give a better than the ordinary article and giving learning. You analyze Maths trick. So here I give remarkable data about giving inclinations on Instagram.

127. -- 2019-05-07 15:57

I am really glad I have found this information and I like the helpful information you provide in your articles. Thanks a lot for revealing your website. Kindly also visit -

128. anonymous -- 2019-05-11 08:34

The next few paragraphs is rather helpful plus We're longing for looking through extra within your discussions. You have got essentially fork out every handy and even exciting posting. Thanks a ton to get spreading this review. best offline games for iPhone

130. Jin Jin -- 2019-05-24 11:22

That's good math. I must study well.

131. สล็อตออนไลน์ -- 2019-06-04 09:27

Amazing post i am pretty much pleased with your good post.You put really very helpful information


132. Goldenslot -- 2019-06-04 09:28

Thanks for your marvelous posting! I definitely enjoyed reading it, you’re a great author


133. Mark James -- 2019-06-10 12:45

aol customer care phone number is a Technical Support Number which assist you in technical questions while using aol email services And facing any issues related to Aol login,emails issues, are answered by Aol Technical Support team.Please contact us at +1800-284-6979. our customer support officer are available 24x7 round o clock.feel free to contact.<a href=""> aol customer care phone number</a>

134. smith mason -- 2019-06-13 09:55

Excellent Post, Thanks for the sharing this useful information. My favorite one is ​ I've used them for years. So if you are looking help for math subject then you can contact them any time

135. anonymous -- 2019-06-18 13:57

Describe super tricks, love the way you make me understand. it will me while doing Mathematics homework Help Mathematics homework Help and right researching for it. Surely it will help a lot.

137. anonymous -- 2019-06-21 06:31

great content.. my blog here:

138. anonymous -- 2019-06-27 15:42

Perder peso puede parecer abrumador: debe descubrir cómo comer de manera saludable y alimentar su cuerpo adecuadamente, planificar un régimen de ejercicios que funcione para usted, dormir lo suficiente y, finalmente, tomar cientos de opciones cada día que lo acerquen más. tu objetivo o te tirará completamente fuera de pista.

139. creepyapk -- 2019-06-29 00:59

thanks for sharing..

140. adamjackson -- 2019-07-01 11:14

If you are among one of those academic who is seeking a trustworthy Demand Supply assignment help online, then AssignmentPrime has the top writing services for you. We have intended our online assignment help in a manner that would suit any student’s budget and educational requirements. Furthermore, the team of Supply-Demand Assignment writers connected with us is extremely skilled in assisting the students on the most difficult topics connected to this subject.

141. minion89 -- 2019-07-08 05:32

Math tricks you share very well. I have learned a lot from this math trick. thanks a lot.

142. anonymous -- 2019-07-11 23:40

helo friend i Havwe a provlfejhjfd

143. -- 2019-07-13 09:58

Hot sizzling girls provide in our Russian Escort In the Russian escorts, call girls are made from outside and they like the logo of India, people of India also like them. Russian Escort And according to your desire the girl has given a call and you can spend the night with a call girl at our Independent Escort Hotel. Russian escorts You can make full enzymes at night. You get the services of call girls in Raipur in Raipur and you do not have to do anything, you need to be aware of the need, and you need to call girls, whom you can join with Visit our website…. Link: #Russianescorts #Russianescort #Russiancallgirl

144. -- 2019-07-13 09:59

Luxuries facilities available in independent escort In the Russian escorts, call girls are made from outside and they like the logo of India, people of India also like them Independent escorts Girls in our Independent Escort Hotel services are available from which you can make full enzymes in the night. You get the services of call girls in Raipur in Raipur and you do not need to do anything, you just contact you with Kia escorts and you According to the need, you will get call girls with whom you can enjoin Link: #Independentescorts #Independentescort

145. anonymous -- 2019-07-16 07:44

We are dealing with every kind of customized boxes. Nowadays, we cannot deny the importance of customized boxes so we are bringing you every type of boxes which are necessary for daily use. Feel free to contact us for any kind of information. For further information visit:

146. anonymous -- 2019-07-16 08:15

Try this simple method and get free v bucks from

147. anonymous -- 2019-07-16 12:22

Thanks for sharing the information it was very helpful for me

148. anonymous -- 2019-07-19 13:25

Most Famous and Romantic Urdu Novels

150. anonymous -- 2019-07-30 14:45

Global assignment help Australia has a team of professional assignment writers that provide you the greatest support in assignment help and high-quality service. Visit

151. Lilian F Liao -- 2019-08-08 18:44

Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up. Get a free chance and have a free Roblox gift cards:

152. anonymous -- 2019-08-26 13:13

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. mywegmansconnect

153. anonymous -- 2019-08-28 12:22

it is true because of the mod value of x - y. After getting a single value, whether it is in negative or positive, the mod reciprocates the value with a plus sign. Thereby it is same as x - y. More details at

154. anonymous -- 2019-08-31 09:54

Thank you very much for this useful math solution. It was very helpful to me. I can now solve problems like this in an easy way.

155. Aiden Madison -- 2019-09-03 09:23

There’s certainly a great deal to know about this topic. I like all the points you’ve made.

157. anonymous -- 2019-09-09 09:30

Thanks for the blog. I especially loved the content and the great stuff here. Also read this: <a href="">math help</a>

158. Anushka Malik -- 2019-09-10 19:39

Note:Beware Of Froud Peoples Independent College Stuff Only Select Images Rite Now We are provide In Dehradun,Mussoorie and Goa.. Model Catg,Vip Stuff,Spend A gud Time With Peacefulness As Per Ur Requirement. We are Dehradun escort service provider, we can host you and can make your trip memorable in Mussoorie & Dehradun Escort service Call Girls. Our team of Dehradun escort service is well educated in (Hospitality), well mannered, and multi lingual who are quick to adoptable your nature and can become a good friend in a very short period of time that you can enjoy your trip at it full boom.

<a href="">Select Pictures Of Dehradun Student Girls</a>

<a href="">Dehradun Female Escorts</a>

<a href="">Mussoorie Busty Female Escort Girls Photo</a>

<a href="">Goa Dream Girls</a>

<a href="">Panipat Call Girls Haryana</a>

<a href="">Book Girls Rite Now</a>

159. anonymous -- 2019-09-23 05:25

I usually calculate here gpa calculator :, you try it

160. Ubaid -- 2019-09-25 09:10

Here you can find the most interesting financial and other facts about famous people. Brendan Fraser net worth is one of the biggest self-development blogs on the web. We publish lessons on self-improvement. And Future Net Worth and much more So here we share related data about our favorite celebrities.

161. John -- 2019-09-25 09:26

I trust you have set yourself up for the forthcoming time! when winding up in a spot where each blogger has imagined for! because this bit of work is something that solitary an additional customary personality could do! such a long way to go from your post! This site has the most reviews for being an expert in exterior designing and outdoor construction! give it a try!

162. Fredluis -- 2019-10-11 05:57

I was suggested this blog by means of my bro. I am not sure whether or not this publish is written by means of him as no one else understand such precise about my problem. You’re wonderful! Thanks!

163. anonymous -- 2019-10-11 13:32

Thanks for sharing this math problem solution. More details at

Add New Comment