Skip to content

Commit

Permalink
FIX: using 64bit integer in tuple math
Browse files Browse the repository at this point in the history
so: `(1.1.1 * 2147483648.0) = 255.255.255` and not `0.0.0`

It is not a best fix, because one can use hight enough decimal and fall into negative integer again :-/ But at least something for now.

Related to: Oldes/Rebol-issues#1974
  • Loading branch information
Oldes committed Mar 6, 2020
1 parent a467d31 commit 10aa48c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/core/t-tuple.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@
REBYTE *ap = NULL;
REBCNT len = 0;
REBCNT alen;
REBCNT v;
REBINT a;
REBI64 v;
REBI64 a;
REBDEC dec;

value = D_ARG(1);
Expand All @@ -205,11 +205,11 @@
ASSERT2(vp != NULL, RP_MISC);

if (IS_INTEGER(arg)) {
a = VAL_INT32(arg);
a = VAL_INT64(arg);
ap = 0;
} else if (IS_DECIMAL(arg) || IS_PERCENT(arg)) {
dec=VAL_DECIMAL(arg);
a = (REBINT)dec;
a = (REBI64)dec;
ap = 0;
} else if (IS_TUPLE(arg)) {
ap = VAL_TUPLE(arg);
Expand All @@ -219,22 +219,22 @@
} else Trap_Math_Args(REB_TUPLE, action);

for (;len > 0; len--, vp++) {
v = *vp;
v = (REBI64)*vp;
if (ap)
a = (REBINT) *ap++;
a = (REBI64) *ap++;
switch (action) {
case A_ADD: v += a; break;
case A_SUBTRACT: v -= a; break;
case A_MULTIPLY:
if (IS_DECIMAL(arg) || IS_PERCENT(arg))
v=(REBINT)(v*dec);
v=(REBI64)(v*dec);
else
v *= a;
break;
case A_DIVIDE:
if (IS_DECIMAL(arg) || IS_PERCENT(arg)) {
if (dec == 0.0) Trap0(RE_ZERO_DIVIDE);
v=(REBINT)Round_Dec(v/dec, 0, 1.0);
v=(REBI64)Round_Dec(v/dec, 0, 1.0); //@@ https://github.com/Oldes/Rebol-issues/issues/1974
} else {
if (a == 0) Trap0(RE_ZERO_DIVIDE);
v /= a;
Expand Down
1 change: 1 addition & 0 deletions src/tests/run-tests.r3
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dt [ ;- delta time
wrap load %units/datatype-test.r3
wrap load %units/parse-test.r3
wrap load %units/task-test.r3
wrap load %units/tuple-test.r3

recycle/torture
recycle
Expand Down
25 changes: 25 additions & 0 deletions src/tests/units/tuple-test.r3
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Rebol [
Title: "Rebol3 tuple test script"
Author: "Oldes, Peter W A Wood"
File: %tuple-test.r3
Tabs: 4
Needs: [%../quick-test-module.r3]
]

~~~start-file~~~ "TUPLE!"

===start-group=== "tuple"
--test-- "tuple divide"
;@@ https://github.com/Oldes/Rebol-issues/issues/1974
--assert (1.1.1 / 0.1) == 10.10.10
--assert (1.1.1 / 0.625) == 2.2.2 ;because round 1 / 0.625 = 2.0
--assert (1.1.1 / 1.953125E-3) == 255.255.255
--assert (1.1.1 / -1.0) == 0.0.0
--assert (1.1.1 / 4.656612873077393e-10) == 255.255.255

--test-- "tuple multiply"
--assert (1.1.1 * 2147483648.0) == 255.255.255

===end-group===

~~~end-file~~~

0 comments on commit 10aa48c

Please sign in to comment.