-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.mo
63 lines (52 loc) · 1.47 KB
/
calculator.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import Int "mo:base/Int";
import Float "mo:base/Float";
actor Calculator {
// Step 1 - Define a mutable variable called `counter`.
var counter : Float = 0.0;
// Step 2 - Implement add
public func add(x : Float) : async Float {
counter := counter + x;
return counter;
};
// Step 3 - Implement sub
public func sub(x : Float) : async Float {
counter:= counter - x;
return counter;
};
// Step 4 - Implement mul
public func mul(x : Float) : async Float {
counter:= counter * x;
return counter;
};
// Step 5 - Implement div
public func div(x : Float) : async ?Float {
if (x == 0.0) {
return null;
} else {
counter:= counter / x;
return ?counter;
};
};
// Step 6 - Implement reset
public func reset(): async () {
counter:= 0.0;
};
// Step 7 - Implement query
public query func see() : async Float {
return counter;
};
// Step 8 - Implement power
public func power(x : Float) : async Float {
counter:=Float.pow(counter,x);
return counter;
};
// Step 9 - Implement sqrt
public func sqrt() : async Float {
counter:=Float.sqrt(counter);
return counter;
};
// Step 10 - Implement floor
public func floor() : async Int {
return Float.toInt(Float.floor(counter));
};
};