[Haskell-cafe] Problem with percentage function?

Brandon Allbery allbery.b at gmail.com
Sun Apr 20 21:30:17 UTC 2014


On Sun, Apr 20, 2014 at 5:12 PM, Chapman, Anthony Sergio <
a.s.chapman.10 at aberdeen.ac.uk> wrote:

> I would it to round to 2 decimal places no matter what the size of the
> input is
>
> ei toPerc 0.22222222222222  --- = 22.22
>     toPerc 0.22342222222222  --- = 22.34
>
> The problem is that what I actually get is 22.220000000000002
>

Looks like typical floating point behavior to me. The last place can't be
precisely controlled because you are printing in base 10 but the internal
format (as defined by the CPU) is base 2 and very few base 10 decimals have
exact base 2 representations.

This is not a bug (unless you consider the existence of floating point to
be a bug) and cannot be "fixed".

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140420/e615da36/attachment.html>


More information about the Haskell-Cafe mailing list