{"id":112,"date":"2010-05-05T08:48:05","date_gmt":"2010-05-05T06:48:05","guid":{"rendered":"http:\/\/www.tapper-ware.net\/blog\/?p=112"},"modified":"2010-05-05T08:48:05","modified_gmt":"2010-05-05T06:48:05","slug":"a-shorter-way-to-do-math-floor-a-little-known-trick","status":"publish","type":"post","link":"https:\/\/www.tapper-ware.net\/blog\/a-shorter-way-to-do-math-floor-a-little-known-trick\/","title":{"rendered":"A shorter way to do Math.floor &#8211; A little known trick"},"content":{"rendered":"<p>Nothing groundbreaking, but someone just handed me code that was filled with Math.floor calls. While this doesn&#8217;t automatically slow down your code the way it used to, thanks to better Javascript engines, it&#8217;s still ugly as hell to look at.<\/p>\n<p>Now, those people who like to put wavy brackets on individual lines will probably start shouting again, but once you&#8217;ve gotten used to it, this is actually a good deal more readable, since it shortens the code considerably, while still having a &#8220;unique look&#8221; that you&#8217;ll be able to identify quickly<\/p>\n<p>Instead of<br \/>\n<code>Math.floor(foo)<\/code><\/p>\n<p>You can simply do a bitwise OR with 0:<br \/>\n<code>0|(foo)<\/code><\/p>\n<p>The reason this works is that any bit operation causes the number to be converted to a signed 32 bit integer. While this isn&#8217;t strictly the same as what Math.floor does, it will give you the same result at least for positive 31 bit integers. For negative ones you get something a little different (but typically far more useful), namely the integer part (-3 for -3.7) instead of the the highest integer smaller or equal to the given number (-4 for -3.7).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nothing groundbreaking, but someone just handed me code that was filled with Math.floor calls. While this doesn&#8217;t automatically slow down your code the way it used to, thanks to better Javascript engines, it&#8217;s still ugly as hell to look at. Now, those people who like to put wavy brackets on individual lines will probably start &hellip; <a href=\"https:\/\/www.tapper-ware.net\/blog\/a-shorter-way-to-do-math-floor-a-little-known-trick\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">A shorter way to do Math.floor &#8211; A little known trick<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/112"}],"collection":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":0,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}