[libmath-prime-util-perl] 12/16: Fix issue with sqrt rounding

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:44:26 UTC 2015


This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.06
in repository libmath-prime-util-perl.

commit c62ab6f50579de04b218c7d5c1289522dd050285
Author: Dana Jacobsen <dana at acm.org>
Date:   Mon Jun 11 17:52:47 2012 -0600

    Fix issue with sqrt rounding
---
 XS.xs                    | 12 ++++++++----
 examples/bench-factor.pl |  2 +-
 factor.c                 |  6 +++---
 lib/Math/Prime/Util.pm   |  4 ++--
 4 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/XS.xs b/XS.xs
index bf9a5b7..017db4f 100644
--- a/XS.xs
+++ b/XS.xs
@@ -147,7 +147,7 @@ segment_primes(IN UV low, IN UV high, IN UV segment_size = 65536UL)
 
       {  /* Avoid recalculations of this */
         UV endp = (high_d >= (UV_MAX/30))  ?  UV_MAX-2  :  30*high_d+29;
-        prime_precalc( sqrt(endp) + 1 );
+        prime_precalc( sqrt(endp) + 0.1 + 1 );
       }
 
       while ( low_d <= high_d ) {
@@ -272,10 +272,14 @@ factor(IN UV n)
             /* For sufficiently large n, try more complex methods. */
             /* SQUFOF (succeeds 98-99.9%) */
             split_success = squfof_factor(n, factor_stack+nstack, 256*1024)-1;
-            /* a few rounds of Pollard rho (succeeds most of the rest) */
+            /* A few rounds of Pollard rho (succeeds most of the rest) */
             if (!split_success) {
               split_success = prho_factor(n, factor_stack+nstack, 400)-1;
             }
+            /* Some rounds of HOLF, good for close to perfect squares */
+            if (!split_success) {
+              split_success = holf_factor(n, factor_stack+nstack, 2000)-1;
+            }
           }
           if (split_success) {
             MPUassert( split_success == 1, "split factor returned more than 2 factors");
@@ -285,13 +289,13 @@ factor(IN UV n)
             /* trial divisions */
             UV f = tlim;
             UV m = tlim % 30;
-            UV limit = sqrt((double) n);
+            UV limit = (UV) (sqrt(n)+0.1);
             while (f <= limit) {
               if ( (n%f) == 0 ) {
                 do {
                   n /= f;  XPUSHs(sv_2mortal(newSVuv( f )));
                 } while ( (n%f) == 0 );
-                limit = sqrt((double) n);
+                limit = (UV) (sqrt(n)+0.1);
               }
               f += wheeladvance30[m];
               m =  nextwheel30[m];
diff --git a/examples/bench-factor.pl b/examples/bench-factor.pl
index f88404a..07cac89 100755
--- a/examples/bench-factor.pl
+++ b/examples/bench-factor.pl
@@ -32,7 +32,7 @@ sub test_at_digits {
     my @mpxs = prime_factors($p);  push @mpxs, $p if $p < 2;
 
     verify_factor($p, \@mpxs, [factor($p)], "Math::Prime::Util $Math::Prime::Util::VERSION");
-}
+  }
 
 
   #my $min_num = min @nums;
diff --git a/factor.c b/factor.c
index 656389a..70192a5 100644
--- a/factor.c
+++ b/factor.c
@@ -50,7 +50,7 @@ int trial_factor(UV n, UV *factors, UV maxtrial)
     return nfactors;
   }
 
-  limit = sqrt((double) n);
+  limit = (UV) (sqrt(n)+0.1);
   if (limit > maxtrial)
     limit = maxtrial;
 
@@ -64,7 +64,7 @@ int trial_factor(UV n, UV *factors, UV maxtrial)
         factors[nfactors++] = f;
         n /= f;
       } while ( (n%f) == 0 );
-      newlimit = sqrt(n);
+      newlimit = (UV) (sqrt(n)+0.1);
       if (newlimit < limit)  limit = newlimit;
     }
     f += wheeladvance30[m];
@@ -371,7 +371,7 @@ int fermat_factor(UV n, UV *factors, UV rounds)
 
   MPUassert( (n >= 3) && ((n%2) != 0) , "bad n in fermat_factor");
 
-  sqn = sqrt((double) n);
+  sqn = (UV) (sqrt(n)+0.1);
   x = 2 * sqn + 1;
   y = 1;
   r = (sqn*sqn) - n;
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index 55619f9..330c4fc 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -368,9 +368,9 @@ C<~ 10^11>.  Memory use is proportional only to C<sqrt(a)>, with total
 memory use under 1MB for any base under C<10^14>.
 
 A later implementation may work on improving performance for values, both
-in reducing memory use (the current maximum is 140MB at 2^64) and improving
+in reducing memory use (the current maximum is 140MB at C<2^64>) and improving
 speed.  Possibilities include a hybrid table approach, using an explicit
-formula with C<li(x) or C<R(x)>, or one of the Meissel, Lehmer,
+formula with C<li(x)> or C<R(x)>, or one of the Meissel, Lehmer,
 or Lagarias-Miller-Odlyzko-Deleglise-Rivat methods.
 
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git



More information about the Pkg-perl-cvs-commits mailing list