{"id":42453,"date":"2003-09-17T11:39:00","date_gmt":"2003-09-17T11:39:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2003\/09\/17\/answers-to-exercise-from-scrollbars-part-11\/"},"modified":"2003-09-17T11:39:00","modified_gmt":"2003-09-17T11:39:00","slug":"answers-to-exercise-from-scrollbars-part-11","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20030917-00\/?p=42453","title":{"rendered":"Answers to exercise from Scrollbars Part 11"},"content":{"rendered":"<p>\n        <strong>Exercise<\/strong>: Why do we use the formula <code>c = a + (b-a)\/2<\/code> instead\n        of the simpler <code>c = (a+b)\/2<\/code>?\n    <\/p>\n<p>\n        <strong>Answer<\/strong>: To avoid integer overflow in the computation of <code>a+b<\/code>.\n    <\/p>\n<p>\n        Here, <code>a<\/code> and <code>b<\/code> are window coordinates, and the window can\n        be anywhere. If the window were placed at extreme coordinates like (MAXLONG,MAXLONG),\n        then the arithmetic would overflow and the &#8220;midpoint&#8221; would be incorrectly computed.\n    <\/p>\n<p>\n        Note that the alternate formula <code>a+(b-a)\/2<\/code> is also subject to overflow,\n        this time in the computation of the value <code>b-a<\/code>. However, in our case, <code>b-a<\/code> is\n        the width of our window, which is something that we can control.\n    <\/p>\n<p>\n        Integer overflow was one of the Windows 95 application compatibility bugs that I had\n        to deal with. There was a DOS game that wanted to do a binary search, and instead\n        of using indices, they attempted to average the two pointers together:\n    <\/p>\n<pre>BYTE *low = ...;\nBYTE *high = ...;\nBYTE *mid = ((UINT)low + (UINT)high)\/2;\n<\/pre>\n<p>\n        This worked as long as the game was being run under an operating system without virtual\n        memory, because the &#8220;low&#8221; and &#8220;high&#8221; pointers would both be comparatively small numbers\n        (nobody had machines with 2GB of RAM), so the sum <code>low+high<\/code> would not\n        overflow.\n    <\/p>\n<p>\n        Windows 95 ran these DOS games, but under a DPMI server that supported virtual memory.\n        The DPMI specification permits the server to put memory anywhere, and we put our memory\n        at the high end of the address space.\n    <\/p>\n<p>\n        This program then overflowed in its attempt to average the two pointers and crashed.\n    <\/p>\n<p>        So be careful how you average two values together. It&#8217;s harder than you think.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exercise: Why do we use the formula c = a + (b-a)\/2 instead of the simpler c = (a+b)\/2? Answer: To avoid integer overflow in the computation of a+b. Here, a and b are window coordinates, and the window can be anywhere. If the window were placed at extreme coordinates like (MAXLONG,MAXLONG), then the arithmetic [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-42453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Exercise: Why do we use the formula c = a + (b-a)\/2 instead of the simpler c = (a+b)\/2? Answer: To avoid integer overflow in the computation of a+b. Here, a and b are window coordinates, and the window can be anywhere. If the window were placed at extreme coordinates like (MAXLONG,MAXLONG), then the arithmetic [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/42453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=42453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/42453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=42453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=42453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=42453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}