Protocol

# Numeric

A type with values that support multiplication.

``protocol Numeric : AdditiveArithmetic, ExpressibleByIntegerLiteral``

The `Numeric` protocol provides a suitable basis for arithmetic on scalar values, such as integers and floating-point numbers. You can write generic methods that operate on any numeric type in the standard library by using the `Numeric` protocol as a generic constraint.

The following example extends `Sequence` with a method that returns an array with the sequenceâ€™s values multiplied by two.

``````extension Sequence where Element: Numeric {
func doublingAll() -> [Element] {
return map { \$0 * 2 }
}
}``````

With this extension, any sequence with elements that conform to `Numeric` has the `doublingAll()` method. For example, you can double the elements of an array of doubles or a range of integers:

``````let observations = [1.5, 2.0, 3.25, 4.875, 5.5]
let doubledObservations = observations.doublingAll()
// doubledObservations == [3.0, 4.0, 6.5, 9.75, 11.0]

let integers = 0..<8
let doubledIntegers = integers.doublingAll()
// doubledIntegers == [0, 2, 4, 6, 8, 10, 12, 14]``````

## Conforming to the Numeric Protocol

To add `Numeric` protocol conformance to your own custom type, implement the required initializer and operators, and provide a `magnitude` property using a type that can represent the magnitude of any value of your custom type.

## Supertypes

• `protocol AdditiveArithmetic`

A type with values that support addition and subtraction.

• `protocol Equatable`

A type that can be compared for value equality.

• `protocol ExpressibleByIntegerLiteral`

A type that can be initialized with an integer literal.

## Requirements

• `associatedtype Magnitude : Comparable, Numeric`

A type that can represent the absolute value of any possible value of the conforming type.

• `init?<T>(exactly: T)`

Creates a new instance from the given integer, if it can be represented exactly.

• `var magnitude: Self.Magnitude`

The magnitude of this value.

• `static func * (Self, Self) -> Self`

Multiplies two values and produces their product.

• `static func *= (inout Self, Self)`

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

## Citizens in Swift

### Subtypes

• `protocol BinaryFloatingPoint`

• `protocol BinaryInteger`

An integer type with a binary representation.

• `protocol FixedWidthInteger`

An integer type that uses a fixed size for every instance.

• `protocol FloatingPoint`

A floating-point numeric type.

• `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.

## Extension in Atomics

### Subtypes

• `protocol AtomicInteger`

A type that supports atomic integer operations through a separate atomic storage representation.

## Extension in RealModule

### Subtypes

• `protocol AlgebraicField`

A type modeling an algebraic field. Refines the `SignedNumeric` protocol, adding division.

• `protocol Real`

A type that models the real numbers.

## Extension in RealModule

`where Self.Magnitude:FloatingPoint`

### Members

• `func isApproximatelyEqual(to: Self, absoluteTolerance: Self.Magnitude, relativeTolerance: Self.Magnitude) -> Bool`

Test if `self` and `other` are approximately equal with specified tolerances.

• `func isApproximatelyEqual(to: Self, relativeTolerance: Self.Magnitude, norm: (Self) -> Self.Magnitude) -> Bool`

Test if `self` and `other` are approximately equal.