Skip to content

Commit 7816c14

Browse files
authored
Merge pull request #221 from axman6/fast-exponentiation
Make exponentiation significantly faster
2 parents d658b88 + 0a34059 commit 7816c14

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Statistics/Sample.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE FlexibleContexts #-}
2+
{-# LANGUAGE BangPatterns #-}
23
-- |
34
-- Module : Statistics.Sample
45
-- Copyright : (c) 2008 Don Stewart, 2009 Bryan O'Sullivan
@@ -452,8 +453,9 @@ pair va vb
452453

453454
-- (^) operator from Prelude is just slow.
454455
(^) :: Double -> Int -> Double
455-
x ^ 1 = x
456-
x ^ n = x * (x ^ (n-1))
456+
x0 ^ n0 = go (n0-1) x0 where
457+
go 0 !acc = acc
458+
go n acc = go (n-1) (acc*x0)
457459
{-# INLINE (^) #-}
458460

459461
-- don't support polymorphism, as we can't get unboxed returns if we use it.

0 commit comments

Comments
 (0)