struct Float80
Inheritance 
BinaryFloatingPoint, Comparable, CustomDebugStringConvertible, CustomStringConvertible, Equatable, ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral, FloatingPoint, Hashable, LosslessStringConvertible, Numeric, SignedNumeric, Strideable
View Protocol Hierarchy →


Associated Types 
Magnitude = Float80
A type that can represent the absolute value of any possible value of this type. Exponent = Int
A type that can represent any written exponent. RawSignificand = UInt64
A type that represents the encoded significand of a value. 
Import  import Swift 
Initializers
Creates a value initialized to zero.
Declaration
init()
Creates a new instance that approximates the given value.
The value of other
is rounded to a representable value, if necessary.
A NaN passed as other
results in another NaN, with a signaling NaN
value converted to quiet NaN.
let x: Double = 21.25
let y = Float80(x)
// y == 21.25
let z = Float80(Double.nan)
// z.isNaN == true
other
: The value to use for the new instance.
Declaration
init(_ other: Double)
Creates a new instance that approximates the given value.
The value of other
is rounded to a representable value, if necessary.
A NaN passed as other
results in another NaN, with a signaling NaN
value converted to quiet NaN.
let x: Float = 21.25
let y = Float80(x)
// y == 21.25
let z = Float80(Float.nan)
// z.isNaN == true
other
: The value to use for the new instance.
Declaration
init(_ other: Float)
Creates a new instance initialized to the given value.
The value of other
is represented exactly by the new instance. A NaN
passed as other
results in another NaN, with a signaling NaN value
converted to quiet NaN.
let x: Float80 = 21.25
let y = Float80(x)
// y == 21.25
let z = Float80(Float80.nan)
// z.isNaN == true
other
: The value to use for the new instance.
Declaration
init(_ other: Float80)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: Int)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: Int8)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: Int16)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: Int32)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: Int64)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: UInt)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: UInt8)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: UInt16)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: UInt32)
Creates the closest representable value to the given integer.
value
: The integer to represent as a floatingpoint value.
Declaration
init(_ v: UInt64)
Creates a new value, rounded to the closest possible representation.
If two representable values are equally close, the result is the value with more trailing zeros in its significand bit pattern.
value
: The integer to convert to a floatingpoint value.
Declaration
init<Source>(_ value: Source)
Declared In
BinaryFloatingPoint
Creates a new value from the given floatingpoint literal.
Do not call this initializer directly. It is used by the compiler when
you create a new Float80
instance by using a floatingpoint literal.
Instead, create a new value by using a literal.
In this example, the assignment to the x
constant calls this
initializer behind the scenes.
let x: Float80 = 21.25
// x == 21.25
value
: The new floatingpoint value.
Declaration
init(floatLiteral value: Float80)
Creates a new value from the given integer literal.
Do not call this initializer directly. It is used by the compiler when
you create a new Float80
instance by using an integer literal.
Instead, create a new value by using a literal.
In this example, the assignment to the x
constant calls this
initializer behind the scenes.
let x: Float80 = 100
// x == 100.0
value
: The new value.
Declaration
init(integerLiteral value: Int64)
Declared In
Float80
, BinaryFloatingPoint
, ExpressibleByIntegerLiteral
Creates a NaN ("not a number") value with the specified payload.
NaN values compare not equal to every value, including themselves. Most
operations with a NaN operand produce a NaN result. Don't use the
equalto operator (==
) to test whether a value is NaN. Instead, use
the value's isNaN
property.
let x = Float80(nan: 0, signaling: false)
print(x == .nan)
// Prints "false"
print(x.isNaN)
// Prints "true"
Parameters:
payload: The payload to use for the new NaN value.
signaling: Pass true
to create a signaling NaN or false
to create
a quiet NaN.
Declaration
init(nan payload: Float80.RawSignificand, signaling: Bool)
Creates a new value from the given sign, exponent, and significand.
The following example uses this initializer to create a new Double
instance. Double
is a binary floatingpoint type that has a radix of
2
.
let x = Double(sign: .plus, exponent: 2, significand: 1.5)
// x == 0.375
This initializer is equivalent to the following calculation, where **
is exponentiation, computed as if by a single, correctly rounded,
floatingpoint operation:
let sign: FloatingPointSign = .plus
let exponent = 2
let significand = 1.5
let y = (sign == .minus ? 1 : 1) * significand * Double.radix ** exponent
// y == 0.375
As with any basic operation, if this value is outside the representable range of the type, overflow or underflow occurs, and zero, a subnormal value, or infinity may result. In addition, there are two other edge cases:
 If the value you pass to
significand
is zero or infinite, the result is zero or infinite, regardless of the value ofexponent
.  If the value you pass to
significand
is NaN, the result is NaN.
For any floatingpoint value x
of type F
, the result of the following
is equal to x
, with the distinction that the result is canonicalized
if x
is in a noncanonical encoding:
let x0 = F(sign: x.sign, exponent: x.exponent, significand: x.significand)
This initializer implements the scaleB
operation defined by the IEEE
754 specification.
Parameters: sign: The sign to use for the new value. exponent: The new value's exponent. significand: The new value's significand.
Declaration
init(sign: FloatingPointSign, exponent: Int, significand: Float80)
Creates a new instance from the specified sign and bit patterns.
The values passed as exponentBitPattern
and significandBitPattern
are
interpreted in the binary interchange format defined by the IEEE 754
specification.
Parameters: sign: The sign of the new value. exponentBitPattern: The bit pattern to use for the exponent field of the new value. significandBitPattern: The bit pattern to use for the significand field of the new value.
Declaration
init(sign: FloatingPointSign, exponentBitPattern: UInt, significandBitPattern: UInt64)
Creates a new floatingpoint value using the sign of one value and the magnitude of another.
The following example uses this initializer to create a new Double
instance with the sign of a
and the magnitude of b
:
let a = 21.5
let b = 305.15
let c = Double(signOf: a, magnitudeOf: b)
print(c)
// Prints "305.15"
This initializer implements the IEEE 754 copysign
operation.
Parameters:
signOf: A value from which to use the sign. The result of the
initializer has the same sign as signOf
.
magnitudeOf: A value from which to use the magnitude. The result of
the initializer has the same magnitude as magnitudeOf
.
Declaration
init(signOf: Float80, magnitudeOf: Float80)
Declared In
BinaryFloatingPoint
Creates a new instance from the given string.
The string passed as text
can represent a real number in decimal or
hexadecimal format or special floatingpoint values for infinity and NaN
("not a number").
The given string may begin with a plus or minus sign character (+
or

). The allowed formats for each of these representations is then as
follows:

A decimal value contains the significand, a sequence of decimal digits that may include a decimal point.
let c = Float80("1.0") // c == 1.0
let d = Float80("28.375") // d == 28.375
A decimal value may also include an exponent following the significand, indicating the power of 10 by which the significand should be multiplied. If included, the exponent is separated by a single character,
e
orE
, and consists of an optional plus or minus sign character and a sequence of decimal digits.let e = Float80("2837.5e2") // e == 28.375

A hexadecimal value contains the significand, either
0X
or0x
, followed by a sequence of hexadecimal digits. The significand may include a decimal point.let f = Float80("0x1c.6") // f == 28.375
A hexadecimal value may also include an exponent following the significand, indicating the power of 2 by which the significand should be multiplied. If included, the exponent is separated by a single character,
p
orP
, and consists of an optional plus or minus sign character and a sequence of decimal digits.let g = Float80("0x1.c6p4") // g == 28.375

A value of infinity contains one of the strings
"inf"
or"infinity"
, case insensitive.let i = Float80("inf") // i == Float80.infinity
let j = Float80("Infinity") // j == Float80.infinity

A value of NaN contains the string
"nan"
, case insensitive.let n = Float80("nan") // n?.isNaN == true // n?.sign == .minus
A NaN value may also include a payload in parentheses following the
"nan"
keyword. The payload consists of a sequence of decimal digits, or the characters0X
or0x
followed by a sequence of hexadecimal digits. If the payload contains any other characters, it is ignored. If the value of the payload is larger than can be stored as the payload of aFloat80.nan
, the least significant bits are used.let p = Float80("nan(0x10)") // p?.isNaN == true // String(p!) == "nan(0x10)"
Passing any other format or any additional characters as text
results
in nil
. For example, the following conversions result in nil
:
Float80(" 5.0") // Includes whitespace
Float80("±2.0") // Invalid character
Float80("0x1.25e4") // Incorrect exponent format
text
: The input string to convert to a Float80
instance. If
text
has invalid characters or is in an invalid format, the result
is nil
.
Declaration
init?<S>(_ text: S)
Creates a new instance initialized to the given value, if it can be represented without rounding.
If other
can't be represented as an instance of Float80
without
rounding, the result of this initializer is nil
. In particular,
passing NaN as other
always results in nil
.
let x: Double = 21.25
let y = Float80(exactly: x)
// y == Optional.some(21.25)
let z = Float80(exactly: Double.nan)
// z == nil
other
: The value to use for the new instance.
Declaration
init?(exactly other: Double)
Creates a new instance initialized to the given value, if it can be represented without rounding.
If other
can't be represented as an instance of Float80
without
rounding, the result of this initializer is nil
. In particular,
passing NaN as other
always results in nil
.
let x: Float = 21.25
let y = Float80(exactly: x)
// y == Optional.some(21.25)
let z = Float80(exactly: Float.nan)
// z == nil
other
: The value to use for the new instance.
Declaration
init?(exactly other: Float)
Creates a new instance initialized to the given value, if it can be represented without rounding.
If other
can't be represented as an instance of Float80
without
rounding, the result of this initializer is nil
. In particular,
passing NaN as other
always results in nil
.
let x: Float80 = 21.25
let y = Float80(exactly: x)
// y == Optional.some(21.25)
let z = Float80(exactly: Float80.nan)
// z == nil
other
: The value to use for the new instance.
Declaration
init?(exactly other: Float80)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: Int)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: Int8)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: Int16)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: Int32)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: Int64)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: UInt)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: UInt8)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: UInt16)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: UInt32)
Creates a value that exactly represents the given integer.
If the given integer is outside the representable range of this type or
can't be represented exactly, the result is nil
.
value
: The integer to represent as a floatingpoint value.
Declaration
init?(exactly value: UInt64)
Creates a new value, if the given integer can be represented exactly.
If the given integer cannot be represented exactly, the result is nil
.
value
: The integer to convert to a floatingpoint value.
Declaration
init?<Source>(exactly value: Source)
Declared In
BinaryFloatingPoint
Static Variables
The number of bits used to represent the type's exponent.
A binary floatingpoint type's exponentBitCount
imposes a limit on the
range of the exponent for normal, finite values. The exponent bias of
a type F
can be calculated as the following, where **
is
exponentiation:
let bias = 2 ** (F.exponentBitCount  1)  1
The least normal exponent for values of the type F
is 1  bias
, and
the largest finite exponent is bias
. An allzeros exponent is reserved
for subnormals and zeros, and an allones exponent is reserved for
infinity and NaN.
For example, the Float
type has an exponentBitCount
of 8, which gives
an exponent bias of 127
by the calculation above.
let bias = 2 ** (Float.exponentBitCount  1)  1
// bias == 127
print(Float.greatestFiniteMagnitude.exponent)
// Prints "127"
print(Float.leastNormalMagnitude.exponent)
// Prints "126"
Declaration
static var exponentBitCount: Int { get }
The greatest finite number representable by this type.
This value compares greater than or equal to all finite numbers, but less
than infinity
.
This value corresponds to typespecific C macros such as FLT_MAX
and
DBL_MAX
. The naming of those macros is slightly misleading, because
infinity
is greater than this value.
Declaration
static var greatestFiniteMagnitude: Float80 { get }
Positive infinity.
Infinity compares greater than all finite numbers and equal to other infinite values.
let x = Double.greatestFiniteMagnitude
let y = x * 2
// y == Double.infinity
// y > x
Declaration
static var infinity: Float80 { get }
The least positive number.
This value compares less than or equal to all positive numbers, but
greater than zero. If the type supports subnormal values,
leastNonzeroMagnitude
is smaller than leastNormalMagnitude
;
otherwise they are equal.
Declaration
static var leastNonzeroMagnitude: Float80 { get }
The least positive normal number.
This value compares less than or equal to all positive normal numbers. There may be smaller positive numbers, but they are subnormal, meaning that they are represented with less precision than normal numbers.
This value corresponds to typespecific C macros such as FLT_MIN
and
DBL_MIN
. The naming of those macros is slightly misleading, because
subnormals, zeros, and negative numbers are smaller than this value.
Declaration
static var leastNormalMagnitude: Float80 { get }
A quiet NaN ("not a number").
A NaN compares not equal, not greater than, and not less than every value, including itself. Passing a NaN to an operation generally results in NaN.
let x = 1.21
// x > Double.nan == false
// x < Double.nan == false
// x == Double.nan == false
Because a NaN always compares not equal to itself, to test whether a
floatingpoint value is NaN, use its isNaN
property instead of the
equalto operator (==
). In the following example, y
is NaN.
let y = x + Double.nan
print(y == Double.nan)
// Prints "false"
print(y.isNaN)
// Prints "true"
Declaration
static var nan: Float80 { get }
The mathematical constant pi.
This value should be rounded toward zero to keep user computations with
angles from inadvertently ending up in the wrong quadrant. A type that
conforms to the FloatingPoint
protocol provides the value for pi
at
its best possible precision.
print(Double.pi)
// Prints "3.14159265358979"
Declaration
static var pi: Float80 { get }
The radix, or base of exponentiation, for this floatingpoint type.
All binary floatingpoint types have a radix of 2. The magnitude of a
floatingpoint value x
of type F
can be calculated by using the
following formula, where **
is exponentiation:
let magnitude = x.significand * F.radix ** x.exponent
Declaration
static var radix: Int { get }
Declared In
BinaryFloatingPoint
A signaling NaN ("not a number").
The default IEEE 754 behavior of operations involving a signaling NaN is to raise the Invalid flag in the floatingpoint environment and return a quiet NaN.
Operations on types conforming to the FloatingPoint
protocol should
support this behavior, but they might also support other options. For
example, it would be reasonable to implement alternative operations in
which operating on a signaling NaN triggers a runtime error or results
in a diagnostic for debugging purposes. Types that implement alternative
behaviors for a signaling NaN must document the departure.
Other than these signaling operations, a signaling NaN behaves in the same manner as a quiet NaN.
Declaration
static var signalingNaN: Float80 { get }
The available number of fractional significand bits.
For fixedwidth floatingpoint types, this is the actual number of fractional significand bits.
For extensible floatingpoint types, significandBitCount
should be the
maximum allowed significand width (without counting any leading integral
bit of the significand). If there is no upper limit, then
significandBitCount
should be Int.max
.
Float80.significandBitCount
is 63, even though 64 bits are used to
store the significand in the memory representation of a Float80
instance. Unlike other floatingpoint types, the Float80
type
explicitly stores the leading integral significand bit.
Declaration
static var significandBitCount: Int { get }
The unit in the last place of 1.0.
The positive difference between 1.0 and the next greater representable
number. The ulpOfOne
constant corresponds to the C macros
FLT_EPSILON
, DBL_EPSILON
, and others with a similar purpose.
Declaration
static var ulpOfOne: Float80 { get }
Declared In
Float80
, BinaryFloatingPoint
, FloatingPoint
Instance Variables
The floatingpoint value with the same sign and exponent as this value, but with a significand of 1.0.
A binade is a set of binary floatingpoint values that all have the
same sign and exponent. The binade
property is a member of the same
binade as this value, but with a unit significand.
In this example, x
has a value of 21.5
, which is stored as
1.34375 * 2**4
, where **
is exponentiation. Therefore, x.binade
is
equal to 1.0 * 2**4
, or 16.0
.
let x = 21.5
// x.significand == 1.34375
// x.exponent == 4
let y = x.binade
// y == 16.0
// y.significand == 1.0
// y.exponent == 4
Declaration
var binade: Float80 { get }
A textual representation of the value, suitable for debugging.
Declaration
var debugDescription: String { get }
A textual representation of the value.
Declaration
var description: String { get }
The exponent of the floatingpoint value.
The exponent of a floatingpoint value is the integer part of the
logarithm of the value's magnitude. For a value x
of a floatingpoint
type F
, the magnitude can be calculated as the following, where **
is exponentiation:
let magnitude = x.significand * F.radix ** x.exponent
In the next example, y
has a value of 21.5
, which is encoded as
1.34375 * 2 ** 4
. The significand of y
is therefore 1.34375.
let y: Double = 21.5
// y.significand == 1.34375
// y.exponent == 4
// Double.radix == 2
The exponent
property has the following edge cases:
 If
x
is zero, thenx.exponent
isInt.min
.  If
x
is +/infinity or NaN, thenx.exponent
isInt.max
This property implements the logB
operation defined by the IEEE 754
specification.
Declaration
var exponent: Int { get }
The raw encoding of the value's exponent field.
This value is unadjusted by the type's exponent bias.
Declaration
var exponentBitPattern: UInt { get }
The classification of this value.
A value's floatingPointClass
property describes its "class" as
described by the IEEE 754 specification.
Declaration
var floatingPointClass: FloatingPointClassification { get }
Declared In
BinaryFloatingPoint
, FloatingPoint
The number's hash value.
Hash values are not guaranteed to be equal across different executions of your program. Do not save hash values to use during a future execution.
Declaration
var hashValue: Int { get }
A Boolean value indicating whether the instance's representation is in the canonical form.
The IEEE 754 specification defines a canonical, or preferred,
encoding of a floatingpoint value's representation. Every Float
or
Double
value is canonical, but noncanonical values of the Float80
type exist, and noncanonical values may exist for other types that
conform to the FloatingPoint
protocol.
Declaration
var isCanonical: Bool { get }
A Boolean value indicating whether this instance is finite.
All values other than NaN and infinity are considered finite, whether normal or subnormal.
Declaration
var isFinite: Bool { get }
A Boolean value indicating whether the instance is infinite.
Note that isFinite
and isInfinite
do not form a dichotomy, because
they are not total: If x
is NaN
, then both properties are false
.
Declaration
var isInfinite: Bool { get }
A Boolean value indicating whether the instance is NaN ("not a number").
Because NaN is not equal to any value, including NaN, use this property
instead of the equalto operator (==
) or notequalto operator (!=
)
to test whether a value is or is not NaN. For example:
let x = 0.0
let y = x * .infinity
// y is a NaN
// Comparing with the equalto operator never returns 'true'
print(x == Double.nan)
// Prints "false"
print(y == Double.nan)
// Prints "false"
// Test with the 'isNaN' property instead
print(x.isNaN)
// Prints "false"
print(y.isNaN)
// Prints "true"
This property is true
for both quiet and signaling NaNs.
Declaration
var isNaN: Bool { get }
A Boolean value indicating whether this instance is normal.
A normal value is a finite number that uses the full precision available to values of a type. Zero is neither a normal nor a subnormal number.
Declaration
var isNormal: Bool { get }
A Boolean value indicating whether the instance is a signaling NaN.
Signaling NaNs typically raise the Invalid flag when used in general computing operations.
Declaration
var isSignalingNaN: Bool { get }
A Boolean value indicating whether the instance is subnormal.
A subnormal value is a nonzero number that has a lesser magnitude than the smallest normal number. Subnormal values do not use the full precision available to values of a type.
Zero is neither a normal nor a subnormal number. Subnormal numbers are often called denormal or denormalizedthese are different names for the same concept.
Declaration
var isSubnormal: Bool { get }
A Boolean value indicating whether the instance is equal to zero.
The isZero
property of a value x
is true
when x
represents either
0.0
or +0.0
. x.isZero
is equivalent to the following comparison:
x == 0.0
.
let x = 0.0
x.isZero // true
x == 0.0 // true
Declaration
var isZero: Bool { get }
The magnitude of this value.
For any value x
, x.magnitude.sign
is .plus
. If x
is not NaN,
x.magnitude
is the absolute value of x
.
The global abs(_:)
function provides more familiar syntax when you need
to find an absolute value. In addition, because abs(_:)
always returns
a value of the same type, even in a generic context, using the function
instead of the magnitude
property is encouraged.
let targetDistance: Float80 = 5.25
let throwDistance: Float80 = 5.5
let margin = targetDistance  throwDistance
// margin == 0.25
// margin.magnitude == 0.25
// Use 'abs(_:)' instead of 'magnitude'
print("Missed the target by \(abs(margin)) meters.")
// Prints "Missed the target by 0.25 meters."
Declaration
var magnitude: Float80 { get }
The greatest representable value that compares less than this value.
For any finite value x
, x.nextDown
is less than x
. For nan
or
infinity
, x.nextDown
is x
itself. The following special cases
also apply:
 If
x
isinfinity
, thenx.nextDown
isgreatestFiniteMagnitude
.  If
x
isleastNonzeroMagnitude
, thenx.nextDown
is0.0
.  If
x
is zero, thenx.nextDown
isleastNonzeroMagnitude
.  If
x
isgreatestFiniteMagnitude
, thenx.nextDown
isinfinity
.
Declaration
var nextDown: Float80 { get }
Declared In
BinaryFloatingPoint
, FloatingPoint
The least representable value that compares greater than this value.
For any finite value x
, x.nextUp
is greater than x
. For nan
or
infinity
, x.nextUp
is x
itself. The following special cases also
apply:
 If
x
isinfinity
, thenx.nextUp
isgreatestFiniteMagnitude
.  If
x
isleastNonzeroMagnitude
, thenx.nextUp
is0.0
.  If
x
is zero, thenx.nextUp
isleastNonzeroMagnitude
.  If
x
isgreatestFiniteMagnitude
, thenx.nextUp
isinfinity
.
Declaration
var nextUp: Float80 { get }
The sign of the floatingpoint value.
The sign
property is .minus
if the value's signbit is set, and
.plus
otherwise. For example:
let x = 33.375
// x.sign == .minus
Do not use this property to check whether a floating point value is
negative. For a value x
, the comparison x.sign == .minus
is not
necessarily the same as x < 0
. In particular, x.sign == .minus
if
x
is 0, and while x < 0
is always false
if x
is NaN, x.sign
could be either .plus
or .minus
.
Declaration
var sign: FloatingPointSign { get }
The significand of the floatingpoint value.
The magnitude of a floatingpoint value x
of type F
can be calculated
by using the following formula, where **
is exponentiation:
let magnitude = x.significand * F.radix ** x.exponent
In the next example, y
has a value of 21.5
, which is encoded as
1.34375 * 2 ** 4
. The significand of y
is therefore 1.34375.
let y: Double = 21.5
// y.significand == 1.34375
// y.exponent == 4
// Double.radix == 2
If a type's radix is 2, then for finite nonzero numbers, the significand
is in the range 1.0 ..< 2.0
. For other values of x
, x.significand
is defined as follows:
 If
x
is zero, thenx.significand
is 0.0.  If
x
is infinity, thenx.significand
is 1.0.  If
x
is NaN, thenx.significand
is NaN. Note: The significand is frequently also called the mantissa, but significand is the preferred terminology in the IEEE 754 specification, to allay confusion with the use of mantissa for the fractional part of a logarithm.
Declaration
var significand: Float80 { get }
The raw encoding of the value's significand field.
The significandBitPattern
property does not include the leading
integral bit of the significand, even for types like Float80
that
store it explicitly.
Declaration
var significandBitPattern: UInt64 { get }
The number of bits required to represent the value's significand.
If this value is a finite nonzero number, significandWidth
is the
number of fractional bits required to represent the value of
significand
; otherwise, significandWidth
is 1. The value of
significandWidth
is always 1 or between zero and
significandBitCount
. For example:
 For any representable power of two,
significandWidth
is zero, becausesignificand
is1.0
.  If
x
is 10,x.significand
is1.01
in binary, sox.significandWidth
is 2.  If
x
is Float.pi,x.significand
is1.10010010000111111011011
in binary, andx.significandWidth
is 23.
Declaration
var significandWidth: Int { get }
The unit in the last place of this value.
This is the unit of the least significant digit in this value's
significand. For most numbers x
, this is the difference between x
and the next greater (in magnitude) representable number. There are some
edge cases to be aware of:
 If
x
is not a finite number, thenx.ulp
is NaN.  If
x
is very small in magnitude, thenx.ulp
may be a subnormal number. If a type does not support subnormals,x.ulp
may be rounded to zero. greatestFiniteMagnitude.ulp
is a finite number, even though the next greater representable value isinfinity
.
This quantity, or a related quantity, is sometimes called epsilon or machine epsilon. Avoid that name because it has different meanings in different languages, which can lead to confusion, and because it suggests that it is a good tolerance to use for comparisons, which it almost never is.
Declaration
var ulp: Float80 { get }
Static Methods
Returns the greater of the two given values.
This method returns the maximum of two values, preserving order and
eliminating NaN when possible. For two values x
and y
, the result of
maximum(x, y)
is x
if x > y
, y
if x <= y
, or whichever of x
or y
is a number if the other is a quiet NaN. If both x
and y
are
NaN, or either x
or y
is a signaling NaN, the result is NaN.
Double.maximum(10.0, 25.0)
// 10.0
Double.maximum(10.0, .nan)
// 10.0
Double.maximum(.nan, 25.0)
// 25.0
Double.maximum(.nan, .nan)
// nan
The maximum
method implements the maxNum
operation defined by the
IEEE 754 specification.
Parameters:
x: A floatingpoint value.
y: Another floatingpoint value.
Returns: The greater of x
and y
, or whichever is a number if the
other is NaN.
Declaration
static func maximum(_ x: Float80, _ y: Float80) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Returns the value with greater magnitude.
This method returns the value with greater magnitude of the two given
values, preserving order and eliminating NaN when possible. For two
values x
and y
, the result of maximumMagnitude(x, y)
is x
if
x.magnitude > y.magnitude
, y
if x.magnitude <= y.magnitude
, or
whichever of x
or y
is a number if the other is a quiet NaN. If both
x
and y
are NaN, or either x
or y
is a signaling NaN, the result
is NaN.
Double.maximumMagnitude(10.0, 25.0)
// 25.0
Double.maximumMagnitude(10.0, .nan)
// 10.0
Double.maximumMagnitude(.nan, 25.0)
// 25.0
Double.maximumMagnitude(.nan, .nan)
// nan
The maximumMagnitude
method implements the maxNumMag
operation
defined by the IEEE 754 specification.
Parameters:
x: A floatingpoint value.
y: Another floatingpoint value.
Returns: Whichever of x
or y
has greater magnitude, or whichever is
a number if the other is NaN.
Declaration
static func maximumMagnitude(_ x: Float80, _ y: Float80) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Returns the lesser of the two given values.
This method returns the minimum of two values, preserving order and
eliminating NaN when possible. For two values x
and y
, the result of
minimum(x, y)
is x
if x <= y
, y
if y < x
, or whichever of x
or y
is a number if the other is a quiet NaN. If both x
and y
are
NaN, or either x
or y
is a signaling NaN, the result is NaN.
Double.minimum(10.0, 25.0)
// 25.0
Double.minimum(10.0, .nan)
// 10.0
Double.minimum(.nan, 25.0)
// 25.0
Double.minimum(.nan, .nan)
// nan
The minimum
method implements the minNum
operation defined by the
IEEE 754 specification.
Parameters:
x: A floatingpoint value.
y: Another floatingpoint value.
Returns: The minimum of x
and y
, or whichever is a number if the
other is NaN.
Declaration
static func minimum(_ x: Float80, _ y: Float80) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Returns the value with lesser magnitude.
This method returns the value with lesser magnitude of the two given
values, preserving order and eliminating NaN when possible. For two
values x
and y
, the result of minimumMagnitude(x, y)
is x
if
x.magnitude <= y.magnitude
, y
if y.magnitude < x.magnitude
, or
whichever of x
or y
is a number if the other is a quiet NaN. If both
x
and y
are NaN, or either x
or y
is a signaling NaN, the result
is NaN.
Double.minimumMagnitude(10.0, 25.0)
// 10.0
Double.minimumMagnitude(10.0, .nan)
// 10.0
Double.minimumMagnitude(.nan, 25.0)
// 25.0
Double.minimumMagnitude(.nan, .nan)
// nan
The minimumMagnitude
method implements the minNumMag
operation
defined by the IEEE 754 specification.
Parameters:
x: A floatingpoint value.
y: Another floatingpoint value.
Returns: Whichever of x
or y
has lesser magnitude, or whichever is
a number if the other is NaN.
Declaration
static func minimumMagnitude(_ x: Float80, _ y: Float80) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Instance Methods
Adds the product of the two given values to this value in place, computed without intermediate rounding.
Parameters: lhs: One of the values to multiply before adding to this value. rhs: The other value to multiply.
Declaration
mutating func addProduct(_ lhs: Float80, _ rhs: Float80)
Returns the result of adding the product of the two given values to this value, computed without intermediate rounding.
This method is equivalent to the C fma
function and implements the
fusedMultiplyAdd
operation defined by the IEEE 754
specification.
Parameters:
lhs: One of the values to multiply before adding to this value.
rhs: The other value to multiply.
Returns: The product of lhs
and rhs
, added to this value.
Declaration
func addingProduct(_ lhs: Float80, _ rhs: Float80) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Returns a new value advanced by the given distance.
For two values x
and d
, the result of a x.advanced(by: d)
is equal
to x + d
a new value y
such that x.distance(to: y)
approximates
d
. For example:
let x = 21.5
let y = x.advanced(by: 6.5)
// y == 15.0
print(x.distance(to: y))
// Prints "6.5"
amount
: The distance to advance this value.
Returns: A new value that is amount
added to this value.
Declaration
func advanced(by amount: Float80) > Float80
Returns the distance from this value to the specified value.
For two values x
and y
, the result of x.distance(to: y)
is equal to
y  x
a distance d
such that x.advanced(by: d)
approximates y
.
For example:
let x = 21.5
let d = x.distance(to: 15.0)
// d == 6.5
print(x.advanced(by: d))
// Prints "15.0"
other
: A value to calculate the distance to.
Returns: The distance between this value and other
.
Declaration
func distance(to other: Float80) > Float80
Replaces this value with the remainder of itself divided by the given value.
For two finite values x
and y
, the remainder r
of dividing x
by
y
satisfies x == y * q + r
, where q
is the integer nearest to
x / y
. If x / y
is exactly halfway between two integers, q
is
chosen to be even. Note that q
is not x / y
computed in
floatingpoint arithmetic, and that q
may not be representable in any
available integer type.
The following example calculates the remainder of dividing 8.625 by 0.75:
var x = 8.625
print(x / 0.75)
// Prints "11.5"
let q = (x / 0.75).rounded(.toNearestOrEven)
// q == 12.0
x.formRemainder(dividingBy: 0.75)
// x == 0.375
let x1 = 0.75 * q + x
// x1 == 8.625
If this value and other
are finite numbers, the remainder is in the
closed range abs(other / 2)...abs(other / 2)
. The
formRemainder(dividingBy:)
method is always exact.
other
: The value to use when dividing this value.
Declaration
mutating func formRemainder(dividingBy other: Float80)
Replaces this value with its square root, rounded to a representable value.
Declaration
mutating func formSquareRoot()
Replaces this value with the remainder of itself divided by the given value using truncating division.
Performing truncating division with floatingpoint values results in a
truncated integer quotient and a remainder. For values x
and y
and
their truncated integer quotient q
, the remainder r
satisfies
x == y * q + r
.
The following example calculates the truncating remainder of dividing 8.625 by 0.75:
var x = 8.625
print(x / 0.75)
// Prints "11.5"
let q = (x / 0.75).rounded(.towardZero)
// q == 11.0
x.formTruncatingRemainder(dividingBy: 0.75)
// x == 0.375
let x1 = 0.75 * q + x
// x1 == 8.625
If this value and other
are both finite numbers, the truncating
remainder has the same sign as this value and is strictly smaller in
magnitude than other
. The formTruncatingRemainder(dividingBy:)
method is always exact.
other
: The value to use when dividing this value.
Declaration
mutating func formTruncatingRemainder(dividingBy other: Float80)
Hashes the essential components of this value by feeding them into the given hasher.
hasher
: The hasher to use when combining the components
of this instance.
Declaration
func hash(into hasher: inout Hasher)
Returns a Boolean value indicating whether this instance is equal to the given value.
This method serves as the basis for the equalto operator (==
) for
floatingpoint values. When comparing two values with this method, 0
is equal to +0
. NaN is not equal to any value, including itself. For
example:
let x = 15.0
x.isEqual(to: 15.0)
// true
x.isEqual(to: .nan)
// false
Double.nan.isEqual(to: .nan)
// false
The isEqual(to:)
method implements the equality predicate defined by
the IEEE 754 specification.
other
: The value to compare with this value.
Returns: true
if other
has the same value as this instance;
otherwise, false
.
Declaration
func isEqual(to other: Float80) > Bool
Returns a Boolean value indicating whether this instance is less than the given value.
This method serves as the basis for the lessthan operator (<
) for
floatingpoint values. Some special cases apply:
 Because NaN compares not less than nor greater than any value, this
method returns
false
when called on NaN or when NaN is passed asother
. infinity
compares less than all values except for itself and NaN.
Every value except for NaN and
+infinity
compares less than+infinity
.let x = 15.0 x.isLess(than: 20.0) // true x.isLess(than: .nan) // false Double.nan.isLess(than: x) // false
The isLess(than:)
method implements the lessthan predicate defined by
the IEEE 754 specification.
other
: The value to compare with this value.
Returns: true
if other
is less than this value; otherwise, false
.
Declaration
func isLess(than other: Float80) > Bool
Returns a Boolean value indicating whether this instance is less than or equal to the given value.
This method serves as the basis for the lessthanorequalto operator
(<=
) for floatingpoint values. Some special cases apply:
 Because NaN is incomparable with any value, this method returns
false
when called on NaN or when NaN is passed asother
. infinity
compares less than or equal to all values except NaN.
Every value except NaN compares less than or equal to
+infinity
.let x = 15.0 x.isLessThanOrEqualTo(20.0) // true x.isLessThanOrEqualTo(.nan) // false Double.nan.isLessThanOrEqualTo(x) // false
The isLessThanOrEqualTo(_:)
method implements the lessthanorequal
predicate defined by the IEEE 754 specification.
other
: The value to compare with this value.
Returns: true
if other
is less than this value; otherwise, false
.
Declaration
func isLessThanOrEqualTo(_ other: Float80) > Bool
Returns a Boolean value indicating whether this instance should precede or tie positions with the given value in an ascending sort.
This relation is a refinement of the lessthanorequalto operator
(<=
) that provides a total order on all values of the type, including
signed zeros and NaNs.
The following example uses isTotallyOrdered(belowOrEqualTo:)
to sort an
array of floatingpoint values, including some that are NaN:
var numbers = [2.5, 21.25, 3.0, .nan, 9.5]
numbers.sort { !$1.isTotallyOrdered(belowOrEqualTo: $0) }
// numbers == [9.5, 2.5, 3.0, 21.25, NaN]
The isTotallyOrdered(belowOrEqualTo:)
method implements the total order
relation as defined by the IEEE 754 specification.
other
: A floatingpoint value to compare to this value.
Returns: true
if this value is ordered below or the same as other
in a total ordering of the floatingpoint type; otherwise, false
.
Declaration
func isTotallyOrdered(belowOrEqualTo other: Float80) > Bool
Declared In
BinaryFloatingPoint
Replaces this value with its additive inverse.
The result is always exact. This example uses the negate()
method to
negate the value of the variable x
:
var x = 21.5
x.negate()
// x == 21.5
Declaration
mutating func negate()
Declared In
Float80
, BinaryFloatingPoint
, SignedNumeric
Returns the remainder of this value divided by the given value.
For two finite values x
and y
, the remainder r
of dividing x
by
y
satisfies x == y * q + r
, where q
is the integer nearest to
x / y
. If x / y
is exactly halfway between two integers, q
is
chosen to be even. Note that q
is not x / y
computed in
floatingpoint arithmetic, and that q
may not be representable in any
available integer type.
The following example calculates the remainder of dividing 8.625 by 0.75:
let x = 8.625
print(x / 0.75)
// Prints "11.5"
let q = (x / 0.75).rounded(.toNearestOrEven)
// q == 12.0
let r = x.remainder(dividingBy: 0.75)
// r == 0.375
let x1 = 0.75 * q + r
// x1 == 8.625
If this value and other
are finite numbers, the remainder is in the
closed range abs(other / 2)...abs(other / 2)
. The
remainder(dividingBy:)
method is always exact. This method implements
the remainder operation defined by the IEEE 754 specification.
other
: The value to use when dividing this value.
Returns: The remainder of this value divided by other
.
Declaration
func remainder(dividingBy other: Float80) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Rounds this value to an integral value using "schoolbook rounding."
The round()
method uses the .toNearestOrAwayFromZero
rounding rule,
where a value halfway between two integral values is rounded to the one
with greater magnitude. The following example rounds several values
using this default rule:
var x = 5.2
x.round()
// x == 5.0
var y = 5.5
y.round()
// y == 6.0
var z = 5.5
z.round()
// z == 6.0
To specify an alternative rule for rounding, use the round(_:)
method
instead.
Declaration
mutating func round()
Declared In
BinaryFloatingPoint
, FloatingPoint
Rounds the value to an integral value using the specified rounding rule.
The following example rounds a value using four different rounding rules:
// Equivalent to the C 'round' function:
var w = 6.5
w.round(.toNearestOrAwayFromZero)
// w == 7.0
// Equivalent to the C 'trunc' function:
var x = 6.5
x.round(.towardZero)
// x == 6.0
// Equivalent to the C 'ceil' function:
var y = 6.5
y.round(.up)
// y == 7.0
// Equivalent to the C 'floor' function:
var z = 6.5
z.round(.down)
// z == 6.0
For more information about the available rounding rules, see the
FloatingPointRoundingRule
enumeration. To round a value using the
default "schoolbook rounding", you can use the shorter round()
method
instead.
var w1 = 6.5
w1.round()
// w1 == 7.0
rule
: The rounding rule to use.
Declaration
mutating func round(_ rule: FloatingPointRoundingRule)
Returns this value rounded to an integral value using "schoolbook rounding."
The rounded()
method uses the .toNearestOrAwayFromZero
rounding rule,
where a value halfway between two integral values is rounded to the one
with greater magnitude. The following example rounds several values
using this default rule:
(5.2).rounded()
// 5.0
(5.5).rounded()
// 6.0
(5.2).rounded()
// 5.0
(5.5).rounded()
// 6.0
To specify an alternative rule for rounding, use the rounded(_:)
method
instead.
Returns: The nearest integral value, or, if two integral values are equally close, the integral value with greater magnitude.
Declaration
func rounded() > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Returns this value rounded to an integral value using the specified rounding rule.
The following example rounds a value using four different rounding rules:
let x = 6.5
// Equivalent to the C 'round' function:
print(x.rounded(.toNearestOrAwayFromZero))
// Prints "7.0"
// Equivalent to the C 'trunc' function:
print(x.rounded(.towardZero))
// Prints "6.0"
// Equivalent to the C 'ceil' function:
print(x.rounded(.up))
// Prints "7.0"
// Equivalent to the C 'floor' function:
print(x.rounded(.down))
// Prints "6.0"
For more information about the available rounding rules, see the
FloatingPointRoundingRule
enumeration. To round a value using the
default "schoolbook rounding", you can use the shorter rounded()
method instead.
print(x.rounded())
// Prints "7.0"
rule
: The rounding rule to use.
Returns: The integral value found by rounding using rule
.
Declaration
func rounded(_ rule: FloatingPointRoundingRule) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Returns the square root of the value, rounded to a representable value.
The following example declares a function that calculates the length of the hypotenuse of a right triangle given its two perpendicular sides.
func hypotenuse(_ a: Double, _ b: Double) > Double {
return (a * a + b * b).squareRoot()
}
let (dx, dy) = (3.0, 4.0)
let distance = hypotenuse(dx, dy)
// distance == 5.0
Returns: The square root of the value.
Declaration
func squareRoot() > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Returns the remainder of this value divided by the given value using truncating division.
Performing truncating division with floatingpoint values results in a
truncated integer quotient and a remainder. For values x
and y
and
their truncated integer quotient q
, the remainder r
satisfies
x == y * q + r
.
The following example calculates the truncating remainder of dividing 8.625 by 0.75:
let x = 8.625
print(x / 0.75)
// Prints "11.5"
let q = (x / 0.75).rounded(.towardZero)
// q == 11.0
let r = x.truncatingRemainder(dividingBy: 0.75)
// r == 0.375
let x1 = 0.75 * q + r
// x1 == 8.625
If this value and other
are both finite numbers, the truncating
remainder has the same sign as this value and is strictly smaller in
magnitude than other
. The truncatingRemainder(dividingBy:)
method
is always exact.
other
: The value to use when dividing this value.
Returns: The remainder of this value divided by other
using
truncating division.
Declaration
func truncatingRemainder(dividingBy other: Float80) > Float80
Declared In
BinaryFloatingPoint
, FloatingPoint
Conditionally Inherited Items
The initializers, methods, and properties listed below may be available on this type under certain conditions (such as methods that are available on Array
when its elements are Equatable
) or may not ever be available if that determination is beyond SwiftDoc.org's capabilities. Please open an issue on GitHub if you see something out of place!
Where RawSignificand : FixedWidthInteger, RawSignificand.Magnitude : UnsignedInteger, RawSignificand.Stride : SignedInteger
Returns a random value within the specified range.
Use this method to generate a floatingpoint value within a specific
range. This example creates three new values in the range
10.0...20.0
.
for _ in 1...3 {
print(Double.random(in: 10.0...20.0))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"
The random()
static method chooses a random value from a continuous
uniform distribution in range
, and then converts that value to the
nearest representable value in this type. Depending on the size and span
of range
, some concrete values may be represented more frequently than
others.
This method uses the default random generator, Random.default
. The call
to Double.random(in: 10.0...20.0)
above is equivalent to calling
Double.random(in: 10.0...20.0, using: &Random.default)
.
range
: The range in which to create a random value.
Returns: A random value within the bounds of range
.
Declaration
static func random(in range: ClosedRange<Float80>) > Float80
Declared In
BinaryFloatingPoint
Returns a random value within the specified range.
Use this method to generate a floatingpoint value within a specific
range. This example creates three new values in the range
10.0..<20.0
.
for _ in 1...3 {
print(Double.random(in: 10.0..<20.0))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"
The random()
static method chooses a random value from a continuous
uniform distribution in range
, and then converts that value to the
nearest representable value in this type. Depending on the size and span
of range
, some concrete values may be represented more frequently than
others.
This method uses the default random generator, Random.default
. The call
to Double.random(in: 10.0..<20.0)
above is equivalent to calling
Double.random(in: 10.0..<20.0, using: &Random.default)
.
range
: The range in which to create a random value.
range
must not be empty.
Returns: A random value within the bounds of range
.
Declaration
static func random(in range: Range<Float80>) > Float80
Declared In
BinaryFloatingPoint
Returns a random value within the specified range, using the given generator as a source for randomness.
Use this method to generate a floatingpoint value within a specific
range when you are using a custom random number generator. This example
creates three new values in the range 10.0...20.0
.
for _ in 1...3 {
print(Double.random(in: 10.0...20.0, using: &myGenerator))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"
The random(in:using:)
static method chooses a random value from a
continuous uniform distribution in range
, and then converts that value
to the nearest representable value in this type. Depending on the size and
span of range
, some concrete values may be represented more frequently
than others.
Parameters:
range: The range in which to create a random value.
generator: The random number generator to use when creating the
new random value.
Returns: A random value within the bounds of range
.
Declaration
static func random<T>(in range: ClosedRange<Float80>, using generator: inout T) > Float80 where T : RandomNumberGenerator
Declared In
BinaryFloatingPoint
Returns a random value within the specified range, using the given generator as a source for randomness.
Use this method to generate a floatingpoint value within a specific
range when you are using a custom random number generator. This example
creates three new values in the range 10.0..<20.0
.
for _ in 1...3 {
print(Double.random(in: 10.0..<20.0, using: &myGenerator))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"
The random(in:using:)
static method chooses a random value from a
continuous uniform distribution in range
, and then converts that value
to the nearest representable value in this type. Depending on the size and
span of range
, some concrete values may be represented more frequently
than others.
Parameters:
range: The range in which to create a random value.
range
must not be empty.
generator: The random number generator to use when creating the
new random value.
Returns: A random value within the bounds of range
.
Declaration
static func random<T>(in range: Range<Float80>, using generator: inout T) > Float80 where T : RandomNumberGenerator
Declared In
BinaryFloatingPoint
Where Stride : SignedInteger
Returns a countable closed range that contains both of its bounds.
Use the closed range operator (...
) to create a closed range of any type
that conforms to the Strideable
protocol with an associated signed
integer Stride
type, such as any of the standard library's integer
types. This example creates a ClosedRange<Int>
from zero up to,
and including, nine.
let singleDigits = 0...9
print(singleDigits.contains(9))
// Prints "true"
You can use sequence or collection methods on the singleDigits
range.
print(singleDigits.count)
// Prints "10"
print(singleDigits.last)
// Prints "9"
Parameters:)`. minimum: The lower bound for the range. maximum: The upper bound for the range.
Declaration
func ...(minimum: Float80, maximum: Float80) > ClosedRange<Float80>
Declared In
BinaryFloatingPoint
, Strideable
An extendedprecision, floatingpoint value type.