diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2026-01-11 18:36:07 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2026-01-11 18:36:07 -0500 |
| commit | cd0a0923a826754b76743ab4e5014c8cffc02839 (patch) | |
| tree | 75b501f6029345840b3f3bd25c8f274f04884350 /src/stdlib | |
| parent | 0a9cf2636291a7d7b198cb6bae32befe3e3ea0ee (diff) | |
More cleanups and fixes
Diffstat (limited to 'src/stdlib')
| -rw-r--r-- | src/stdlib/reals.c | 28 | ||||
| -rw-r--r-- | src/stdlib/reals.h | 3 |
2 files changed, 23 insertions, 8 deletions
diff --git a/src/stdlib/reals.c b/src/stdlib/reals.c index f8b8991f..cf9d5560 100644 --- a/src/stdlib/reals.c +++ b/src/stdlib/reals.c @@ -75,9 +75,7 @@ static inline Real_t box_ptr(void *ptr, uint64_t tag) { } static inline Real_t make_double(double d) { - Real_t n; - n.d = d; - return n; + return (Real_t){.d = d}; } public @@ -118,6 +116,22 @@ static Real_t promote_double(double d) { } public +CONSTFUNC Real_t Real$from_float64(double n) { + return make_double(n); // Preserve sign of zero +} + +public +Real_t Real$from_int(Int_t i) { + double d = Float64$from_int(i, true); + if (Int$equal_value(i, Int$from_float64(d, true))) + return make_double(d); + + Int_t *b = GC_MALLOC(sizeof(Int_t)); + *b = i; + return box_ptr(b, REAL_TAG_BIGINT); +} + +public double Real$as_float64(Real_t n, bool truncate) { if (!is_boxed(n)) return n.d; @@ -455,7 +469,7 @@ static Text_t format_binary_op(Text_t left, Text_t right, const char *op, bool c const char *operator_color = colorize ? "\033[33m" : ""; const char *paren_color = colorize ? "\033[37m" : ""; const char *reset = colorize ? "\033[m" : ""; - + return colorize ? Texts(paren_color, "(", reset, left, operator_color, op, reset, right, paren_color, ")", reset) : Texts("(", left, op, right, ")"); } @@ -465,7 +479,7 @@ static Text_t format_unary_func(Text_t arg, const char *func_name, bool colorize const char *operator_color = colorize ? "\033[33m" : ""; const char *paren_color = colorize ? "\033[37m" : ""; const char *reset = colorize ? "\033[m" : ""; - + return colorize ? Texts(operator_color, func_name, paren_color, "(", reset, arg, paren_color, ")", reset) : Texts(func_name, "(", arg, ")"); } @@ -475,7 +489,7 @@ static Text_t format_binary_func(Text_t left, Text_t right, const char *func_nam const char *operator_color = colorize ? "\033[33m" : ""; const char *paren_color = colorize ? "\033[37m" : ""; const char *reset = colorize ? "\033[m" : ""; - + return colorize ? Texts(operator_color, func_name, paren_color, "(", reset, left, operator_color, ", ", reset, right, paren_color, ")", reset) : Texts(func_name, "(", left, ", ", right, ")"); } @@ -484,7 +498,7 @@ static Text_t format_binary_func(Text_t left, Text_t right, const char *func_nam static Text_t format_constant(const char *symbol, bool colorize) { const char *number_color = colorize ? "\033[35m" : ""; const char *reset = colorize ? "\033[m" : ""; - + return colorize ? Texts(number_color, symbol, reset) : Text$from_str(symbol); } diff --git a/src/stdlib/reals.h b/src/stdlib/reals.h index d03e4e5f..63742330 100644 --- a/src/stdlib/reals.h +++ b/src/stdlib/reals.h @@ -34,8 +34,9 @@ Real_t Real$cos(Real_t x); Real_t Real$divided_by(Real_t x, Real_t y); Real_t Real$exp(Real_t x); Real_t Real$floor(Real_t x); -Real_t Real$from_float64(double n); +CONSTFUNC Real_t Real$from_float64(double n); Real_t Real$from_int(Int_t i); +Real_t Real$from_rational(int64_t num, int64_t den); Real_t Real$from_text(Text_t text); Real_t Real$log(Real_t x); Real_t Real$log10(Real_t x); |
