Skip to content

Commit 4a0d814

Browse files
committed
FEAT: counting an hourly money rate using division of time! with money!
related to: Oldes/Rebol-issues#2497
1 parent 8219628 commit 4a0d814

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/core/t-time.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
// these are used in TIME * MONEY action
3333
deci deci_multiply(const deci a, const deci b);
34+
deci deci_divide(const deci a, const deci b);
3435
deci decimal_to_deci(REBDEC a);
3536

3637
/***********************************************************************
@@ -377,6 +378,7 @@ deci decimal_to_deci(REBDEC a);
377378
REBVAL *val;
378379
REBVAL *arg = NULL;
379380
REBI64 num;
381+
deci hours; // used with money type math
380382

381383
val = D_ARG(1);
382384

@@ -488,9 +490,17 @@ deci decimal_to_deci(REBDEC a);
488490
secs = (REBI64)(secs * VAL_DECIMAL(arg));
489491
goto setTime;
490492
}
491-
else if (type == REB_MONEY && action == A_MULTIPLY) { // handle TIME * MONEY case
493+
else if (type == REB_MONEY && (action == A_MULTIPLY || action == A_DIVIDE)) {
492494
// https://github.com/Oldes/Rebol-issues/issues/2497
493-
VAL_DECI(D_RET) = deci_multiply(decimal_to_deci(secs * NANO / 3600.0), VAL_DECI(arg));
495+
hours = decimal_to_deci(secs * NANO / 3600.0);
496+
if (action == A_MULTIPLY) {
497+
// handle TIME * MONEY case
498+
VAL_DECI(D_RET) = deci_multiply(hours, VAL_DECI(arg));
499+
}
500+
else {
501+
// handle TIME / MONEY case (an horly money rate)
502+
VAL_DECI(D_RET) = deci_divide(VAL_DECI(arg), hours);
503+
}
494504
SET_TYPE(D_RET, REB_MONEY);
495505
return R_RET;
496506
}

src/tests/units/time-test.r3

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Rebol [
127127
--test-- "time! money! math"
128128
;@@ https://github.com/Oldes/Rebol-issues/issues/2497
129129
--assert $7.5 = (1:30:0 * $5)
130-
--assert error? try [1:30:0 / $5]
130+
--assert $25 = (4:0:0 / $100) ; an hourly rate
131131
--assert error? try [1:30:0 + $5]
132132
--assert error? try [1:30:0 - $5]
133133

0 commit comments

Comments
 (0)