You don't have to write everything yourself. steav2 comes with ready-made collections of useful functions, called modules. To use one, you import it with yok (which means import, or "take"), and its functions become available in your program.
steav2 has three modules built in:
| module | what's inside |
|---|---|
math |
square roots, powers, rounding, sin and cos, PI, ... |
random |
random numbers, for games and surprises |
vector |
Vec2, Vec3 and Vec4: points and arrows in 2D, 3D and 4D |
yok math;
bongSlanhOun "maths";
yok math;
jongyeytha sqrt(16);
jongyeytha pow(2, 10);
jongyeytha floor(3.7);
jongyeytha max(4, 9);
jongyeytha PI; 4
1024
3
9
3.141592653589793 sqrt(x)is the square root: which number times itself makesx?pow(a, b)isato the powerb:2 * 2 * 2 * ..., ten times.floor(x)rounds down, andceil(x)rounds up.min(a, b)andmax(a, b)pick the smaller or bigger number.PIis a constant: a named value that never changes.
The yok line usually goes at the top, just after bongSlanhOun. Without it, steav2 has never heard of sqrt:
bongSlanhOun "forgot";
jongyeytha sqrt(16); [line 2] Error at 'sqrt': undefined name 'sqrt' Here's math doing something useful, the area of a circle (pi times the radius squared):
bongSlanhOun "circle";
yok math;
rupamun circleArea(radius: Lek) -> Lek {
morvenh PI * pow(radius, 2);
}
jongyeytha "A pizza with radius 15 cm has " + floor(circleArea(15)) + " square cm of pizza"; A pizza with radius 15 cm has 706 square cm of pizza Nicknames with jea
jea means as. yok math jea m; imports math under the nickname m, and then you write m. in front of everything from it:
bongSlanhOun "alias";
yok math jea m;
jongyeytha m.sqrt(2);
jongyeytha m.PI * 2; 1.4142135623730951
6.283185307179586 This is handy when a name from a module clashes with one of yours. If you wrote your own function called sqrt, a plain yok math; would be an error, but yok math jea m; keeps the two apart: yours is sqrt, the module's is m.sqrt.
yok random;
random gives you random numbers:
uniform()gives a random decimal between 0 and 1.interval(min, max)gives a random decimal betweenminandmax(it can bemin, but never quitemax).seed(n)picks which random sequence you get (more on that in a moment).
Let's make a dice! interval(1, 7) gives numbers like 4.457..., and LekKut(...) chops off the decimals, leaving a whole number from 1 to 6:
bongSlanhOun "dice";
yok random;
rupamun rollDie() -> LekKut {
morvenh LekKut(interval(1, 7));
}
somhab (akthe i: LekKut = 1; i <= 5; i += 1) {
jongyeytha "roll " + i + ": " + rollDie();
} roll 1: 6
roll 2: 4
roll 3: 5
roll 4: 6
roll 5: 6 Run it a few times. Notice anything? You get the same rolls every time! Computer "random" numbers are really a long, jumbled-up sequence, and steav2 always starts at the same place in it. That's actually useful (a bug that involves random numbers happens the same way every time, so you can find it). For a different sequence, call seed with any whole number you like:
bongSlanhOun "seeded dice";
yok random;
seed(2024);
somhab (akthe i: LekKut = 1; i <= 3; i += 1) {
jongyeytha LekKut(interval(1, 7));
} 5
1
2 Change 2024 to something else, like your birthday, and you'll get different rolls.
yok vector;
A vector is a group of numbers that belong together, like the x and y position of something on the screen. Vec2 has an x and a y, and Vec3 adds a z for 3D. They're structs (remember sampoan?), with lots of maths built in:
bongSlanhOun "vectors";
yok vector;
akthe position = Vec2(0, 0);
akthe speed = Vec2(3, 4);
somhab (akthe second: LekKut = 1; second <= 3; second += 1) {
position = position + speed;
jongyeytha "after " + second + "s: " + position;
}
jongyeytha "distance from start: " + position.length(); after 1s: Vec2(3, 4)
after 2s: Vec2(6, 8)
after 3s: Vec2(9, 12)
distance from start: 15 position + speed adds the xs together and the ys together, and .length() tells you how long the arrow from (0, 0) to the point is. That's the kind of maths that moves characters in games, and it's exactly what the ray tracer example in the File menu uses to draw 3D pictures!
Your turn
- Use
mathto work out the length of the long side of a right-angled triangle with sides 5 and 12. Hint:sqrt(5 * 5 + 12 * 12). - Roll two dice 36 times and print how many times you got a double (both the same). On average it's 1 roll in 6, so about 6, but luck varies!
Hint: keep a
LekKutcounter and+= 1it inside aber. - Make a
Vec3(1, 2, 3)and print it multiplied by2.
Solution for exercise 2:
bongSlanhOun "doubles";
yok random;
rupamun rollDie() -> LekKut {
morvenh LekKut(interval(1, 7));
}
akthe doubles: LekKut = 0;
somhab (akthe i: LekKut = 0; i < 36; i += 1) {
ber (rollDie() == rollDie()) {
doubles += 1;
}
}
jongyeytha "doubles: " + doubles; doubles: 3 Only 3 this time. Try a seed(...) line and see if your luck changes!