Skip to content

Commit 10aa48c

Browse files
committed
FIX: using 64bit integer in tuple math
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
1 parent a467d31 commit 10aa48c

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/core/t-tuple.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@
190190
REBYTE *ap = NULL;
191191
REBCNT len = 0;
192192
REBCNT alen;
193-
REBCNT v;
194-
REBINT a;
193+
REBI64 v;
194+
REBI64 a;
195195
REBDEC dec;
196196

197197
value = D_ARG(1);
@@ -205,11 +205,11 @@
205205
ASSERT2(vp != NULL, RP_MISC);
206206

207207
if (IS_INTEGER(arg)) {
208-
a = VAL_INT32(arg);
208+
a = VAL_INT64(arg);
209209
ap = 0;
210210
} else if (IS_DECIMAL(arg) || IS_PERCENT(arg)) {
211211
dec=VAL_DECIMAL(arg);
212-
a = (REBINT)dec;
212+
a = (REBI64)dec;
213213
ap = 0;
214214
} else if (IS_TUPLE(arg)) {
215215
ap = VAL_TUPLE(arg);
@@ -219,22 +219,22 @@
219219
} else Trap_Math_Args(REB_TUPLE, action);
220220

221221
for (;len > 0; len--, vp++) {
222-
v = *vp;
222+
v = (REBI64)*vp;
223223
if (ap)
224-
a = (REBINT) *ap++;
224+
a = (REBI64) *ap++;
225225
switch (action) {
226226
case A_ADD: v += a; break;
227227
case A_SUBTRACT: v -= a; break;
228228
case A_MULTIPLY:
229229
if (IS_DECIMAL(arg) || IS_PERCENT(arg))
230-
v=(REBINT)(v*dec);
230+
v=(REBI64)(v*dec);
231231
else
232232
v *= a;
233233
break;
234234
case A_DIVIDE:
235235
if (IS_DECIMAL(arg) || IS_PERCENT(arg)) {
236236
if (dec == 0.0) Trap0(RE_ZERO_DIVIDE);
237-
v=(REBINT)Round_Dec(v/dec, 0, 1.0);
237+
v=(REBI64)Round_Dec(v/dec, 0, 1.0); //@@ https://github.com/Oldes/Rebol-issues/issues/1974
238238
} else {
239239
if (a == 0) Trap0(RE_ZERO_DIVIDE);
240240
v /= a;

src/tests/run-tests.r3

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dt [ ;- delta time
4747
wrap load %units/datatype-test.r3
4848
wrap load %units/parse-test.r3
4949
wrap load %units/task-test.r3
50+
wrap load %units/tuple-test.r3
5051

5152
recycle/torture
5253
recycle

src/tests/units/tuple-test.r3

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Rebol [
2+
Title: "Rebol3 tuple test script"
3+
Author: "Oldes, Peter W A Wood"
4+
File: %tuple-test.r3
5+
Tabs: 4
6+
Needs: [%../quick-test-module.r3]
7+
]
8+
9+
~~~start-file~~~ "TUPLE!"
10+
11+
===start-group=== "tuple"
12+
--test-- "tuple divide"
13+
;@@ https://github.com/Oldes/Rebol-issues/issues/1974
14+
--assert (1.1.1 / 0.1) == 10.10.10
15+
--assert (1.1.1 / 0.625) == 2.2.2 ;because round 1 / 0.625 = 2.0
16+
--assert (1.1.1 / 1.953125E-3) == 255.255.255
17+
--assert (1.1.1 / -1.0) == 0.0.0
18+
--assert (1.1.1 / 4.656612873077393e-10) == 255.255.255
19+
20+
--test-- "tuple multiply"
21+
--assert (1.1.1 * 2147483648.0) == 255.255.255
22+
23+
===end-group===
24+
25+
~~~end-file~~~

0 commit comments

Comments
 (0)