A floating-point numeric type.
- Xcode 8.0+
Floating-point types are used to represent fractional numbers, like 5.5, 100.0, or 3.14159274. Each floating-point type has its own possible range and precision. The floating-point types in the standard library are
Float80 where available.
Create new instances of floating-point types using integer or floating-point literals. For example:
Floating protocol declares common arithmetic operations, so you can write functions and algorithms that work on any floating-point type. The following example declares a function that calculates the length of the hypotenuse of a right triangle given its two perpendicular sides. Because the
hypotenuse(_: function uses a generic parameter constrained to the
Floating protocol, you can call it using any floating-point type.
Floating-point values are represented as a sign and a magnitude, where the magnitude is calculated using the type’s radix and the instance’s significand and exponent. This magnitude calculation takes the following form for a floating-point value
x of type
** is exponentiation:
Here’s an example of the number -8.5 represented as an instance of the
Double type, which defines a radix of 2.
Types that conform to the
Floating protocol provide most basic (clause 5) operations of the IEEE 754 specification. The base, precision, and exponent range are not fixed in any way by this protocol, but it enforces the basic requirements of any IEEE 754 floating-point type.
In addition to representing specific numbers, floating-point types also have special values for working with overflow and nonnumeric results of calculation.
Any value whose magnitude is so great that it would round to a value outside the range of representable numbers is rounded to infinity. For a type
F, positive and negative infinity are represented as
-F, respectively. Positive infinity compares greater than every finite value and negative infinity, while negative infinity compares less than every finite value and positive infinity. Infinite values with the same sign are equal to each other.
Operations with infinite values follow real arithmetic as much as possible: Adding or subtracting a finite value, or multiplying or dividing infinity by a nonzero finite value, results in infinity.
NaN (“not a number”)
Floating-point types represent values that are neither finite numbers nor infinity as NaN, an abbreviation for “not a number.” Comparing a NaN with any value, including another NaN, results in
Because testing whether one NaN is equal to another NaN results in
false, use the
is property to test whether a value is NaN.
NaN propagates through many arithmetic operations. When you are operating on many values, this behavior is valuable because operations on NaN simply forward the value and don’t cause runtime errors. The following example shows how NaN values operate in different contexts.
Imagine you have a set of temperature data for which you need to report some general statistics: the total number of observations, the number of valid observations, and the average temperature. First, a set of observations in Celsius is parsed from strings to
Note that some elements in the
temperature array are not valid numbers. When these invalid strings are parsed by the
Double failable initializer, the example uses the nil-coalescing operator (
??) to provide NaN as a fallback value.
Next, the observations in Celsius are converted to Fahrenheit:
The NaN values in the
temps array are propagated through the conversion and remain NaN in
Because calculating the average of the observations involves combining every value of the
temps array, any NaN values cause the result to also be NaN, as seen in this example:
Instead, when you need an operation to have a specific numeric result, filter out any NaN values using the
Finally, report the average temperature and observation counts: