From: Vincent Pit Date: Wed, 20 Aug 2008 13:04:11 +0000 (+0200) Subject: Fix edge case in cumulate() when the coefficient is 1 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FSub-Nary.git;a=commitdiff_plain;h=622118ca9de1af186f318d89d9058399b589b4f4 Fix edge case in cumulate() when the coefficient is 1 --- diff --git a/Nary.xs b/Nary.xs index bcdde48..7a78e93 100644 --- a/Nary.xs +++ b/Nary.xs @@ -218,14 +218,18 @@ CODE: sv = SvRV(sv); if (!hv_iterinit((HV *) sv)) XSRETURN_UNDEF; - c = 1; - a = c0; - for (; n > 0; n /= 2) { - if (n % 2) - c *= a; - a *= a; + if (c0 == 1 || (SvIOK(csv) && SvIV(csv) == 1)) { + c = n; + } else { + c = 1; + a = c0; + for (; n > 0; n /= 2) { + if (n % 2) + c *= a; + a *= a; + } + c = (1 - c) / (1 - c0); } - c = (1 - c) / (1 - c0); res = newHV(); while (key = hv_iternext((HV *) sv)) { SV *k = HeSVKEY_force(key);