[Haskell-beginners] ghci: inconsistent return values for succ

Rein Henrichs rein.henrichs at gmail.com
Sun Oct 18 18:11:10 UTC 2015


This has nothing to do with Parsec or Decimal. Two things are happening:

First, the type of the literal 3.14 is Fractional a => a. GHCi's extended
default rules [1] pick Double as the default for Fractional. (This is a
good choice.) Second, the closest number to 4.14 that is reresentable as a
Double is 4.1400000000000001. This is an inherent limitation of the
representation used. This effects every use of floating point numbers in
every programming language, and every programmer should understand this.
See [2] for more information.

For an exact result, you can provide a different type for your Fractional
literal that is capable of representing the result exactly:

> succ 3.14 :: Rational

207 % 50

> There must be some reason why Float and Double implement the Enum class,
> but I can't understand in what sense (mathematically) are real numbers
enumerable

You're absolutely right. It is, at best, a kludge and has no mathematical
justification.

[1]
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/interactive-evaluation.html#extended-default-rules
[2] https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html



>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151018/cc823fa2/attachment.html>


More information about the Beginners mailing list