Protocolswift 6.0.1Swift
AdditiveArithmetic
A type with values that support addition and subtraction.
protocol AdditiveArithmetic : Equatable
The AdditiveArithmetic
protocol provides a suitable basis for additive arithmetic on scalar values, such as integers and floating-point numbers, or vectors. You can write generic methods that operate on any numeric type in the standard library by using the AdditiveArithmetic
protocol as a generic constraint.
The following code declares a method that calculates the total of any sequence with AdditiveArithmetic
elements.
extension Sequence where Element: AdditiveArithmetic {
func sum() -> Element {
return reduce(.zero, +)
}
}
The sum()
method is now available on any sequence with values that conform to AdditiveArithmetic
, whether it is an array of Double
or a range of Int
.
let arraySum = [1.1, 2.2, 3.3, 4.4, 5.5].sum()
// arraySum == 16.5
let rangeSum = (1..<10).sum()
// rangeSum == 45
Conforming to the AdditiveArithmetic Protocol
To add AdditiveArithmetic
protocol conformance to your own custom type, implement the required operators, and provide a static zero
property.
Supertypes
protocol Equatable
A type that can be compared for value equality.
Requirements
Type members
static var zero: Self
The zero value.
static func + (lhs: Self, rhs: Self
) -> Self Adds two values and produces their sum.
static func += (lhs: inout Self, rhs: Self
) Adds two values and stores the result in the left-hand-side variable.
static func - (lhs: Self, rhs: Self
) -> Self Subtracts one value from another and produces their difference.
static func -= (lhs: inout Self, rhs: Self
) Subtracts the second value from the first and stores the difference in the left-hand-side variable.
Citizens in Swift
Type members
static func + (x: Self
) -> Self Returns the given number unchanged.
static func += (lhs: inout Self, rhs: Self
) static func -= (lhs: inout Self, rhs: Self
)
Subtypes
protocol BinaryFloatingPoint
A radix-2 (binary) floating-point type.
protocol BinaryInteger
An integer type with a binary representation.
protocol DurationProtocol
A type that defines a duration for a given
InstantProtocol
type.protocol FixedWidthInteger
An integer type that uses a fixed size for every instance.
protocol FloatingPoint
A floating-point numeric type.
protocol Numeric
A type with values that support multiplication.
protocol SignedInteger
An integer type that can represent both positive and negative values.
protocol SignedNumeric
A numeric type with a negation operation.
protocol UnsignedInteger
An integer type that can represent only nonnegative values.
Citizens in Swift
where Self:ExpressibleByIntegerLiteral
Type members
static var zero: Self
The zero value.
Extension in RealModule
Instance members
func isApproximatelyEqual<Magnitude>(to: Self, absoluteTolerance: Magnitude, relativeTolerance: Magnitude, norm: (Self) -> Magnitude
) -> Bool Test if
self
andother
are approximately equal with specified tolerances and norm.
Subtypes
protocol AlgebraicField
A type modeling an algebraic field. Refines the
SignedNumeric
protocol, adding division.protocol ElementaryFunctions
A type that has elementary functions available.
protocol Real
A type that models the real numbers.
protocol RealFunctions