-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.kat
55 lines (47 loc) · 1.29 KB
/
math.kat
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
/**
* Opérations communes sur les entiers.
*/
class Math() is {
def Math() is {}
/** Modulo (ou reste de division euclidienne) sur entiers positifs. */
def static mod(i, m: Integer): Integer is {
result := i - i / m;
}
/** Puissance positive. */
def static pow(n, exp: Integer): Integer is {
if exp <= 0
then result := 1;
else result := n * Math.pow(n, exp - 1);
}
/** Décalage à gauche des bits. */
def static leftShift(n, shift: Integer): Integer is {
if shift <= 0
then result := n;
else result := Math.leftShift(n * 2, shift - 1);
}
/** Décalage à droite des bits. */
def static rightShift(n, shift: Integer): Integer is {
if shift <= 0
then result := n;
else result := Math.leftShift(n / 2, shift - 1);
}
/** Nombre pseudo-aléatoire. */
def static rand(seed: Integer): Integer is {
/* Approx. ANSI C LCG */
result := Math.leftShift(Math.mod(1103515245 * seed + 12345, 2147483648), 30);
}
}
{
r: Integer
is
r := 10;
"r0: ".print(); r.toString().println();
r := Math.rand(r);
"r1: ".print(); r.toString().println();
r := Math.rand(r);
"r2: ".print(); r.toString().println();
r := Math.rand(r);
"r3: ".print(); r.toString().println();
r := Math.rand(r);
"r4: ".print(); r.toString().println();
}