Skip to content

Fractional

Fractional is the class of all fractional number types. It's a subclass of Num. Instances include Float, Double, and Rational.

GHCi
>>> :info Fractional
type Fractional :: * -> Constraint
class Num a => Fractional a where
  (/) :: a -> a -> a
  recip :: a -> a
  fromRational :: Rational -> a
  {-# MINIMAL fromRational, (recip | (/)) #-}
    -- Defined in ‘GHC.Real’
[More omitted output]

Every fractional number type a needs to support fractional division ((/)), taking the reciprocal of a number (rescip), and conversion from a Rational number to a. Similar to the requirement that every Num type a supports conversion from Integral to a, the reason why every Fractional type a needs to support conversion from Rational to a is that Haskell parses number literals such as 1.0, 0.5 or 523.78 as Rational numbers. fromRational then allows us to convert these Rational numbers into any Fractional type a.