diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-06-25 18:55:58 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-06-25 18:55:58 -0500 |
| commit | bf647e430e3a3642c8199dc81a411d5e1586cabb (patch) | |
| tree | c95a82bcaa233e1653b702d9fa7ce54ca7dd95e6 /std/stdmath.sloth | |
| parent | 6a59bf6d5345fbe2487e1cc36c36aa6884fcc39d (diff) | |
| download | sloth-bf647e430e3a3642c8199dc81a411d5e1586cabb.tar.gz | |
Standard library beginnings
Diffstat (limited to 'std/stdmath.sloth')
| -rw-r--r-- | std/stdmath.sloth | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/std/stdmath.sloth b/std/stdmath.sloth new file mode 100644 index 0000000..ebf9a7c --- /dev/null +++ b/std/stdmath.sloth @@ -0,0 +1,69 @@ +foreign fn rand() Int; + +fn abs(x: Int) Int { + if x < 0 { + return -x; + } + return x; +} + +fn fabs(x: Float) Float { + if x < 0 { + return -x; + } + return x; +} + +fn max(x: Int, y: Int) Int { + if x > y { + return x; + } + return y; +} + +fn min(x: Int, y: Int) Int { + if x < y { + return x; + } + return y; +} + +fn fmax(x: Float, y: Float) Float { + if x > y { + return x; + } + return y; +} + +fn fmin(x: Float, y: Float) Float { + if x < y { + return x; + } + return y; +} + +fn pow(x: Int, y: Int) Int { + while y > 1 { + x = x*x; + y = y-1; + } + return x; +} + +fn floor(x: Float) Float { + return x - abs(x % 1.0); +} + +fn ceil(x: Float) Float { + if x < 0.0 { + return floor(x) - 1.0; + } + return floor(x) + 1.0; +} + +fn round(x: Float) Float { + if fabs(x % 1.0) >= 0.5 { + return ceil(x); + } + return floor(x); +} |
