We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f2c40dc commit fbc1dd1Copy full SHA for fbc1dd1
src/main/g8/src/main/scala/Main.scala
@@ -35,6 +35,7 @@ object Main {
35
def powerCode(n: Int, x: Expr[Double]): Expr[Double] =
36
if (n == 0) 1.0.toExpr // toExpr lifts 1.0 to '(1.0)
37
else if (n == 1) x // optimization to not generate x * 1
38
+ else if (n < 0) throw new Exception("Negative powers not implemented. Left as a small exercise. Dont be shy, try it out.")
39
else if (n == 2) '(~x * ~x) // optimization to not generate { val y = x; y * y }
40
else if (n % 2 == 1) '{ ~x * ~powerCode(n - 1, x) }
41
else '{ val y = ~x * ~x; ~powerCode(n / 2, '(y)) }
0 commit comments