-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cx
More file actions
146 lines (116 loc) · 2.6 KB
/
math.cx
File metadata and controls
146 lines (116 loc) · 2.6 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package math
/* Mathematical constants */
// The mathematical constant pi with 10 decimals precision.
var PI f64 = 3.1415926536D
// The mathematical constant e with 10 decimals precision.
var E f64 = 2.7182818284D
/* Angular functions */
// Convert angle rad from radians to degrees.
func degrees(rad f64) (deg f64) {
deg = f64.mul(rad, f64.div(180.0D, PI))
}
// Convert angle deg from radians to degrees.
func radians(deg f64) (rad f64) {
rad = f64.mul(deg, f64.div(PI, 180.0D))
}
/* Power and Logarithmic funcions */
// Calculates a raised to the power of b.
func pow(base f64, exp i64) (result f64) {
if i64.eq(exp, 0L) {
result = 1.0D;
} else {
result = f64.mul(pow(base, i64.div(exp, 2L)), pow(base, i64.div(exp, 2L)));
if (odd(exp)) {
result = f64.mul(base, result);
}
}
if i64.lt(exp, 0L) {
result = f64.div(result, 1.0D);
}
return result;
}
func pow10(exp i64) (result f64) {
result = pow(10.0D, exp);
}
/* Number representation functions */
// Returns the absolte value of n.
func abs(n f64) (result f64) {
if gteq(n, 0.0D) {
result = n;
} else {
result = f64.sub(0.0D, n);
}
return result;
}
// Returns the inverse value of n.
func inv(n f64) (result f64) {
result = f64.sub(0.0D, n);
}
// Returns the largest integer value less than or equal to x.
func floor(n f64) (result i64) {
if f64.gteq(n, 0.0D) {
result = f64.i64(n)
} else {
if (f64.eq(n, i64.f64(f64.i64(n)))) {
result = f64.i64(n)
} else {
result = i64.sub(f64.i64(n), 1L)
}
}
return result;
}
// Returns the smallest integer value greater than or equal to x.
func ceil(n f64) (result i64) {
if (f64.eq(n, i64.f64(f64.i64(n)))) {
result = f64.i64(n)
} else {
if f64.gt(n, 0.0D) {
result = i64.add(f64.i64(n), 1L)
} else {
result = f64.i64(n)
}
}
return result;
}
func remainder(num i64, divisor i64) (remainder i64) {
if i64.eq(divisor, 0L) {
remainder = num;
} else {
remainder = i64.sub(num, i64.mul(divisor, i64.div(num, divisor)))
}
}
func odd(num i64) (is_odd bool) {
is_odd = i64.eq(mod(num, 2L), 1L)
}
func even(num i64) (is_even bool) {
is_even = i64.eq(mod(num, 2L), 0L)
}
/* Other functions */
func sum(slice []f64) (result f64) {
result = 0.0D;
for i:=0; i<len(slice); i++ {
result = f64.add(result, slice[i])
}
return result;
}
func min(x f64, y f64) (result f64) {
if f64.lteq(x, y) {
result = x;
} else {
result = y;
}
}
func max(x f64, y f64) (result f64) {
if f64.gteq(x, y) {
result = x;
} else {
result = y;
}
}
func factorial(n i64) (result i64) {
if i64.eq(n, 0L) {
result = 1L;
} else {
result = i64.mul(n, factorial(i64.sub(n, 1L)))
}
}