Skip to content

Commit

Permalink
FEAT: allow multiplication between time! and money! values
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Jun 13, 2022
1 parent 8e06062 commit 8219628
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core/t-money.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
VAL_DECI(D_RET) = decimal_to_deci(VAL_DECIMAL(arg));
arg = D_RET;
}
else if (IS_TIME(arg) && action == A_MULTIPLY) {
VAL_DECI(D_RET) = decimal_to_deci(VAL_TIME(arg) * NANO / 3600.0);
arg = D_RET;
}
else Trap_Math_Args(REB_MONEY, action);

switch (action) {
Expand Down
12 changes: 11 additions & 1 deletion src/core/t-time.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

#include "sys-core.h"

// these are used in TIME * MONEY action
deci deci_multiply(const deci a, const deci b);
deci decimal_to_deci(REBDEC a);

/***********************************************************************
**
*/ void Split_Time(REBI64 t, REB_TIMEF *tf)
Expand Down Expand Up @@ -477,13 +481,19 @@
T_Date(ds, action);
return R_RET;
}
else if (type == REB_PERCENT && action == A_MULTIPLY) { // handle PERCENT * TIME case
else if (type == REB_PERCENT && action == A_MULTIPLY) { // handle TIME * PERCENT case
// https://github.com/Oldes/Rebol-issues/issues/1391
//O: this could be handled like REB_DECIMAL above, but I think that support
//O: for actions like A_ADD does not make sense, so only MULTIPLY is supported!
secs = (REBI64)(secs * VAL_DECIMAL(arg));
goto setTime;
}
else if (type == REB_MONEY && action == A_MULTIPLY) { // handle TIME * MONEY case
// https://github.com/Oldes/Rebol-issues/issues/2497
VAL_DECI(D_RET) = deci_multiply(decimal_to_deci(secs * NANO / 3600.0), VAL_DECI(arg));
SET_TYPE(D_RET, REB_MONEY);
return R_RET;
}
Trap_Math_Args(REB_TIME, action);
}
else {
Expand Down
7 changes: 7 additions & 0 deletions src/tests/units/money-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ Rebol [
;@@ https://github.com/Oldes/Rebol-issues/issues/569
--assert not strict-equal? $1 1

--test-- "money! time! math"
;@@ https://github.com/Oldes/Rebol-issues/issues/2497
--assert $7.5 = ($5 * 1:30:0)
--assert error? try [$5 / 1:30:0]
--assert error? try [$5 + 1:30:0]
--assert error? try [$5 - 1:30:0]

===end-group===

~~~end-file~~~
7 changes: 7 additions & 0 deletions src/tests/units/time-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ Rebol [
--assert -596523:14:07 = to-time negate (to-integer #{8000 0000}) - 1
--assert all [error? e: try [to-time 2 ** 63 / (10 ** 9) - 1 + 0.9999999] e/id = 'out-of-range]

--test-- "time! money! math"
;@@ https://github.com/Oldes/Rebol-issues/issues/2497
--assert $7.5 = (1:30:0 * $5)
--assert error? try [1:30:0 / $5]
--assert error? try [1:30:0 + $5]
--assert error? try [1:30:0 - $5]

===end-group===

===start-group=== "random"
Expand Down

0 comments on commit 8219628

Please sign in to comment.