{"id":146,"date":"2009-01-06T00:00:33","date_gmt":"2009-01-06T07:00:33","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=475"},"modified":"2009-01-06T00:00:33","modified_gmt":"2009-01-06T07:00:33","slug":"clamp-mathmaxmathmin","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=146","title":{"rendered":"Clamp Math.max(Math.min())"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ this one-liner is good to know:\r\n\/\/ Math.max(minValue, Math.min(maxValue, valueToClamp));\r\n\r\n\/\/ wrapped in a function it looks like this:\r\nconst MIN:Number = 0;\r\nconst MAX:Number = 100;\r\n\r\nfunction clamp(val:Number, min:Number = MIN, max:Number = MAX){\r\n\treturn Math.max(min, Math.min(max, val))\r\n}\r\n\r\nfor (var i:int = 0; i&lt;1000; i++){\r\n\tvar val:Number = Math.random()*1000 - 500;\r\n    \/\/ clamp the above random value between -100 and +100\r\n    trace(clamp(val, -100, 100));\r\n}\r\n<\/pre>\n<p>Although I show a function implementation above, I use this technique inline a good deal. If there is some value that I need to clamp I&#8217;ll often just &#8220;max min it&#8221;:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar val:Number = Math.random()*1000;\r\n\/\/ clamp it - 0-100\r\ntrace(Math.max(0, Math.min(100, val));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ this one-liner is good to know: \/\/ Math.max(minValue, Math.min(maxValue, valueToClamp)); \/\/ wrapped in a function it looks like this: const MIN:Number = 0; const MAX:Number = 100; function clamp(val:Number, min:Number = MIN, max:Number = MAX){ return Math.max(min, Math.min(max, val)) } for (var i:int = 0; i&lt;1000; i++){ var val:Number = Math.random()*1000 &#8211; 500; \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,37],"tags":[3,4],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-misc","category-one-liners","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=146"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/146\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}