enternetusers > References > Haskell reference
| Indexes | Syntax | Prelude | >> Ratio << | Complex | Numeric | Ix | Array | List | Maybe | Char | Monad | IO | Directory | System | Time | Locale | CPUTime | Random

Ratio

For each Integral type t, there is a type Ratio t of rational pairs with components of type t. The type name Rational is a synonym for Ratio Integer.

Ratio is an instance of classes Eq, Ord, Num, Real, Fractional, RealFrac, Enum, Read, and Show. In each case, the instance for Ratio t simply "lifts" the corresponding operations over t. If t is a bounded type, the results may be unpredictable; for example Ratio Int may give rise to integer overflow even for rational numbers of small absolute size.

The operator (%) forms the ratio of two integral numbers, reducing the fraction to terms with no common factor and such that the denominator is positive. The functions numerator and denominator extract the components of a ratio; these are in reduced form with a positive denominator. Ratio is an abstract type. For example, 12 % 8 is reduced to 3/2 and 12 % (-8) is reduced to (-3)/2.

The approxRational function, applied to two real fractional numbers x and epsilon, returns the simplest rational number within the open interval (x-epsilon, x+epsilon). A rational number n/d in reduced form is said to be simpler than another n'/d' if |n| <=|n'| and d <=d'. Note that it can be proved that any real interval contains a unique simplest rational.


-- Standard functions on rational numbers

module  Ratio (
    Ratio, Rational, (%), numerator, denominator, approxRational ) where

infixl 7  %

prec = 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 "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)        =  if x < 0 then (-y) :% (-x) else 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
    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 > prec)
                              (\r -> [(x%y,u) | (x,s)   <- reads r,
                                                ("%",t) <- lex s,
                                                (y,u)   <- reads t ])

instance  (Integral a)  => Show (Ratio a)  where
    showsPrec p (x:%y)  =  showParen (p > prec)
                               (shows x . showString " % " . shows 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

Partner sites:

Board Shorts >>


Quiksilver Board Shorts
Roxy Board Shorts
Volcom Board Shorts
Billabong Board Shorts
Quicksilver Board Shorts
Hurley Board Shorts
Quicksilveredition Board Shorts
Rusty Board Shorts
Reef Board Shorts
oneill Board Shorts

Ezekiel surf clothing Board Shorts
Paul Frank surf clothing Board Shorts
Mada surf clothing Board Shorts
Analog surf clothing Board Shorts
Quicksilver surf clothing Board Shorts
Jet Pilot surf clothing Board Shorts
Quicksilveredition surf clothing Board Shorts
Dakine surf clothing Board Shorts
Matix surf clothing Board Shorts
Reef surf clothing Board Shorts
O'neil surf clothing Board Shorts

Also, you will want to check out Stanton California so you can see what's up and they are part of Stanton City Hall as well.

You can also get Organic Skin Care products from Bliss Bath Body and you must check out their Natural Body Lotions and bath soaps

Now if you are looking for the best deals on surf clothing from Quiksilver and Roxy then you have to check these amazing deals here: quiksilver tees http://www.killerdana.com/killerdana/search2.asp?SearchResults.SearchHandle=QT1NZW5zIFNTIFRlZXN_Qj1NZW5zIFNTIFRlZXN_RD01OX5FPTBeMV4yXm51bV9uZXd_ST1QcmljZX5LPTR_TD0xfk09NH5OPTN_&SearchResults.SortAscending=False&SearchResults.SortBy=num_new&Answer=766

quiksilver shirt Quiksilver Shirt

quiksilver board short quiksilver board short

quiksilver clothing Quiksilver clothing

quiksilver apparel Quiksilver apparel

quiksilver jackets quiksilver jacket

quiksilver jeans Quiksilver Shirt

Quiksilver Shirt

Quiksilver Pants

quiksilver shoes Quiksilver Shoes

quiksilver sweaters Quiksilver Sweaters

quiksilver tank Quiksilver Tanks

quiksilver tees Quiksilver Tops

Quiksilver Tees

quiksilver wetsuits Quiksilver Wetsuits

Quiksilver Wetsuits

Hey, check out this Organic Skin Care European Soaps along with Natural Lavender Body Lotion and shea butter

This is the guy we need in government office. His name is Kevin Carr City Of Stanton Council Candidate and he is a great guy. and we can get surf t shirts surfing shirt and Swim Shop for swim wear wimming gear women's and men's This is the guy we need in government office. His name is Kevin Carr City Of Stanton Council Candidate and he is a great guy. and we can get surf t shirts surfing shirt and Swim Shop for swim wear wimming gear women's and men's

 

French Lavender Soaps Organic And Natural Body Care Shea Body Butters

If you may be in the market for French Lavender Soaps or Thyme Body Care,
or even Shea Body Butters, ShopErba has the finest products available

Ken Arnold is the Democratic Candidate For the 68th Assembly District.

Board Shorts >>


Quiksilver Board Shorts
Roxy Board Shorts
Volcom Board Shorts
Billabong Board Shorts
Quicksilver Board Shorts
Hurley Board Shorts
Quicksilveredition Board Shorts
Rusty Board Shorts
Reef Board Shorts
oneill Board Shorts

Ezekiel surf clothing Board Shorts
Paul Frank surf clothing Board Shorts
Mada surf clothing Board Shorts
Analog surf clothing Board Shorts
Quicksilver surf clothing Board Shorts
Jet Pilot surf clothing Board Shorts
Quicksilveredition surf clothing Board Shorts
Dakine surf clothing Board Shorts
Matix surf clothing Board Shorts
Reef surf clothing Board Shorts
O'neil surf clothing Board Shorts

Also, you will want to check out Stanton California so you can see what's up and they are part of Stanton City Hall as well.

You can also get Organic Skin Care products from Bliss Bath Body and you must check out their Natural Body Lotions and bath soaps

Now if you are looking for the best deals on surf clothing from Quiksilver and Roxy then you have to check these amazing deals here: quiksilver tees http://www.killerdana.com/killerdana/search2.asp?SearchResults.SearchHandle=QT1NZW5zIFNTIFRlZXN_Qj1NZW5zIFNTIFRlZXN_RD01OX5FPTBeMV4yXm51bV9uZXd_ST1QcmljZX5LPTR_TD0xfk09NH5OPTN_&SearchResults.SortAscending=False&SearchResults.SortBy=num_new&Answer=766

quiksilver shirt Quiksilver Shirt

quiksilver board short quiksilver board short

quiksilver clothing Quiksilver clothing

quiksilver apparel Quiksilver apparel

quiksilver jackets quiksilver jacket

quiksilver jeans Quiksilver Shirt

Quiksilver Shirt

Quiksilver Pants

quiksilver shoes Quiksilver Shoes

quiksilver sweaters Quiksilver Sweaters

quiksilver tank Quiksilver Tanks

quiksilver tees Quiksilver Tops

Quiksilver Tees

quiksilver wetsuits Quiksilver Wetsuits

Quiksilver Wetsuits

Hey, check out this Organic Skin Care European Soaps along with Natural Lavender Body Lotion and shea butter