From beb185056790fb74a315834bcd0fdb296a71c854 Mon Sep 17 00:00:00 2001 From: Chip Salzenberg Date: Sat, 22 Mar 1997 12:40:20 +1200 Subject: [PATCH] Refine modulus ("%") per suggestion of Tim Goodwin --- pp.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pp.c b/pp.c index e3ed078..a988646 100644 --- a/pp.c +++ b/pp.c @@ -688,34 +688,43 @@ PP(pp_modulo) { UV left; UV right; - bool negate; + bool left_neg; + bool right_neg; UV ans; if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) { IV i = SvIVX(POPs); - right = (i < 0) ? -i : i; + right = (right_neg = (i < 0)) ? -i : i; } else { double n = POPn; - right = U_V((n < 0) ? -n : n); + right = U_V((right_neg = (n < 0)) ? -n : n); } if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) { IV i = SvIVX(POPs); - left = (negate = (i < 0)) ? -i : i; + left = (left_neg = (i < 0)) ? -i : i; } else { double n = POPn; - left = U_V((negate = (n < 0)) ? -n : n); + left = U_V((left_neg = (n < 0)) ? -n : n); } if (!right) DIE("Illegal modulus zero"); ans = left % right; - if (negate && ans) + if ((left_neg != right_neg) && ans) ans = right - ans; - PUSHu(ans); + if (right_neg) { + if (ans <= -(UV)IV_MAX) + sv_setiv(TARG, (IV) -ans); + else + sv_setnv(TARG, -(double)ans); + } + else + sv_setuv(TARG, ans); + PUSHTARG; RETURN; } } -- 2.7.4