[Haskell-cafe] Re: Is logBase right?
Steve
stevech1097 at yahoo.com.au
Sun Aug 23 09:27:48 EDT 2009
On Sun, 2009-08-23 at 15:12 +0400, Eugene Kirpichov wrote:
> > There is *not* the same problem in Python:
> > $ python
> > Python 2.6.2 (r262:71600, Jul 9 2009, 23:16:53)
> > [GCC 4.4.0 20090506 (Red Hat 4.4.0-4)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> import math
> >>>> math.log10(1000)
> > 3.0
> >
>
> >>> import math
> >>> math.log(1000,10)
> 2.9999999999999996
That is surprising. I think its a Python bug - the results should be
consistent.
>
> > Recent work in Python 3 (and Python 2.6) has improved the handling of
> > floating point numbers, and addresses exactly the problem that Roberto
> > has raised.
> >
> > I see no reason why Haskell could not improve its handling of floating
> > point numbers by using similar techniques.
>
> You mean introducing a "log10" function into the definition of the
> Floating class ? That might be a proposal for Haskell Prime.
I was not thinking of the log10 function. I was thinking of the changes
mentioned in
http://docs.python.org/3.1/whatsnew/3.1.html
where it says
"Python now uses David Gay’s algorithm for finding the shortest floating
point representation that doesn’t change its value. This should help
mitigate some of the confusion surrounding binary floating point
numbers."
Also, I had a problem using floating point in Python where
>>> round(697.04157958254996, 10)
gave
697.04157958259998
which is closer to
697.0415795826
than the desired result of
697.0415795825
Its been fixed in the latest versions of Python:
>>> round(697.04157958254996, 10)
697.0415795825
I'm not sure what the equivalent in Haskell is. Is there a function for
rounding to a number of decimal digits ?
I came up with this:
roundN :: Double -> Int -> Double
roundN n ndigits = fromIntegral (round $ n * m) / m
where m = 10 ^ ndigits
ghci> roundN 697.04157958254996 10
697.0415795826
which is not the desired result.
Steve
More information about the Haskell-Cafe
mailing list