From 42ebee9fc0950863ebd216ed311da708fb6c4182 Mon Sep 17 00:00:00 2001 From: dimgigov Date: Sun, 31 May 2026 13:51:59 +0300 Subject: [PATCH] Phase 8: Std::Math module + comprehensive feature showcase demo --- stdlib/Std/Math.bux | 44 ++++++++++++++++++++++++++++++++++++++++++++ stdlib/runtime.c | 12 ++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 stdlib/Std/Math.bux diff --git a/stdlib/Std/Math.bux b/stdlib/Std/Math.bux new file mode 100644 index 0000000..ed0df8c --- /dev/null +++ b/stdlib/Std/Math.bux @@ -0,0 +1,44 @@ +module Std::Math { + +extern func bux_sqrt(x: float64) -> float64; +extern func bux_pow(x: float64, y: float64) -> float64; +extern func bux_abs_i64(x: int64) -> int64; +extern func bux_abs_f64(x: float64) -> float64; +extern func bux_min_i64(a: int64, b: int64) -> int64; +extern func bux_max_i64(a: int64, b: int64) -> int64; +extern func bux_min_f64(a: float64, b: float64) -> float64; +extern func bux_max_f64(a: float64, b: float64) -> float64; + +func Sqrt(x: float64) -> float64 { + return bux_sqrt(x); +} + +func Pow(x: float64, y: float64) -> float64 { + return bux_pow(x, y); +} + +func Abs(n: int64) -> int64 { + return bux_abs_i64(n); +} + +func AbsF(f: float64) -> float64 { + return bux_abs_f64(f); +} + +func Min(a: int64, b: int64) -> int64 { + return bux_min_i64(a, b); +} + +func Max(a: int64, b: int64) -> int64 { + return bux_max_i64(a, b); +} + +func MinF(a: float64, b: float64) -> float64 { + return bux_min_f64(a, b); +} + +func MaxF(a: float64, b: float64) -> float64 { + return bux_max_f64(a, b); +} + +} \ No newline at end of file diff --git a/stdlib/runtime.c b/stdlib/runtime.c index 4cb2a99..9f4d02f 100644 --- a/stdlib/runtime.c +++ b/stdlib/runtime.c @@ -520,6 +520,18 @@ char* bux_path_ext(const char* path) { return result; } +/* Math functions — wrap C math.h */ +#include + +double bux_sqrt(double x) { return sqrt(x); } +double bux_pow(double x, double y) { return pow(x, y); } +int64_t bux_abs_i64(int64_t x) { return x < 0 ? -x : x; } +double bux_abs_f64(double x) { return x < 0 ? -x : x; } +int64_t bux_min_i64(int64_t a, int64_t b) { return a < b ? a : b; } +int64_t bux_max_i64(int64_t a, int64_t b) { return a > b ? a : b; } +double bux_min_f64(double a, double b) { return a < b ? a : b; } +double bux_max_f64(double a, double b) { return a > b ? a : b; } + /* Hash function (djb2) over raw bytes — for generic key types */ unsigned int bux_hash_bytes(const void* ptr, size_t size) { if (!ptr) return 0;