Skip to content

Commit fbc1dd1

Browse files
committed
Add a small exercise for the user
1 parent f2c40dc commit fbc1dd1

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

src/main/g8/src/main/scala/Main.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ object Main {
3535
def powerCode(n: Int, x: Expr[Double]): Expr[Double] =
3636
if (n == 0) 1.0.toExpr // toExpr lifts 1.0 to '(1.0)
3737
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.")
3839
else if (n == 2) '(~x * ~x) // optimization to not generate { val y = x; y * y }
3940
else if (n % 2 == 1) '{ ~x * ~powerCode(n - 1, x) }
4041
else '{ val y = ~x * ~x; ~powerCode(n / 2, '(y)) }

0 commit comments

Comments
 (0)