~ubuntu-branches/ubuntu/vivid/regina-normal/vivid

« back to all changes in this revision

Viewing changes to engine/maths/ninteger.cpp

  • Committer: Package Import Robot
  • Author(s): Ben Burton
  • Date: 2013-11-02 11:44:32 UTC
  • mfrom: (1.2.8)
  • Revision ID: package-import@ubuntu.com-20131102114432-acgci6b1pb2hjl8q
Tags: 4.95-1
* New upstream release.
* The python module is now installed in a standard location beneath
  /usr/lib/python2.7/dist-packages.
* Switched python packaging from python-support to dh_python2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <cstdlib>
37
37
#include "maths/ninteger.h"
38
38
#include "maths/numbertheory.h"
39
 
#include "utilities/intutils.h"
40
39
#include "utilities/nthread.h"
41
40
 
42
41
// We instantiate both variants of the NIntegerBase template at the bottom
311
310
            mpz_init_set_si(large_, small_);
312
311
            mpz_mul_si(large_, large_, other.small_);
313
312
        } else
314
 
            small_ = ans;
 
313
            small_ = static_cast<long>(ans);
315
314
    }
316
315
    return *this;
317
316
}
331
330
            mpz_init_set_si(large_, small_);
332
331
            mpz_mul_si(large_, large_, other);
333
332
        } else
334
 
            small_ = ans;
 
333
            small_ = static_cast<long>(ans);
335
334
    }
336
335
    return *this;
337
336
}
539
538
        // (ii) |other| == |this|, in which case the result is 0;
540
539
        // (iii) |other| < |this|, in which case we can convert
541
540
        // everything to native C/C++ integer arithmetic.
 
541
 
 
542
        // Test other <=> |this|:
542
543
        int res = (small_ >= 0 ?
543
544
            mpz_cmp_si(other.large_, small_) :
544
545
            mpz_cmp_ui(other.large_, - small_) /* ui cast makes this work
550
551
            return *this;
551
552
        }
552
553
 
 
554
        // Test other <=> -|this|:
553
555
        res = (small_ >= 0 ?
554
556
            mpz_cmp_si(other.large_, - small_) :
555
 
            mpz_cmp_ui(other.large_, small_));
 
557
            mpz_cmp_si(other.large_, small_));
556
558
 
557
559
        if (res < 0)
558
560
            return *this;
564
566
        // Everything can be made native integer arithmetic.
565
567
        // Opportunistically reduce other while we're at it.
566
568
        const_cast<NIntegerBase<supportInfinity>&>(other).forceReduce();
567
 
        small_ %= other.small_;
 
569
        // Some compilers will crash on LONG_MIN % -1, sigh.
 
570
        if (other.small_ == -1)
 
571
            small_ = 0;
 
572
        else
 
573
            small_ %= other.small_;
568
574
        return *this;
569
575
    } else
570
576
        return (*this) %= other.small_;
635
641
        long a = small_;
636
642
        long b = other.small_;
637
643
 
 
644
        if ((a == LONG_MIN && (b == LONG_MIN || b == 0)) ||
 
645
                (b == LONG_MIN && a == 0)) {
 
646
            // gcd(a,b) = LONG_MIN, which means we can't make it
 
647
            // non-negative without switching to large integers.
 
648
            large_ = new mpz_t;
 
649
            mpz_init_set_si(large_, LONG_MIN);
 
650
            mpz_neg(large_, large_);
 
651
            return;
 
652
        }
638
653
        if (a == LONG_MIN) {
639
 
            if (b == LONG_MIN) {
640
 
                // gcd(a,b) = LONG_MIN, which means we can't make it
641
 
                // non-negative without switching to large integers.
642
 
                large_ = new mpz_t;
643
 
                mpz_init_set_si(large_, LONG_MIN);
644
 
                mpz_neg(large_, large_);
645
 
                return;
646
 
            }
647
654
            a >>= 1; // Won't affect the gcd, but allows us to negate.
648
655
        } else if (b == LONG_MIN) {
649
656
            b >>= 1; // Won't affect the gcd, but allows us to negate.