HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebase-4.20.2.0Haskell2010

GHC.Float

The types Float and Double, the classes Floating and RealFloat and casting between Word32 and Float and Word64 and Double.

  • 5 types
  • 2 classes
  • 123 values
  • Packagebase-4.20.2.0
  • Exports130
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceFloat.hs

Classes

2 declarations
classclass Fractional a => Floating a where
#

Trigonometric and hyperbolic functions and related functions.

The Haskell Report defines no laws for Floating. However, (+), (*) and exp are customarily expected to define an exponential field and have the following properties:

  • exp (a + b) = exp a * exp b

  • exp (fromInteger 0) = fromInteger 1

Methods

  • pi :: a
  • exp :: a -> a
  • log :: a -> a
  • sqrt :: a -> a
  • (**) :: a -> a -> ainfixr 8
  • logBase :: a -> a -> a
  • sin :: a -> a
  • cos :: a -> a
  • tan :: a -> a
  • asin :: a -> a
  • acos :: a -> a
  • atan :: a -> a
  • sinh :: a -> a
  • cosh :: a -> a
  • tanh :: a -> a
  • asinh :: a -> a
  • acosh :: a -> a
  • atanh :: a -> a
  • log1p :: a -> a

    log1p x computes log (1 + x), but provides more precise results for small (absolute) values of x if possible.

  • expm1 :: a -> a

    expm1 x computes exp x - 1, but provides more precise results for small (absolute) values of x if possible.

  • log1pexp :: a -> a

    log1pexp x computes log (1 + exp x), but provides more precise results if possible.

    Examples:

    • if x is a large negative number, log (1 + exp x) will be imprecise for the reasons given in log1p.

    • if exp x is close to -1, log (1 + exp x) will be imprecise for the reasons given in expm1.

  • log1mexp :: a -> a

    log1mexp x computes log (1 - exp x), but provides more precise results if possible.

    Examples:

    • if x is a large negative number, log (1 - exp x) will be imprecise for the reasons given in log1p.

    • if exp x is close to 1, log (1 - exp x) will be imprecise for the reasons given in expm1.

Instances10Floating, …
classclass (RealFrac a, Floating a) => RealFloat a where
#

Efficient, machine-independent access to the components of a floating-point number.

Methods

  • floatRadix :: a -> Integer

    a constant function, returning the radix of the representation (often 2)

  • floatDigits :: a -> Int

    a constant function, returning the number of digits of floatRadix in the significand

  • floatRange :: a -> (Int, Int)

    a constant function, returning the lowest and highest values the exponent may assume

  • decodeFloat :: a -> (Integer, Int)

    The function decodeFloat applied to a real floating-point number returns the significand expressed as an Integer and an appropriately scaled exponent (an Int). If decodeFloat x yields (m,n), then x is equal in value to m*b^^n, where b is the floating-point radix, and furthermore, either m and n are both zero or else b^(d-1) <= abs m < b^d, where d is the value of floatDigits x. In particular, decodeFloat 0 = (0,0). If the type contains a negative zero, also decodeFloat (-0.0) = (0,0). The result of decodeFloat x is unspecified if either of isNaN x or isInfinite x is True.

  • encodeFloat :: Integer -> Int -> a

    encodeFloat performs the inverse of decodeFloat in the sense that for finite x with the exception of -0.0, Prelude.uncurry encodeFloat (decodeFloat x) = x. encodeFloat m n is one of the two closest representable floating-point numbers to m*b^^n (or ±Infinity if overflow occurs); usually the closer, but if m contains too many bits, the result may be rounded in the wrong direction.

  • exponent :: a -> Int

    exponent corresponds to the second component of decodeFloat. exponent 0 = 0 and for finite nonzero x, exponent x = snd (decodeFloat x) + floatDigits x. If x is a finite floating-point number, it is equal in value to significand x * b ^^ exponent x, where b is the floating-point radix. The behaviour is unspecified on infinite or NaN values.

  • significand :: a -> a

    The first component of decodeFloat, scaled to lie in the open interval (-1,1), either 0.0 or of absolute value >= 1/b, where b is the floating-point radix. The behaviour is unspecified on infinite or NaN values.

  • scaleFloat :: Int -> a -> a

    multiplies a floating-point number by an integer power of the radix

  • isNaN :: a -> Bool

    True if the argument is an IEEE "not-a-number" (NaN) value

  • isInfinite :: a -> Bool

    True if the argument is an IEEE infinity or negative infinity

  • isDenormalized :: a -> Bool

    True if the argument is too small to be represented in normalized format

  • isNegativeZero :: a -> Bool

    True if the argument is an IEEE negative zero

  • isIEEE :: a -> Bool

    True if the argument is an IEEE floating point number

  • atan2 :: a -> a -> a

    a version of arctangent taking two real floating-point arguments. For real floating x and y, atan2 y x computes the angle (from the positive x-axis) of the vector from the origin to the point (x,y). atan2 y x returns a value in the range [-pi, pi]. It follows the Common Lisp semantics for the origin when signed zeroes are supported. atan2 y 1, with y in a type that is RealFloat, should return the same value as atan y. A default definition of atan2 is provided, but implementors can provide a more accurate implementation.

Instances8RealFloat, …

Float

2 declarations
datadata Float
#

Single-precision floating point numbers. It is desirable that this type be at least equal in range and precision to the IEEE single-precision type.

Constructors

Instances25Enum, Floating, Fractional, Data, Num, Read, …
  • Enum FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    fromEnum just truncates its argument, beware of all sorts of overflows.

    List generators have extremely peculiar behavior, mandated by Haskell Report 2010:

    Example1 expression
    [0..1.5 :: Float][0.0,1.0,2.0]
  • Eq FloatDefined in ghc-prim-0.12.0 · GHC.Classes

    Note that due to the presence of NaN, Float's Eq instance does not satisfy reflexivity.

    Example1 expression
    0/0 == (0/0 :: Float)False

    Also note that Float's Eq instance does not satisfy extensionality:

    Example2 expressions
    0 == (-0 :: Float)Truerecip 0 == recip (-0 :: Float)False
  • Floating FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float
  • Fractional FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero.

    Example4 expressions
    0 == (-0 :: Float)Truerecip 0 == recip (-0 :: Float)Falsemap (/ 0) [-1, 0, 1 :: Float][-Infinity,NaN,Infinity]map (* 0) $ map (/ 0) [-1, 0, 1 :: Float][NaN,NaN,NaN]
  • Data FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero. Neither addition nor multiplication are associative or distributive:

    Example3 expressions
    (0.1 + 0.1 :: Float) + 0.5 == 0.1 + (0.1 + 0.5)False(0.1 + 0.2 :: Float) * 0.9 == 0.1 * 0.9 + 0.2 * 0.9False(0.1 * 0.1 :: Float) * 0.9 == 0.1 * (0.1 * 0.9)False
  • Ord FloatDefined in ghc-prim-0.12.0 · GHC.Classes

    See instance Ord Double for discussion of deviations from IEEE 754 standard.

  • Read FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    Beware that toRational generates garbage for non-finite arguments:

    Example2 expressions
    toRational (1/0 :: Float)340282366920938463463374607431768211456 % 1toRational (0/0 :: Float)510423550381407695195061911147652317184 % 1
  • RealFloat FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float
  • RealFrac FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    Beware that results for non-finite arguments are garbage:

    Example2 expressions
    [ f x | f <- [round, floor, ceiling], x <- [-1/0, 0/0, 1/0 :: Float] ] :: [Int][0,0,0,0,0,0,0,0,0]map properFraction [-1/0, 0/0, 1/0] :: [(Int, Float)][(0,0.0),(0,0.0),(0,0.0)]

    and get even more non-sensical if you ask for Integer instead of Int.

  • Show FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan
  • Storable FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg FloatDefined in base-4.20.2.0 · Text.Printf
  • Generic1 (URec Float)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Foldable UFloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Foldable
  • Traversable UFloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Traversable
  • Functor (URec Float)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Eq (URec Float p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Ord (URec Float p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Show (URec Float p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Generic (URec Float p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • type Rep (URec Float p) = D1 ('MetaData "URec" "GHC.Internal.Generics" "ghc-internal" 'False) (C1 ('MetaCons "UFloat" 'PrefixI 'True) (S1 ('MetaSel ('Just "uFloat#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) UFloat))Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • type Rep1 (URec Float) = D1 ('MetaData "URec" "GHC.Internal.Generics" "ghc-internal" 'False) (C1 ('MetaCons "UFloat" 'PrefixI 'True) (S1 ('MetaSel ('Just "uFloat#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) UFloat))Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • data URec FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics

    Used for marking occurrences of Float#

Conversion

Operations

Predicate

Comparison

Arithmetic

Double

2 declarations
datadata Double
#

Double-precision floating point numbers. It is desirable that this type be at least equal in range and precision to the IEEE double-precision type.

Constructors

Instances25Enum, Floating, Fractional, Data, Num, Read, …
  • Enum DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    fromEnum just truncates its argument, beware of all sorts of overflows.

    List generators have extremely peculiar behavior, mandated by Haskell Report 2010:

    Example1 expression
    [0..1.5][0.0,1.0,2.0]
  • Eq DoubleDefined in ghc-prim-0.12.0 · GHC.Classes

    Note that due to the presence of NaN, Double's Eq instance does not satisfy reflexivity.

    Example1 expression
    0/0 == (0/0 :: Double)False

    Also note that Double's Eq instance does not satisfy substitutivity:

    Example2 expressions
    0 == (-0 :: Double)Truerecip 0 == recip (-0 :: Double)False
  • Floating DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float
  • Fractional DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero.

    Example4 expressions
    0 == (-0 :: Double)Truerecip 0 == recip (-0 :: Double)Falsemap (/ 0) [-1, 0, 1][-Infinity,NaN,Infinity]map (* 0) $ map (/ 0) [-1, 0, 1][NaN,NaN,NaN]
  • Data DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero. Neither addition nor multiplication are associative or distributive:

    Example3 expressions
    (0.1 + 0.1) + 0.4 == 0.1 + (0.1 + 0.4)False(0.1 + 0.2) * 0.3 == 0.1 * 0.3 + 0.2 * 0.3False(0.1 * 0.1) * 0.3 == 0.1 * (0.1 * 0.3)False
  • Ord DoubleDefined in ghc-prim-0.12.0 · GHC.Classes

    IEEE 754 Double-precision type includes not only numbers, but also positive and negative infinities and a special element called NaN (which can be quiet or signal).

    IEEE 754-2008, section 5.11 requires that if at least one of arguments of <=, <, >, >= is NaN then the result of the comparison is False, and instance Ord Double complies with this requirement. This violates the reflexivity: both NaN <= NaN and NaN >= NaN are False.

    IEEE 754-2008, section 5.10 defines totalOrder predicate. Unfortunately, compare on Doubles violates the IEEE standard and does not define a total order. More specifically, both compare NaN x and compare x NaN always return GT.

    Thus, users must be extremely cautious when using instance Ord Double. For instance, one should avoid ordered containers with keys represented by Double, because data loss and corruption may happen. An IEEE-compliant compare is available in fp-ieee package as TotallyOrdered newtype.

    Moving further, the behaviour of min and max with regards to NaN is also non-compliant. IEEE 754-2008, section 5.3.1 defines that quiet NaN should be treated as a missing data by minNum and maxNum functions, for example, minNum(NaN, 1) = minNum(1, NaN) = 1. Some languages such as Java deviate from the standard implementing minNum(NaN, 1) = minNum(1, NaN) = NaN. However, min / max in base are even worse: min NaN 1 is 1, but min 1 NaN is NaN.

    IEEE 754-2008 compliant min / max can be found in ieee754 package under minNum / maxNum names. Implementations compliant with minimumNumber / maximumNumber from a newer IEEE 754-2019, section 9.6 are available from fp-ieee package.

  • Read DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    Beware that toRational generates garbage for non-finite arguments:

    Example2 expressions
    toRational (1/0)179769313 (and 300 more digits...) % 1toRational (0/0)269653970 (and 300 more digits...) % 1
  • RealFloat DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float
  • RealFrac DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    Beware that results for non-finite arguments are garbage:

    Example2 expressions
    [ f x | f <- [round, floor, ceiling], x <- [-1/0, 0/0, 1/0] ] :: [Int][0,0,0,0,0,0,0,0,0]map properFraction [-1/0, 0/0, 1/0] :: [(Int, Double)][(0,0.0),(0,0.0),(0,0.0)]

    and get even more non-sensical if you ask for Integer instead of Int.

  • Show DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan
  • Storable DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg DoubleDefined in base-4.20.2.0 · Text.Printf
  • Generic1 (URec Double)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Foldable UDoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Foldable
  • Traversable UDoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Traversable
  • Functor (URec Double)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Eq (URec Double p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Ord (URec Double p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Show (URec Double p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Generic (URec Double p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • type Rep (URec Double p) = D1 ('MetaData "URec" "GHC.Internal.Generics" "ghc-internal" 'False) (C1 ('MetaCons "UDouble" 'PrefixI 'True) (S1 ('MetaSel ('Just "uDouble#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) UDouble))Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • type Rep1 (URec Double) = D1 ('MetaData "URec" "GHC.Internal.Generics" "ghc-internal" 'False) (C1 ('MetaCons "UDouble" 'PrefixI 'True) (S1 ('MetaSel ('Just "uDouble#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) UDouble))Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • data URec DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics

    Used for marking occurrences of Double#

Conversion

Operations

Predicate

Comparison

Arithmetic

Formatting

5 declarations
valueshowFloat :: RealFloat a => a -> ShowS
#

Show a signed RealFloat value to full precision using standard decimal notation for arguments whose absolute value lies between 0.1 and 9,999,999, and scientific notation otherwise.

Operations

7 declarations
valuelog1mexpOrd :: (Ord a, Floating a) => a -> a
#

Default implementation for log1mexp requiring Ord to test against a threshold to decide which implementation variant to use.

valuefloatToDigits :: RealFloat a => Integer -> a -> ([Int], Int)
#

floatToDigits takes a base and a non-negative RealFloat number, and returns a list of digits and an exponent. In particular, if x>=0, and

floatToDigits base x = ([d1,d2,...,dn], e)

then

  1. n >= 1
  2. x = 0.d1d2...dn * (base**e)
  3. 0 <= di <= base-1

Converts a positive integer to a floating-point value.

The value nearest to the argument will be returned. If there are two such values, the one with an even significand will be returned (i.e. IEEE roundTiesToEven).

The argument must be strictly positive, and floatRadix (undefined :: a) must be 2.

Monomorphic equality operators

2 declarations

See GHC.Classes#matching_overloaded_methods_in_rules

Internal

14 declarations

These may vanish in a future release

valueclamp :: Int -> Int -> Int
#

Used to prevent exponent over/underflow when encoding floating point numbers. This is also the same as

\(x,y) -> max (-x) (min x y)
Example
Example1 expression
clamp (-10) 510