From 62a5b8f0725ffec59ab95154e140e462fb39cd6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20von=20Sydow?= Date: Sat, 11 Nov 2023 14:06:31 +0100 Subject: [PATCH 1/2] fixed bug in round in int instance of Integral --- base/builtin/int.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/base/builtin/int.c b/base/builtin/int.c index e1f6808a7..bf11ed5dc 100644 --- a/base/builtin/int.c +++ b/base/builtin/int.c @@ -246,10 +246,12 @@ B_float B_IntegralD_intD___float__ (B_IntegralD_int wit, B_int n) { $WORD B_IntegralD_intD___ceil__ (B_IntegralD_int wit, B_int n, B_Integral wit2) { return wit2->$class->__fromatom__(wit2,(B_atom)n); } - + +B_int B_IntegralD_intD___floordiv__(B_IntegralD_int wit, B_int a, B_int b); + B_int B_IntegralD_intD___round__ (B_IntegralD_int wit, B_int n, B_int p) { zz_struct nval = n->val; - if (nval.size < 0) { + if (nval.size < 0) { B_int n1 = malloc_int(); zz_neg(&n1->val,&nval); B_int res = B_IntegralD_intD___round__(wit,n1,p); @@ -265,7 +267,9 @@ B_int B_IntegralD_intD___round__ (B_IntegralD_int wit, B_int n, B_int p) { if (pval>=0) return n; B_int p10 = B_IntegralD_intD___pow__(NULL,to$int(10), B_IntegralD_intD___neg__(NULL,p)); - return B_IntegralD_intD___mul__(NULL,n,p10); + B_int p10half = B_IntegralD_intD___floordiv__(NULL, p10,to$int(2)); + B_int n1 = B_IntegralD_intD___floordiv__(NULL,B_IntegralD_intD___add__(NULL,n,p10half),p10); + return B_IntegralD_intD___mul__(NULL,n1,p10); } $WORD B_IntegralD_intD_numerator (B_IntegralD_int wit, B_int n, B_Integral wit2) { From c74929412f1ab88cc2f572144a543392723d535d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20von=20Sydow?= Date: Sat, 11 Nov 2023 14:21:52 +0100 Subject: [PATCH 2/2] fixed test of rouding intest/builtins_auto/int.act --- test/builtins_auto/int.act | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/builtins_auto/int.act b/test/builtins_auto/int.act index 8fafa6831..b8b825d63 100644 --- a/test/builtins_auto/int.act +++ b/test/builtins_auto/int.act @@ -59,7 +59,8 @@ def test_u32_divzero(): u32(3) // u32(0) except ZeroDivisionError: return - raise testing.NotRaisesError("expected ZeroDivisionError for u32") + + raise testing.NotRaisesError("expected ZeroDivisionError for u32") def test_u64_divzero(): try: @@ -147,9 +148,9 @@ actor main(env): raise ValueError("10**500/10**500 != 1") if (round(123456789, 10)) != 1.23456789e8: raise ValueError("round(123456789, 10) != 1.23456789e8") - if round(123456789, -10) != 1234567890000000000: + if round(123456789, -4) != 123460000: raise ValueError("round(123456789, -10) != 1234567890000000000") - if round(-123456789, -10) != -1234567890000000000: + if round(-123456789, -4) != -123460000: raise ValueError("round(-123456789, -10) != -1234567890000000000") if int('123') != 123: raise ValueError("int('123') != 123")