Skip to content

Commit b0515e8

Browse files
authored
Add power operator (#240)
1 parent a99b58a commit b0515e8

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/Base/Int.fram

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ pub method mod {~__line__, ~__file__} (self : Int) (n : Int) =
2929
assert {msg="Division by zero"} (n != 0);
3030
(extern dbl_modInt : Int -> Int -> Int) self n
3131

32+
pub method rec pow {~__line__, ~__file__} (self : Int) (n : Int) =
33+
assert {msg="Negative exponential"} (n >= 0);
34+
if n == 0 then 1
35+
else if n % 2 == 0 then (self * self).pow (n / 2)
36+
else self * (self * self).pow (n / 2)
37+
3238
pub method neg (self : Int) = 0 - self
3339

3440
pub method land = (extern dbl_andInt : Int -> Int -> Int)

lib/Base/Int64.fram

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ pub method neg = (extern dbl_negInt64 : Int64 -> Int64)
1818
pub method add = (extern dbl_addInt64 : Int64 -> Int64 -> Int64)
1919
pub method sub = (extern dbl_subInt64 : Int64 -> Int64 -> Int64)
2020
pub method mul = (extern dbl_mulInt64 : Int64 -> Int64 -> Int64)
21+
2122
pub method div {~__line__, ~__file__} (self : Int64) (n : Int64) =
22-
assert {msg="Division by zero"} (n.neq 0L);
23+
assert {msg="Division by zero"} (n != 0L);
2324
(extern dbl_divInt64 : Int64 -> Int64 -> Int64) self n
2425
pub method mod {~__line__, ~__file__} (self : Int64) (n : Int64) =
25-
assert {msg="Division by zero"} (n.neq 0L);
26+
assert {msg="Division by zero"} (n != 0L);
2627
(extern dbl_modInt64 : Int64 -> Int64 -> Int64) self n
2728

29+
pub method rec pow {~__line__, ~__file__} (self : Int64) (n : Int64) =
30+
assert {msg="Negative exponential"} (n >= 0L);
31+
if n == 0L then 1L
32+
else if n % 2L == 0L then (self * self).pow (n / 2L)
33+
else self * (self * self).pow (n / 2L)
34+
2835
pub method land = (extern dbl_andInt64 : Int64 -> Int64 -> Int64)
2936
pub method lor = (extern dbl_orInt64 : Int64 -> Int64 -> Int64)
3037
pub method lxor = (extern dbl_xorInt64 : Int64 -> Int64 -> Int64)

lib/Base/Operators.fram

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ pub let (/)
1919
, method div : {~__line__, ~__file__} -> T -> U} (x : T) =
2020
x.div
2121
pub let (*) {method mul : T -> U} (x : T) = x.mul
22+
pub let (**)
23+
{ ~__line__, ~__file__
24+
, method pow : {~__line__, ~__file__} -> T -> U} (x : T) =
25+
x.pow
2226

2327
pub let (==) {method equal : T -> U} (x : T) = x.equal
2428
pub let (!=) {method neq : T -> U} (x : T) = x.neq

0 commit comments

Comments
 (0)