diff options
| author | Cody <cody@codyq.dev> | 2023-06-25 23:23:36 -0500 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-06-25 23:23:36 -0500 |
| commit | 91ac969f992521b665bb41c1c024a5f69ca6df67 (patch) | |
| tree | 8562a40e5be385bb7d7f4a69d0840fc8493c71d9 /std/stdmath.sloth | |
| parent | ae4c1af949c7230c07b2a7cc86a2e4031c37f651 (diff) | |
| parent | c3bb1751d7f62a13a46f28c1eb67d9e20d8d6f7f (diff) | |
| download | sloth-91ac969f992521b665bb41c1c024a5f69ca6df67.tar.gz | |
Merge branch 'master' of github.com:slothlang/sloth
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..7ff5c82 --- /dev/null +++ b/std/stdmath.sloth @@ -0,0 +1,69 @@ +foreign fn randGen(min: Int, max: Int) Int; + +fn abs(x: Int) Int { + if x < 0 { + return -x; + } + return x; +} + +fn fabs(x: Float) Float { + if x < 0.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 - fabs(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); +} |
