Skip to content

Commit

Permalink
Merge pull request #1577 from actonlang/fix_int_rounded
Browse files Browse the repository at this point in the history
Fix int rounded
  • Loading branch information
sydow authored Nov 11, 2023
2 parents 81e16fa + c749294 commit 08bd695
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions base/builtin/int.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions test/builtins_auto/int.act
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 08bd695

Please sign in to comment.