diff --git a/src/util/mpq.cpp b/src/util/mpq.cpp index ddc227847..456508403 100644 --- a/src/util/mpq.cpp +++ b/src/util/mpq.cpp @@ -289,6 +289,25 @@ void mpq_manager::power(mpq const & a, unsigned p, mpq & b) { set(b, 1); return; } + if (eq(a, 1)) { + set(b, 1); + return; + } + if (eq(a, -1)) { + if (p % 2 == 0) + set(b, 1); + else + set(b, -1); + return; + } + if (eq(a, 0)) { + set(b, 0); + return; + } + + if (p > (1 << 20)) + throw default_exception("power is too large to compute"); + unsigned mask = 1; mpq power; set(power, a);