]> git.vpit.fr Git - perl/modules/Sub-Nary.git/blobdiff - Nary.xs
Fix edge case in cumulate() when the coefficient is 1
[perl/modules/Sub-Nary.git] / Nary.xs
diff --git a/Nary.xs b/Nary.xs
index bcdde48999c3c4fc881ee43c181495fe4ca9d3c7..7a78e938aaf92804a91840b40ca81b438617424c 100644 (file)
--- 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);