|
Packit |
785658 |
-- |
|
|
Packit |
785658 |
-- Module: Math.NumberTheory.Powers.Integer
|
|
Packit |
785658 |
-- Copyright: (c) 2011-2014 Daniel Fischer
|
|
Packit |
785658 |
-- Licence: MIT
|
|
Packit |
785658 |
-- Maintainer: Daniel Fischer <daniel.is.fischer@googlemail.com>
|
|
Packit |
785658 |
-- Stability: Provisional
|
|
Packit |
785658 |
-- Portability: Non-portable (GHC extensions)
|
|
Packit |
785658 |
--
|
|
Packit |
785658 |
-- Potentially faster power function for 'Integer' base and 'Int'
|
|
Packit |
785658 |
-- or 'Word' exponent.
|
|
Packit |
785658 |
--
|
|
Packit |
785658 |
{-# LANGUAGE CPP #-}
|
|
Packit |
785658 |
module Math.NumberTheory.Powers.Integer
|
|
Packit |
785658 |
{-# DEPRECATED "It is no faster than (^)" #-}
|
|
Packit |
785658 |
( integerPower
|
|
Packit |
785658 |
, integerWordPower
|
|
Packit |
785658 |
) where
|
|
Packit |
785658 |
|
|
Packit |
785658 |
#if !MIN_VERSION_base(4,8,0)
|
|
Packit |
785658 |
import Data.Word
|
|
Packit |
785658 |
#endif
|
|
Packit |
785658 |
|
|
Packit |
785658 |
-- | Power of an 'Integer' by the left-to-right repeated squaring algorithm.
|
|
Packit |
785658 |
-- This needs two multiplications in each step while the right-to-left
|
|
Packit |
785658 |
-- algorithm needs only one multiplication for 0-bits, but here the
|
|
Packit |
785658 |
-- two factors always have approximately the same size, which on average
|
|
Packit |
785658 |
-- gains a bit when the result is large.
|
|
Packit |
785658 |
--
|
|
Packit |
785658 |
-- For small results, it is unlikely to be any faster than '(^)', quite
|
|
Packit |
785658 |
-- possibly slower (though the difference shouldn't be large), and for
|
|
Packit |
785658 |
-- exponents with few bits set, the same holds. But for exponents with
|
|
Packit |
785658 |
-- many bits set, the speedup can be significant.
|
|
Packit |
785658 |
--
|
|
Packit |
785658 |
-- /Warning:/ No check for the negativity of the exponent is performed,
|
|
Packit |
785658 |
-- a negative exponent is interpreted as a large positive exponent.
|
|
Packit |
785658 |
integerPower :: Integer -> Int -> Integer
|
|
Packit |
785658 |
integerPower = (^)
|
|
Packit |
785658 |
{-# DEPRECATED integerPower "Use (^) instead" #-}
|
|
Packit |
785658 |
|
|
Packit |
785658 |
-- | Same as 'integerPower', but for exponents of type 'Word'.
|
|
Packit |
785658 |
integerWordPower :: Integer -> Word -> Integer
|
|
Packit |
785658 |
integerWordPower = (^)
|
|
Packit |
785658 |
{-# DEPRECATED integerWordPower "Use (^) instead" #-}
|