From cd0a0923a826754b76743ab4e5014c8cffc02839 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 11 Jan 2026 18:36:07 -0500 Subject: More cleanups and fixes --- src/stdlib/reals.c | 28 +++++++++++++++++++++------- src/stdlib/reals.h | 3 ++- 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'src/stdlib') 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 @@ -117,6 +115,22 @@ static Real_t promote_double(double d) { return box_ptr(r, REAL_TAG_RATIONAL); } +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); -- cgit v1.2.3