{"id":1010,"date":"2009-12-28T16:28:16","date_gmt":"2009-12-28T05:28:16","guid":{"rendered":"http:\/\/www.vectorstorm.org\/?p=1010"},"modified":"2009-12-28T16:28:16","modified_gmt":"2009-12-28T05:28:16","slug":"bugs-which-arent-bugs","status":"publish","type":"post","link":"https:\/\/www.vectorstorm.com.au\/2009\/12\/28\/bugs-which-arent-bugs\/","title":{"rendered":"Bugs which aren’t bugs"},"content":{"rendered":"
Inside VectorStorm, there’s a function vsClamp; \u00a0it looks like this:<\/p>\n
#define vsClamp(a,min,max) ( vsMin( max, vsMax( min, a ) ) )<\/pre>\n(modified for legibility; \u00a0C\/C++ coders, no need to warn me about how painfully dangerous that code snippit is. :) )<\/p>\n
You’d use it like this:<\/p>\n
vsClamp(1, 0, 10) -> 1\r\nvsClamp(0, 10, 20) -> 10\r\nvsClamp(6, 2, 3) -> 3<\/pre>\nEffectively, you give it a number, and then a minimum and maximum legal value; \u00a0the value that pops out of the other end is the original number, clamped to within the requested range.<\/p>\n
But what I’ve just discovered is that about half of my code is actually calling this function like this:<\/p>\n
vsClamp( min, a, max );<\/p>\n
That is, it sandwiches the value to be clamped between the minimum and maximum values. \u00a0Which actually makes a weird kind of sense, but isn’t how I actually implemented that function. \u00a0“Oh no,” I thought, “all that code is going to be doing totally the wrong thing”<\/p>\n
But as it turns out, calling vsClamp( \u00a0a, min, max ) is identical to calling vsClamp( min, a, max ), which is itself identical to calling vsClamp( min, max, a ). \u00a0As long as whichever value you intend to act as the ‘min’ is less than whichever value you intend to act as the ‘max’, all three are guaranteed to return the same result.<\/p>\n
How bizarre is that? \u00a0:)<\/p>\n","protected":false},"excerpt":{"rendered":"
Inside VectorStorm, there’s a function vsClamp; \u00a0it looks like this: #define vsClamp(a,min,max) ( vsMin( max, vsMax( min, a ) ) ) (modified for legibility; \u00a0C\/C++ coders, no need to warn me about how painfully dangerous that code snippit is. :) ) You’d use it like this: vsClamp(1, 0, 10) -> 1 vsClamp(0, 10, 20) ->…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[4,3],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/po9WK-gi","_links":{"self":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/1010"}],"collection":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":0,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}