<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Data.Ratio</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ window.onload = function () {pageLoad();setSynopsis("mini_Data-Ratio.html");}; //]]> </script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">haskell2010-1.1.0.1: Compatibility with Haskell 2010</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Safe Haskell</th><td>Safe</td></tr></table><p class="caption">Data.Ratio</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Specification </a></li></ul></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">data</span> <a href="#t:Ratio">Ratio</a> a</li><li class="src short"><span class="keyword">type</span> <a href="#t:Rational">Rational</a> = <a href="Data-Ratio.html#t:Ratio">Ratio</a> <a href="Prelude.html#t:Integer">Integer</a></li><li class="src short"><a href="#v:-37-">(%)</a> :: <a href="Prelude.html#t:Integral">Integral</a> a => a -> a -> <a href="Data-Ratio.html#t:Ratio">Ratio</a> a</li><li class="src short"><a href="#v:numerator">numerator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a => <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -> a</li><li class="src short"><a href="#v:denominator">denominator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a => <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -> a</li><li class="src short"><a href="#v:approxRational">approxRational</a> :: <a href="Prelude.html#t:RealFrac">RealFrac</a> a => a -> a -> <a href="Data-Ratio.html#t:Rational">Rational</a></li></ul></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:Ratio" class="def">Ratio</a> a <a href="../base-4.5.1.0/src/GHC-Real.html#Ratio" class="link">Source</a></p><div class="doc"><p>Rational numbers, with numerator and denominator of some <code><a href="Prelude.html#t:Integral">Integral</a></code> type. </p></div><div class="subs instances"><p id="control.i:Ratio" class="caption collapser" onclick="toggleSection('i:Ratio')">Instances</p><div id="section.i:Ratio" class="show"><table><tr><td class="src"><a href="../base-4.5.1.0/Data-Typeable-Internal.html#t:Typeable1">Typeable1</a> <a href="Data-Ratio.html#t:Ratio">Ratio</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a => <a href="Prelude.html#t:Enum">Enum</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Eq">Eq</a> a => <a href="Prelude.html#t:Eq">Eq</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a => <a href="Prelude.html#t:Fractional">Fractional</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a => <a href="Prelude.html#t:Num">Num</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a => <a href="Prelude.html#t:Ord">Ord</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Prelude.html#t:Integral">Integral</a> a, <a href="Prelude.html#t:Read">Read</a> a) => <a href="Prelude.html#t:Read">Read</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a => <a href="Prelude.html#t:Real">Real</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Prelude.html#t:Integral">Integral</a> a => <a href="Prelude.html#t:RealFrac">RealFrac</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Prelude.html#t:Integral">Integral</a> a, <a href="Prelude.html#t:Show">Show</a> a) => <a href="Prelude.html#t:Show">Show</a> (<a href="Data-Ratio.html#t:Ratio">Ratio</a> a)</td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:Rational" class="def">Rational</a> = <a href="Data-Ratio.html#t:Ratio">Ratio</a> <a href="Prelude.html#t:Integer">Integer</a><a href="../base-4.5.1.0/src/GHC-Real.html#Rational" class="link">Source</a></p><div class="doc"><p>Arbitrary-precision rational numbers, represented as a ratio of two <code><a href="Prelude.html#t:Integer">Integer</a></code> values. A rational number may be constructed using the <code><a href="Data-Ratio.html#v:-37-">%</a></code> operator. </p></div></div><div class="top"><p class="src"><a name="v:-37-" class="def">(%)</a> :: <a href="Prelude.html#t:Integral">Integral</a> a => a -> a -> <a href="Data-Ratio.html#t:Ratio">Ratio</a> a<a href="../base-4.5.1.0/src/GHC-Real.html#%25" class="link">Source</a></p><div class="doc"><p>Forms the ratio of two integral numbers. </p></div></div><div class="top"><p class="src"><a name="v:numerator" class="def">numerator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a => <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -> a<a href="../base-4.5.1.0/src/GHC-Real.html#numerator" class="link">Source</a></p><div class="doc"><p>Extract the numerator of the ratio in reduced form: the numerator and denominator have no common factor and the denominator is positive. </p></div></div><div class="top"><p class="src"><a name="v:denominator" class="def">denominator</a> :: <a href="Prelude.html#t:Integral">Integral</a> a => <a href="Data-Ratio.html#t:Ratio">Ratio</a> a -> a<a href="../base-4.5.1.0/src/GHC-Real.html#denominator" class="link">Source</a></p><div class="doc"><p>Extract the denominator of the ratio in reduced form: the numerator and denominator have no common factor and the denominator is positive. </p></div></div><div class="top"><p class="src"><a name="v:approxRational" class="def">approxRational</a> :: <a href="Prelude.html#t:RealFrac">RealFrac</a> a => a -> a -> <a href="Data-Ratio.html#t:Rational">Rational</a><a href="../base-4.5.1.0/src/Data-Ratio.html#approxRational" class="link">Source</a></p><div class="doc"><p><code><a href="Data-Ratio.html#v:approxRational">approxRational</a></code>, applied to two real fractional numbers <code>x</code> and <code>epsilon</code>, returns the simplest rational number within <code>epsilon</code> of <code>x</code>. A rational number <code>y</code> is said to be <em>simpler</em> than another <code>y'</code> if </p><ul><li> <code><code><a href="Prelude.html#v:abs">abs</a></code> (<code><a href="Data-Ratio.html#v:numerator">numerator</a></code> y) <= <code><a href="Prelude.html#v:abs">abs</a></code> (<code><a href="Data-Ratio.html#v:numerator">numerator</a></code> y')</code>, and </li><li> <code><code><a href="Data-Ratio.html#v:denominator">denominator</a></code> y <= <code><a href="Data-Ratio.html#v:denominator">denominator</a></code> y'</code>. </li></ul><p>Any real interval contains a unique simplest rational; in particular, note that <code>0/1</code> is the simplest rational of all. </p></div></div><h1 id="g:1">Specification </h1><div class="doc"><pre> module Data.Ratio ( Ratio, Rational, (%), numerator, denominator, approxRational ) where infixl 7 % ratPrec = 7 :: Int data (Integral a) => Ratio a = !a :% !a deriving (Eq) type Rational = Ratio Integer (%) :: (Integral a) => a -> a -> Ratio a numerator, denominator :: (Integral a) => Ratio a -> a approxRational :: (RealFrac a) => a -> a -> Rational -- "reduce" is a subsidiary function used only in this module. -- It normalises a ratio by dividing both numerator -- and denominator by their greatest common divisor. -- -- E.g., 12 `reduce` 8 == 3 :% 2 -- 12 `reduce` (-8) == 3 :% (-2) reduce _ 0 = error "Data.Ratio.% : zero denominator" reduce x y = (x `quot` d) :% (y `quot` d) where d = gcd x y x % y = reduce (x * signum y) (abs y) numerator (x :% _) = x denominator (_ :% y) = y instance (Integral a) => Ord (Ratio a) where (x:%y) <= (x':%y') = x * y' <= x' * y (x:%y) < (x':%y') = x * y' < x' * y instance (Integral a) => Num (Ratio a) where (x:%y) + (x':%y') = reduce (x*y' + x'*y) (y*y') (x:%y) * (x':%y') = reduce (x * x') (y * y') negate (x:%y) = (-x) :% y abs (x:%y) = abs x :% y signum (x:%y) = signum x :% 1 fromInteger x = fromInteger x :% 1 instance (Integral a) => Real (Ratio a) where toRational (x:%y) = toInteger x :% toInteger y instance (Integral a) => Fractional (Ratio a) where (x:%y) / (x':%y') = (x*y') % (y*x') recip (x:%y) = y % x fromRational (x:%y) = fromInteger x :% fromInteger y instance (Integral a) => RealFrac (Ratio a) where properFraction (x:%y) = (fromIntegral q, r:%y) where (q,r) = quotRem x y instance (Integral a) => Enum (Ratio a) where succ x = x+1 pred x = x-1 toEnum = fromIntegral fromEnum = fromInteger . truncate -- May overflow enumFrom = numericEnumFrom -- These numericEnumXXX functions enumFromThen = numericEnumFromThen -- are as defined in Prelude.hs enumFromTo = numericEnumFromTo -- but not exported from it! enumFromThenTo = numericEnumFromThenTo instance (Read a, Integral a) => Read (Ratio a) where readsPrec p = readParen (p > ratPrec) (\r -> [(x%y,u) | (x,s) <- readsPrec (ratPrec+1) r, ("%",t) <- lex s, (y,u) <- readsPrec (ratPrec+1) t ]) instance (Integral a) => Show (Ratio a) where showsPrec p (x:%y) = showParen (p > ratPrec) showsPrec (ratPrec+1) x . showString " % " . showsPrec (ratPrec+1) y) approxRational x eps = simplest (x-eps) (x+eps) where simplest x y | y < x = simplest y x | x == y = xr | x > 0 = simplest' n d n' d' | y < 0 = - simplest' (-n') d' (-n) d | otherwise = 0 :% 1 where xr@(n:%d) = toRational x (n':%d') = toRational y simplest' n d n' d' -- assumes 0 < n%d < n'%d' | r == 0 = q :% 1 | q /= q' = (q+1) :% 1 | otherwise = (q*n''+d'') :% n'' where (q,r) = quotRem n d (q',r') = quotRem n' d' (n'':%d'') = simplest' d' r' d r </pre></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.11.0</p></div></body></html>