AdditiveArithmetic

protocol AdditiveArithmetic

A type with values that support addition and subtraction.

Inheritance Equatable
Conforming Types Numeric

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 using a type that can represent the magnitude of any value of your custom type.

Type Variables

var zero Required

The zero value.

Zero is the identity element for addition. For any value, x + .zero == x and .zero + x == x.

Declaration

var zero: Self

Type Methods

func +(lhs: Self, rhs: Self) -> Self Required

Adds two values and produces their sum.

The addition operator (+) calculates the sum of its two arguments. For example:

1 + 2                   // 3
-10 + 15                // 5
-15 + -5                // -20
21.5 + 3.25             // 24.75

You cannot use + with arguments of different types. To add values of different types, convert one of the values to the other value's type.

let x: Int8 = 21
let y: Int = 1000000
Int(x) + y              // 1000021

Declaration

static func +(lhs: Self, rhs: Self) -> Self
func +=(lhs: inout Self, rhs: Self) Required

Adds two values and stores the result in the left-hand-side variable.

Declaration

static func +=(lhs: inout Self, rhs: Self)
func -(lhs: Self, rhs: Self) -> Self Required

Subtracts one value from another and produces their difference.

The subtraction operator (-) calculates the difference of its two arguments. For example:

8 - 3                   // 5
-10 - 5                 // -15
100 - -5                // 105
10.5 - 100.0            // -89.5

You cannot use - with arguments of different types. To subtract values of different types, convert one of the values to the other value's type.

let x: UInt8 = 21
let y: UInt = 1000000
y - UInt(x)             // 999979

Declaration

static func -(lhs: Self, rhs: Self) -> Self
func -=(lhs: inout Self, rhs: Self) Required

Subtracts the second value from the first and stores the difference in the left-hand-side variable.

Declaration

static func -=(lhs: inout Self, rhs: Self)

Default Implementations

func !=(lhs: Self, rhs: Self) -> Bool

Declaration

public static func !=(lhs: Self, rhs: Self) -> Bool