Chance

public struct Chance
extension Chance: Equatable
extension Chance: Hashable
extension Chance: ExpressibleByFloatLiteral
extension Chance: Describable

A struct that represents the chance or probability of something happening.

Chances are stored as fractions, and the constructors that take decimal values use some algorithm off of StackOverflow to convert them to fractions.

Since

0.16.0

Author

Samasaur
  • n

    The numerator of the fraction

    Declaration

    Swift

    public let n: Int
  • d

    The denominator of the fraction

    Declaration

    Swift

    public let d: Int
  • The decimal representation of the fraction.

    Declaration

    Swift

    public var value: Double { get }
  • The fraction as a tuple (numerator, denominator).

    Declaration

    Swift

    public var fraction: (Int, Int) { get }
  • Creates a new Chance object of the fraction form 1/d.

    Declaration

    Swift

    public static func oneOut(of d: Int) throws -> Chance

    Parameters

    d

    The denominator of the fraction.

    Return Value

    A new fraction with the given denominator and a numerator of 1.

  • Creates a new Chance object of the fraction form n/d.

    Declaration

    Swift

    public init(_ n: Int, outOf d: Int) throws

    Parameters

    n

    The numerator of the fraction.

    d

    The denominator of the fraction.

  • Creates a new Chance object of the fraction form 1/d.

    Declaration

    Swift

    public init(oneOutOf d: Int) throws

    Parameters

    d

    The denominator of the fraction.

  • Creates a new Chance object approximating the given decimal.

    This uses some algorithm off of StackOverflow.

    Declaration

    Swift

    public init(approximating x0: Double) throws

    Parameters

    x0

    The decimal value to convert to a fraction.

  • A Chance of zero.

    Declaration

    Swift

    public static let zero: Chance
  • one

    A Chance of one.

    Declaration

    Swift

    public static let one: Chance
  • Declaration

    Swift

    public static func == (lhs: Chance, rhs: Chance) -> Bool
  • Declaration

    Swift

    public func hash(into hasher: inout Hasher)
  • Declaration

    Swift

    public typealias FloatLiteralType = Double
  • Creates a new Chance object approximating the given decimal. There is no error-checking!

    This function calls init(approximating:), and will crash if that function throws an error.

    Declaration

    Swift

    public init(floatLiteral value: Chance.FloatLiteralType)

    Parameters

    value

    The decimal value to convert to a fraction.

  • The greatest common divisor/factor of two integers.

    Since

    0.17.0

    Declaration

    Swift

    static func gcd(_ a: Int, _ b: Int) -> Int

    Parameters

    a

    The first integer.

    b

    The second integer.

    Return Value

    The greatest common divisor of the two integers.

  • The least/lowest common multiple of two integers.

    Since

    0.17.0

    Declaration

    Swift

    static func lcm(_ a: Int, _ b: Int) -> Int

    Parameters

    a

    The first integer.

    b

    The second integer.

    Return Value

    The lowest common multiple of the two integers.

  • Adds two Chance instances together.

    Since

    0.17.0

    Declaration

    Swift

    static func + (lhs: Chance, rhs: Chance) -> Chance

    Parameters

    lhs

    The augend (first summand).

    rhs

    The addend (second summand).

    Return Value

    The sum of the two Chance instances.

  • Subtracts one Chance instance from another.

    Since

    0.17.0

    Declaration

    Swift

    static func - (lhs: Chance, rhs: Chance) -> Chance

    Parameters

    lhs

    The minuend (the value to be subtracted from).

    rhs

    The subtrahend (the value to subtract).

    Return Value

    The difference of the two values.

  • Adds the two Chance instances and sets the left-hand instance to the sum.

    Since

    0.17.0

    Declaration

    Swift

    static func += (lhs: inout Chance, rhs: Chance)

    Parameters

    lhs

    The summand that will be set to the sum.

    rhs

    The summand that will not be set to the sum.

  • Multiplies two Chance instances together.

    Since

    0.24.0

    Declaration

    Swift

    static func * (lhs: Chance, rhs: Chance) -> Chance

    Parameters

    lhs

    The first factor to multiply.

    rhs

    The second factor to multiply.

    Return Value

    The product of the two factors.

  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public var debugDescription: String { get }