
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!

What do you consider to be the specification of logBase? Exact
floating-point numbers don't exist, so it has to return an
approximation. The standard doesn't give any guarantees as to the
precision of the approximation. Is a precision of 0.00000000000004 not
satisfactory for you?
2009/8/21 Roberto
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Web IR developer, market.yandex.ru

If 4.0 / 2.0 was 1.9999999999999999999998, it would be ok? The real value of log10 1000 is 3 (3.0). It can be represented with accuracy and it should be. You get the accuracy value in Perl, but there is the same problem in Python. It's a bit discouraging. Eugene Kirpichov wrote:
What do you consider to be the specification of logBase? Exact floating-point numbers don't exist, so it has to return an approximation. The standard doesn't give any guarantees as to the precision of the approximation. Is a precision of 0.00000000000004 not satisfactory for you?

2009/8/22 Roberto López
If 4.0 / 2.0 was 1.9999999999999999999998, it would be ok?
I think yes. However, hardware can afford to do computations "as accurately as possible", whereas software like Haskell Prelude can't.
The real value of log10 1000 is 3 (3.0). It can be represented with accuracy and it should be.
Doing so would not be of much use and would impose a performance penalty required to achieve this accuracy. I looked into the Prelude source and saw that logBase is implemented like x`logBase`y = log x / log y. I think it is quite a sensible definition, and there's no point in modifying it just to make accuracy even better than 0.000000000000000004 in certain cases where it is possible. To my mind, an accuracy of "the representable number closest to the exact answer" is only required in very specific circumstances, for example when you are converting a string to a float. If you have a good reason to need exact real numbers, you can use a corresponding package for exact reals. However, if you propose an equivalently fast definition of logBase that achieves better accuracy, I am sure it will be much welcomed.
You get the accuracy value in Perl, but there is the same problem in Python. It's a bit discouraging.
Eugene Kirpichov wrote:
What do you consider to be the specification of logBase? Exact floating-point numbers don't exist, so it has to return an approximation. The standard doesn't give any guarantees as to the precision of the approximation. Is a precision of 0.00000000000004 not satisfactory for you?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Web IR developer, market.yandex.ru

Am Samstag 22 August 2009 11:34:51 schrieb Eugene Kirpichov:
2009/8/22 Roberto López
: If 4.0 / 2.0 was 1.9999999999999999999998, it would be ok?
I think yes.
I don't. Not for (+), (-), (*), (/). There I want "the representable number closest to the exact answer".
However, hardware can afford to do computations "as accurately as possible", whereas software like Haskell Prelude can't.
The real value of log10 1000 is 3 (3.0). It can be represented with accuracy and it should be.
Doing so would not be of much use and would impose a performance penalty required to achieve this accuracy.
+1 It is so rare that such functions have exactly representable results that it's absolutely not worth to check. Hence it is the programmer's obligation to test and adjust if it's moderately likely that such a case arises and it's important.

Even in Perl you've got MigMit:~ MigMit$ perl -e 'printf("%.20f\n",log(125)/log(5))' 3.00000000000000044409 On 22 Aug 2009, at 13:24, Roberto López wrote:
If 4.0 / 2.0 was 1.9999999999999999999998, it would be ok?
The real value of log10 1000 is 3 (3.0). It can be represented with accuracy and it should be.
You get the accuracy value in Perl, but there is the same problem in Python. It's a bit discouraging.
Eugene Kirpichov wrote:
What do you consider to be the specification of logBase? Exact floating-point numbers don't exist, so it has to return an approximation. The standard doesn't give any guarantees as to the precision of the approximation. Is a precision of 0.00000000000004 not satisfactory for you?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

2009/8/22 Roberto López
If 4.0 / 2.0 was 1.9999999999999999999998, it would be ok?
The real value of log10 1000 is 3 (3.0). It can be represented with accuracy and it should be.
Well, it already can be, you just need to choose your representation properly:
logBase 10 1000 :: Double 2.9999999999999996 logBase 10 1000 :: Float 3.0
Welcome the wonderful land of floating point numbers. (The examples above are from an Intel 32-bit machine, I suspect it'd change on any other type of architecture.) /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe

2009/8/22 Roberto López
You get the accuracy value in Perl, but there is the same problem in Python. It's a bit discouraging.
You don't get an accurate answer with Perl. It just lies to you to keep you happy in your ignorance. $ perl -e 'printf "%.22f\n", log(1000)/log(10);' 2.9999999999999995559108

he who compares floating point numbers for equality is in a state of sin. mark On 22/08/2009, at 5:00 AM, 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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I always thought that he who compares floating point numbers for
equality was acting in tangent of reason...
-- Jeff
On Sat, Aug 22, 2009 at 4:02 AM, Mark Wotton
he who compares floating point numbers for equality is in a state of sin.
mark
On 22/08/2009, at 5:00 AM, 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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (10)
-
Bryan O'Sullivan
-
Dan Weston
-
Daniel Fischer
-
Eugene Kirpichov
-
Jeff Heard
-
Magnus Therning
-
Mark Wotton
-
Miguel Mitrofanov
-
Roberto
-
Roberto López