| 1 |
/* |
| 2 |
Implement some basic math functions. |
| 3 |
*/ |
| 4 |
|
| 5 |
/* basic arithmetic and algebra */ |
| 6 |
int factorial(int n) { |
| 7 |
int prod := 1; |
| 8 |
repeat |
| 9 |
prod := prod * n; |
| 10 |
n := n - 1; |
| 11 |
until n <= 1 |
| 12 |
return prod; |
| 13 |
} |
| 14 |
|
| 15 |
float pow(float x, int y) { |
| 16 |
if x = 0 then return 0; end |
| 17 |
if x = 1 || y = 0 then return 1; end |
| 18 |
|
| 19 |
/* handle negative powers */ |
| 20 |
negative_pow := sgn(y) < 0; |
| 21 |
y := abs(y); |
| 22 |
|
| 23 |
float result := 1; |
| 24 |
repeat |
| 25 |
result := result * x; |
| 26 |
y := y - 1; |
| 27 |
until y = 0 |
| 28 |
|
| 29 |
if negative_pow then result := 1 / result; end |
| 30 |
|
| 31 |
return result; |
| 32 |
} |
| 33 |
|
| 34 |
float abs(float a) { |
| 35 |
if a < 0 then return -a; end |
| 36 |
return a; |
| 37 |
} |
| 38 |
|
| 39 |
int sgn(float x) { |
| 40 |
if x > 0 then return 1; end |
| 41 |
if x < 0 then return -1; end |
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
float round(float a, int n) { |
| 46 |
int mag := 1; |
| 47 |
if n > 0 then |
| 48 |
repeat |
| 49 |
mag := mag * 10; |
| 50 |
n := n - 1; |
| 51 |
until n = 0 |
| 52 |
end |
| 53 |
int trunc := a * mag + 0.5; |
| 54 |
return trunc / mag; |
| 55 |
} |
| 56 |
|
| 57 |
int div(int a, int b) { |
| 58 |
int quo := a / b; |
| 59 |
return quo; |
| 60 |
} |
| 61 |
|
| 62 |
int mod(int a, int b) { |
| 63 |
int quo := a / b; |
| 64 |
int ret := a - quo * b; |
| 65 |
if ret < 0 then ret := ret + b; end |
| 66 |
return ret; |
| 67 |
} |
| 68 |
|
| 69 |
float sqrt(float a) { |
| 70 |
float eps := 0.00000000001; |
| 71 |
float y := a / 2.0; |
| 72 |
repeat |
| 73 |
if abs(y * y - a) < eps then return y; end |
| 74 |
y := (y + a / y) / 2; |
| 75 |
until 0 |
| 76 |
} |
| 77 |
|
| 78 |
float hypot(float x, float y) { |
| 79 |
return sqrt(x * x + y * y); |
| 80 |
} |
| 81 |
|
| 82 |
/* Some trig functions */ |
| 83 |
|
| 84 |
float pi() { return 3.14159265358979323846; } |
| 85 |
float rad(float deg) { return deg * pi() / 180.0; } |
| 86 |
float deg(float rad) { return rad * 180.0 / pi(); } |
| 87 |
|
| 88 |
float sin(float rad_) { |
| 89 |
/* Taylor series expansion of sin(x) */ |
| 90 |
float x := rad_; |
| 91 |
int mult := 1; |
| 92 |
float sum := x; |
| 93 |
int n := 1; |
| 94 |
int exponent := 1; |
| 95 |
repeat |
| 96 |
n := n + 1; |
| 97 |
mult := -mult; |
| 98 |
exponent := exponent + 2; |
| 99 |
sum := sum + mult * (pow(x, exponent) / factorial(exponent)); |
| 100 |
until n >= 12 |
| 101 |
return sum; |
| 102 |
} |
| 103 |
|
| 104 |
float cos(float rad_) { |
| 105 |
/* Taylor series expansion of cos(x) */ |
| 106 |
float sum := 1; |
| 107 |
int n := 1; |
| 108 |
int exponent := 0; |
| 109 |
int mult := 1; |
| 110 |
repeat |
| 111 |
n := n + 1; |
| 112 |
mult := -mult; |
| 113 |
exponent := exponent + 2; |
| 114 |
sum := sum + mult * pow(rad_, exponent) / factorial(exponent); |
| 115 |
until n >= 12 |
| 116 |
return sum; |
| 117 |
} |
| 118 |
|
| 119 |
float tan(float rad_) { |
| 120 |
min_cos := 1e-18; |
| 121 |
|
| 122 |
float num := sin(rad_); |
| 123 |
float den := cos(rad_); |
| 124 |
|
| 125 |
/* Avoid division by zero */ |
| 126 |
if abs(den) < min_cos then den := sgn(den) * min_cos; end |
| 127 |
return num / den; |
| 128 |
} |
| 129 |
|
| 130 |
float exp(float x) { |
| 131 |
/* Taylor series expansion of eˣ */ |
| 132 |
float sum := 1.0; |
| 133 |
int n := 0; |
| 134 |
if x > 0 then |
| 135 |
repeat |
| 136 |
n := n + 1; |
| 137 |
sum := sum + pow(x, n) / factorial(n); |
| 138 |
until n >= 12 |
| 139 |
end |
| 140 |
return sum; |
| 141 |
} |
| 142 |
|
| 143 |
int main() { |
| 144 |
int i := 1; |
| 145 |
repeat |
| 146 |
write "sqrt("; write i; write ") = ="; |
| 147 |
write round(sqrt(i), 4); |
| 148 |
write endl; |
| 149 |
i := i + 1; |
| 150 |
until i > 10 |
| 151 |
|
| 152 |
|
| 153 |
int a:= 0; |
| 154 |
int b; |
| 155 |
repeat |
| 156 |
a := a + 1; |
| 157 |
b := a - 1; |
| 158 |
repeat |
| 159 |
b := b + 1; |
| 160 |
write "hypot("; write a; write ", "; write b; write ") = "; |
| 161 |
write round(hypot(a, b), 3); |
| 162 |
write endl; |
| 163 |
until b >= 10 |
| 164 |
until a = 10 |
| 165 |
|
| 166 |
write "rad(180) = "; write rad(180); write endl; |
| 167 |
write "deg(pi()) = "; write deg(pi()); write endl; |
| 168 |
|
| 169 |
write "sin(rad(30)) = "; write sin(rad(30)); write endl; |
| 170 |
write "sin(rad(90)) = "; write sin(rad(90)); write endl; |
| 171 |
write "cos(rad(60)) = "; write cos(rad(60)); write endl; |
| 172 |
write "cos(rad(90)) = "; write cos(rad(90)); write endl; |
| 173 |
write "cos(rad(180)) = "; write cos(rad(180)); write endl; |
| 174 |
write "tan(rad(45)) = "; write tan(rad(45)); write endl; |
| 175 |
write "tan(0) = "; write tan(0); write endl; |
| 176 |
write "tan(pi() / 2) = "; write tan(pi() / 2); write endl; |
| 177 |
|
| 178 |
write "pow(10, 0) = "; write pow(10, 0); write endl; |
| 179 |
write "pow(10, 1) = "; write pow(10, 1); write endl; |
| 180 |
write "pow(10, 2) = "; write pow(10, 2); write endl; |
| 181 |
|
| 182 |
write "exp(0) = "; write exp(0); write endl; |
| 183 |
write "exp(1) = "; write exp(1); write endl; |
| 184 |
|
| 185 |
i := -5; |
| 186 |
repeat |
| 187 |
write "mod("; write i; write ", 3) = "; |
| 188 |
write mod(i, 3); write endl; |
| 189 |
i := i + 1; |
| 190 |
until i > 5 |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |