Created
September 6, 2015 02:16
-
-
Save InvisibleTech/bc5af79928145d783435 to your computer and use it in GitHub Desktop.
Just a simple power function for an integer exponent. Just for fun.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pow(x: Double, n: Int) : Double = { | |
n match { | |
case 0 => 1.0 | |
case n if n < 0 => 1.0 / pow(x, Math.abs(n)) | |
case n if (n & 0x1) == 0 => pow(x, n >> 1) * pow(x, n >> 1) | |
case _ => x * pow(x, n - 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment