[Haskell-cafe] Is logBase right?

Dan Weston westondan at imageworks.com
Mon Aug 24 17:31:28 EDT 2009


I don't know if anyone actually answered the question you didn't ask, 
but you can always improve an inaccurate guess when you need to. A limit 
will always exist, and should be unique (independent of the initial 
guess), assuming (+) and (*) are well-conditioned.

In practice, a single first-order Taylor step should be enough:

logBase' :: Double -> Double -> Double
logBase' b y     = if b == 0.0 then 1.0 else improve x0
    where bLogInv = 1.0 / log(b)
          f     x = x + (1.0-b**x/y) * bLogInv

          -- First step is enough, if we guess smartly
          improve = f
          x0      = log(y) * bLogInv

          -- or use the limit from any initial guess
          -- improve x = let y = f x in if y == x then y else improve y
          -- x0      = 0.0

Dan

Roberto wrote:
> Hi, 
> 
> There is a mistake is logBase:
> 
> $ ghci
> GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
> Loading package ghc-prim ... linking ... done.
> Loading package integer ... linking ... done.
> Loading package base ... linking ... done.
> Prelude> logBase 10 10
> 1.0
> Prelude> logBase 10 100
> 2.0
> Prelude> logBase 10 1000
> 2.9999999999999996                  <--- eeeerrgghhhh!
> Prelude> logBase 10 10000
> 4.0
> 
> 
> My host is a Debian GNU/Linux 5.0.2 (lenny) with the following GHC packages:
> 
> ii  ghc6                                 6.10.4-1                       
> ii  ghc6-doc                             6.10.4-1                      
> ii  libghc6-mtl-dev                      1.1.0.2-7+b1                  
> ii  libghc6-utf8-string-dev              0.3.5-1+b1                    
> ii  libghc6-x11-dev                      1.4.5-6                       
> rc  libghc6-x11-doc                      1.4.2-1                       
> ii  libghc6-x11-xft-dev                  0.3-3+b3                      
> ii  libghc6-xmonad-contrib-dev           0.8.1-3+b3                    
> rc  libghc6-xmonad-contrib-doc           0.8-2                         
> ii  libghc6-xmonad-dev                   0.8.1-5                       
> 
> Regards!
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 



More information about the Haskell-Cafe mailing list