{"id":719,"date":"2014-04-23T16:10:12","date_gmt":"2014-04-23T16:10:12","guid":{"rendered":"http:\/\/shivdev.com\/blog\/?p=719"},"modified":"2014-08-20T19:15:54","modified_gmt":"2014-08-20T19:15:54","slug":"raising-a-number-to-a-power-in-java","status":"publish","type":"post","link":"http:\/\/shivdev.com\/blog\/2014\/04\/23\/raising-a-number-to-a-power-in-java\/","title":{"rendered":"Raising a number to a power in Java"},"content":{"rendered":"<p>In Java, use the <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/lang\/Math.html#pow(double, double)\" title=\"JavaDocs\" target=\"_blank\">Math.pow(a, b)<\/a> to raise the value of &#8216;a&#8217; to the power of &#8216;b&#8217;.<br \/>\nIn Microsoft Excel and some other tools the Caret (^) symbol is used for this operation.<br \/>\nNote that in Java, the caret (^) symbol implies XOR of the two numbers.<\/p>\n<p>Here&#8217;s the difference between the two.<\/p>\n<blockquote><p>\n2 raised to 4 = 2*2*2*2 = 16 (2 to the power of 4)<br \/>\n2 XOR 4 = 010 XOR 100 = 110 = 6 (Binary representation of 2 = 010 ; and 4 = 100)\n<\/p><\/blockquote>\n<p>Here&#8217;s a simple Java code snippet to try out.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String[] args) {\r\n\tSystem.out.println(&quot;Math.pow(2, 4) = &quot; + Math.pow(2, 4)); \/\/ returns 16\r\n\tSystem.out.println(&quot;2 ^ 4 = &quot; + (2 ^ 4) ); \/\/ returns 6 (010 XOR 100 = 110)\r\n}\r\n<\/pre>\n<p>And here&#8217;s a SQL flavor.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nmysql&gt; select power(2, 4);\r\n+-------------+\r\n| power(2, 4) |\r\n+-------------+\r\n|          16 |\r\n+-------------+\r\n1 row in set (0.00 sec)\r\n<\/pre>\n<p>And a Divide &#038; Conquer algorithm in Java.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic double power(double a, long b) {\r\n\t\/\/ Base Cases\r\n\tif (a == 0) return 0;\r\n\tif (b == 0) return 1;\r\n\t\r\n\t\/\/ Divide &amp; Conquer\r\n\t\/\/ 2*2*2*2 = (2*2) * (2*2) = ((2)*(2)) * ((2)*(2))\r\n\tdouble p = power(a, b\/2);\r\n\t\r\n\t\/\/ Handle Negative Exponents\r\n\tdouble x = (b &gt;= 0) ? a : 1\/a;\r\n\r\n\t\/\/ Handle Odd &amp; Even b's \r\n\t\/\/ 2*2*2*2*2 = 2 * (2*2) * (2*2) = a * p * p\r\n\treturn (b%2 != 0) ? x*p*p : p*p; \t\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Java, use the Math.pow(a, b) to raise the value of &#8216;a&#8217; to the power of &#8216;b&#8217;. In Microsoft Excel and some other tools the Caret (^) symbol is used for this operation. Note that in Java, the caret (^) symbol implies XOR of the two numbers. Here&#8217;s the difference between the two. 2 raised [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,11,6],"tags":[],"_links":{"self":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/719"}],"collection":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/comments?post=719"}],"version-history":[{"count":12,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/719\/revisions"}],"predecessor-version":[{"id":731,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/719\/revisions\/731"}],"wp:attachment":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/media?parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/categories?post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/tags?post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}