RatNum
public struct RatNum : Comparable, CustomStringConvertible
AbstractState: RatNum
represents an immutable rational number.
It includes all of the elements in the set of rationals, as well
as the special NaN
(not-a-number) element that results from
division by zero.
The NaN
element is special in many ways. Any arithmetic
operation (such as addition) involving NaN
will return NaN
.
With respect to comparison operations, such as less-than, NaN
is
considered equal to itself, and larger than all other rationals.
Examples of RatNums include -1/13
, 53/7
, 4
, NaN
, and 0
.
-
A constant holding a Not-a-Number (NaN) value of type RatNum
Declaration
Swift
public static let nan: RatNum
-
A constant holding a zero value of type RatNum
Declaration
Swift
public static let zero: RatNum
-
A constant holding a one value of type RatNum
Declaration
Swift
public static let one: RatNum
-
Effect: If d = 0, constructs a new RatNum = NaN. Else constructs a new RatNum = (n / d).
Declaration
Swift
public init(_ n: Int, _ d: Int)
Parameters
n
The numerator of the new RatNum.
d
The denominator of the new RatNum.
-
Effect: Constructs a new RatNum = n.
Declaration
Swift
public init(_ n: Int)
Parameters
n
The value of the new RatNum.
-
Makes a RatNum from a string describing it.
Requires: ‘description’ is an instance of a string, with no spaces, of the form:
NaN
N/M
, where N and M are both integers in decimal notation, and M != 0, orN
, where N is an integer in decimal notation.NaN if description =
NaN
.A RatNum r = ( N / M ), letting M be 1 in the case where only
N
is passed in.
Declaration
Swift
public init?(_ description: String)
Parameters
description
A string of the format described in the
requires
clause.Return Value
Either:
-
true iff self is NaN (not-a-number)
Declaration
Swift
public var isNaN: Bool { get }
-
true iff self > 0
Declaration
Swift
public var isPositive: Bool { get }
-
true iff self < 0
Declaration
Swift
public var isNegative: Bool { get }
-
An approximate value of this rational. Note that
NaN
is mapped to Double.nan, which is treated in a special manner by several arithmetic operations, such as the comparison and equality operators.Declaration
Swift
public var asDouble: Double { get }
-
A String representing self, in reduced terms. It will either be
NaN
, or it will take on either of the formsN
orN/M
, where N and M are both integers in decimal notation and M != 0.Declaration
Swift
public var description: String { get }
-
Declaration
Swift
public static func < (lhs: RatNum, rhs: RatNum) -> Bool
Return Value
true iff lhs < rhs
-
Declaration
Swift
public static func == (lhs: RatNum, rhs: RatNum) -> Bool
Return Value
true iff lhs == rhs
-
Declaration
Swift
public static func + (lhs: RatNum, rhs: RatNum) -> RatNum
Return Value
lhs + rhs, or NaN if either lhs or rhs is NaN
-
Declaration
Swift
public static func - (lhs: RatNum, rhs: RatNum) -> RatNum
Return Value
lhs - rhs, or NaN if either lhs or rhs is NaN
-
Declaration
Swift
public static func * (lhs: RatNum, rhs: RatNum) -> RatNum
Return Value
lhs * rhs, or NaN if either lhs or rhs is NaN
-
Declaration
Swift
public static func / (lhs: RatNum, rhs: RatNum) -> RatNum
Return Value
lhs / rhs, or NaN if either lhs or rhs is NaN or rhs == 0
-
Declaration
Swift
public prefix static func - (n: RatNum) -> RatNum
Return Value
the additive inverse of n (ie, 0 - n)