lemon/random.h
changeset 499 b1ef32ab39f3
parent 491 879c55700cd4
child 517 2b6d5d22bb23
     1.1 --- a/lemon/random.h	Fri Feb 20 18:06:10 2009 +0000
     1.2 +++ b/lemon/random.h	Fri Feb 20 20:43:13 2009 +0000
     1.3 @@ -344,56 +344,46 @@
     1.4        }
     1.5      };
     1.6  
     1.7 -    template <typename Result, int exp, bool pos = (exp >= 0)>
     1.8 +    template <typename Result, int exp>
     1.9      struct ShiftMultiplier {
    1.10        static const Result multiplier() {
    1.11          Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
    1.12          res *= res;
    1.13 -        if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
    1.14 -        return res;
    1.15 -      }
    1.16 -    };
    1.17 -
    1.18 -    template <typename Result, int exp>
    1.19 -    struct ShiftMultiplier<Result, exp, false> {
    1.20 -      static const Result multiplier() {
    1.21 -        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
    1.22 -        res *= res;
    1.23          if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
    1.24          return res;
    1.25        }
    1.26      };
    1.27  
    1.28      template <typename Result>
    1.29 -    struct ShiftMultiplier<Result, 0, true> {
    1.30 +    struct ShiftMultiplier<Result, 0> {
    1.31        static const Result multiplier() {
    1.32          return static_cast<Result>(1.0);
    1.33        }
    1.34      };
    1.35  
    1.36      template <typename Result>
    1.37 -    struct ShiftMultiplier<Result, -20, true> {
    1.38 +    struct ShiftMultiplier<Result, 20> {
    1.39        static const Result multiplier() {
    1.40          return static_cast<Result>(1.0/1048576.0);
    1.41        }
    1.42      };
    1.43  
    1.44      template <typename Result>
    1.45 -    struct ShiftMultiplier<Result, -32, true> {
    1.46 +    struct ShiftMultiplier<Result, 32> {
    1.47        static const Result multiplier() {
    1.48 -        return static_cast<Result>(1.0/424967296.0);
    1.49 +        return static_cast<Result>(1.0/4294967296.0);
    1.50        }
    1.51      };
    1.52  
    1.53      template <typename Result>
    1.54 -    struct ShiftMultiplier<Result, -53, true> {
    1.55 +    struct ShiftMultiplier<Result, 53> {
    1.56        static const Result multiplier() {
    1.57          return static_cast<Result>(1.0/9007199254740992.0);
    1.58        }
    1.59      };
    1.60  
    1.61      template <typename Result>
    1.62 -    struct ShiftMultiplier<Result, -64, true> {
    1.63 +    struct ShiftMultiplier<Result, 64> {
    1.64        static const Result multiplier() {
    1.65          return static_cast<Result>(1.0/18446744073709551616.0);
    1.66        }
    1.67 @@ -413,7 +403,7 @@
    1.68        static const int bits = std::numeric_limits<Word>::digits;
    1.69  
    1.70        static Result convert(RandomCore<Word>& rnd) {
    1.71 -        return Shifting<Result, - shift - rest>::
    1.72 +        return Shifting<Result, shift + rest>::
    1.73            shift(static_cast<Result>(rnd() >> (bits - rest)));
    1.74        }
    1.75      };
    1.76 @@ -423,7 +413,7 @@
    1.77        static const int bits = std::numeric_limits<Word>::digits;
    1.78  
    1.79        static Result convert(RandomCore<Word>& rnd) {
    1.80 -        return Shifting<Result, - shift - bits>::
    1.81 +        return Shifting<Result, shift + bits>::
    1.82            shift(static_cast<Result>(rnd())) +
    1.83            RealConversion<Result, Word, rest-bits, shift + bits>::
    1.84            convert(rnd);