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-
The numerator of the fraction
Declaration
Swift
public let n: Int -
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
Chanceobject of the fraction form 1/d.Declaration
Swift
public static func oneOut(of d: Int) throws -> ChanceParameters
dThe denominator of the fraction.
Return Value
A new fraction with the given denominator and a numerator of 1.
-
Creates a new
Chanceobject of the fraction form n/d.Declaration
Swift
public init(_ n: Int, outOf d: Int) throwsParameters
nThe numerator of the fraction.
dThe denominator of the fraction.
-
Creates a new
Chanceobject of the fraction form 1/d.Declaration
Swift
public init(oneOutOf d: Int) throwsParameters
dThe denominator of the fraction.
-
Creates a new
Chanceobject approximating the given decimal.This uses some algorithm off of StackOverflow.
Declaration
Swift
public init(approximating x0: Double) throwsParameters
x0The decimal value to convert to a fraction.
-
A
Chanceof zero.Declaration
Swift
public static let zero: Chance -
A
Chanceof 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
Chanceobject 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
valueThe 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) -> IntParameters
aThe first integer.
bThe 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) -> IntParameters
aThe first integer.
bThe second integer.
Return Value
The lowest common multiple of the two integers.
-
Adds two
Chanceinstances together.Since
0.17.0
Declaration
Swift
static func + (lhs: Chance, rhs: Chance) -> ChanceParameters
lhsThe augend (first summand).
rhsThe addend (second summand).
Return Value
The sum of the two
Chanceinstances. -
Subtracts one
Chanceinstance from another.Since
0.17.0
Declaration
Swift
static func - (lhs: Chance, rhs: Chance) -> ChanceParameters
lhsThe minuend (the value to be subtracted from).
rhsThe subtrahend (the value to subtract).
Return Value
The difference of the two values.
-
Adds the two
Chanceinstances and sets the left-hand instance to the sum.Since
0.17.0
Declaration
Swift
static func += (lhs: inout Chance, rhs: Chance)Parameters
lhsThe summand that will be set to the sum.
rhsThe summand that will not be set to the sum.
-
Multiplies two
Chanceinstances together.Since
0.24.0
Declaration
Swift
static func * (lhs: Chance, rhs: Chance) -> ChanceParameters
lhsThe first factor to multiply.
rhsThe 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 }
View on GitHub
Chance Structure Reference