@@ -1596,10 +1596,48 @@ public protocol BinaryFloatingPoint: FloatingPoint, ExpressibleByFloatLiteral {
1596
1596
1597
1597
extension FloatingPoint {
1598
1598
1599
+ /// The unit in the last place of 1.0.
1600
+ ///
1601
+ /// The positive difference between 1.0 and the next greater representable
1602
+ /// number. The `ulpOfOne` constant corresponds to the C macros
1603
+ /// `FLT_EPSILON`, `DBL_EPSILON`, and others with a similar purpose.
1599
1604
public static var ulpOfOne : Self {
1600
1605
return Self ( 1 ) . ulp
1601
1606
}
1602
1607
1608
+ /// Returns this value rounded to an integral value using the specified
1609
+ /// rounding rule.
1610
+ ///
1611
+ /// The following example rounds a value using four different rounding rules:
1612
+ ///
1613
+ /// let x = 6.5
1614
+ ///
1615
+ /// // Equivalent to the C 'round' function:
1616
+ /// print(x.rounded(.toNearestOrAwayFromZero))
1617
+ /// // Prints "7.0"
1618
+ ///
1619
+ /// // Equivalent to the C 'trunc' function:
1620
+ /// print(x.rounded(.towardZero))
1621
+ /// // Prints "6.0"
1622
+ ///
1623
+ /// // Equivalent to the C 'ceil' function:
1624
+ /// print(x.rounded(.up))
1625
+ /// // Prints "7.0"
1626
+ ///
1627
+ /// // Equivalent to the C 'floor' function:
1628
+ /// print(x.rounded(.down))
1629
+ /// // Prints "6.0"
1630
+ ///
1631
+ /// For more information about the available rounding rules, see the
1632
+ /// `FloatingPointRoundingRule` enumeration. To round a value using the
1633
+ /// default "schoolbook rounding", you can use the shorter `rounded()`
1634
+ /// method instead.
1635
+ ///
1636
+ /// print(x.rounded())
1637
+ /// // Prints "7.0"
1638
+ ///
1639
+ /// - Parameter rule: The rounding rule to use.
1640
+ /// - Returns: The integral value found by rounding using `rule`.
1603
1641
@_transparent
1604
1642
public func rounded( _ rule: FloatingPointRoundingRule ) -> Self {
1605
1643
var lhs = self
@@ -1658,39 +1696,166 @@ extension FloatingPoint {
1658
1696
round ( . toNearestOrAwayFromZero)
1659
1697
}
1660
1698
1699
+ /// The greatest representable value that compares less than this value.
1700
+ ///
1701
+ /// For any finite value `x`, `x.nextDown` is less than `x`. For `nan` or
1702
+ /// `-infinity`, `x.nextDown` is `x` itself. The following special cases
1703
+ /// also apply:
1704
+ ///
1705
+ /// - If `x` is `infinity`, then `x.nextDown` is `greatestFiniteMagnitude`.
1706
+ /// - If `x` is `leastNonzeroMagnitude`, then `x.nextDown` is `0.0`.
1707
+ /// - If `x` is zero, then `x.nextDown` is `-leastNonzeroMagnitude`.
1708
+ /// - If `x` is `-greatestFiniteMagnitude`, then `x.nextDown` is `-infinity`.
1661
1709
@_transparent
1662
1710
public var nextDown : Self {
1663
1711
return - ( - self ) . nextUp
1664
1712
}
1665
1713
1714
+ /// Returns the remainder of this value divided by the given value using
1715
+ /// truncating division.
1716
+ ///
1717
+ /// Performing truncating division with floating-point values results in a
1718
+ /// truncated integer quotient and a remainder. For values `x` and `y` and
1719
+ /// their truncated integer quotient `q`, the remainder `r` satisfies
1720
+ /// `x == y * q + r`.
1721
+ ///
1722
+ /// The following example calculates the truncating remainder of dividing
1723
+ /// 8.625 by 0.75:
1724
+ ///
1725
+ /// let x = 8.625
1726
+ /// print(x / 0.75)
1727
+ /// // Prints "11.5"
1728
+ ///
1729
+ /// let q = (x / 0.75).rounded(.towardZero)
1730
+ /// // q == 11.0
1731
+ /// let r = x.truncatingRemainder(dividingBy: 0.75)
1732
+ /// // r == 0.375
1733
+ ///
1734
+ /// let x1 = 0.75 * q + r
1735
+ /// // x1 == 8.625
1736
+ ///
1737
+ /// If this value and `other` are both finite numbers, the truncating
1738
+ /// remainder has the same sign as this value and is strictly smaller in
1739
+ /// magnitude than `other`. The `truncatingRemainder(dividingBy:)` method
1740
+ /// is always exact.
1741
+ ///
1742
+ /// - Parameter other: The value to use when dividing this value.
1743
+ /// - Returns: The remainder of this value divided by `other` using
1744
+ /// truncating division.
1666
1745
@_transparent
1667
1746
public func truncatingRemainder( dividingBy other: Self ) -> Self {
1668
1747
var lhs = self
1669
1748
lhs. formTruncatingRemainder ( dividingBy: other)
1670
1749
return lhs
1671
1750
}
1672
1751
1752
+ /// Returns the remainder of this value divided by the given value.
1753
+ ///
1754
+ /// For two finite values `x` and `y`, the remainder `r` of dividing `x` by
1755
+ /// `y` satisfies `x == y * q + r`, where `q` is the integer nearest to
1756
+ /// `x / y`. If `x / y` is exactly halfway between two integers, `q` is
1757
+ /// chosen to be even. Note that `q` is *not* `x / y` computed in
1758
+ /// floating-point arithmetic, and that `q` may not be representable in any
1759
+ /// available integer type.
1760
+ ///
1761
+ /// The following example calculates the remainder of dividing 8.625 by 0.75:
1762
+ ///
1763
+ /// let x = 8.625
1764
+ /// print(x / 0.75)
1765
+ /// // Prints "11.5"
1766
+ ///
1767
+ /// let q = (x / 0.75).rounded(.toNearestOrEven)
1768
+ /// // q == 12.0
1769
+ /// let r = x.remainder(dividingBy: 0.75)
1770
+ /// // r == -0.375
1771
+ ///
1772
+ /// let x1 = 0.75 * q + r
1773
+ /// // x1 == 8.625
1774
+ ///
1775
+ /// If this value and `other` are finite numbers, the remainder is in the
1776
+ /// closed range `-abs(other / 2)...abs(other / 2)`. The
1777
+ /// `remainder(dividingBy:)` method is always exact. This method implements
1778
+ /// the remainder operation defined by the [IEEE 754 specification][spec].
1779
+ ///
1780
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
1781
+ ///
1782
+ /// - Parameter other: The value to use when dividing this value.
1783
+ /// - Returns: The remainder of this value divided by `other`.
1673
1784
@_transparent
1674
1785
public func remainder( dividingBy other: Self ) -> Self {
1675
1786
var lhs = self
1676
1787
lhs. formRemainder ( dividingBy: other)
1677
1788
return lhs
1678
1789
}
1679
1790
1791
+ /// Returns the square root of the value, rounded to a representable value.
1792
+ ///
1793
+ /// The following example declares a function that calculates the length of
1794
+ /// the hypotenuse of a right triangle given its two perpendicular sides.
1795
+ ///
1796
+ /// func hypotenuse(_ a: Double, _ b: Double) -> Double {
1797
+ /// return (a * a + b * b).squareRoot()
1798
+ /// }
1799
+ ///
1800
+ /// let (dx, dy) = (3.0, 4.0)
1801
+ /// let distance = hypotenuse(dx, dy)
1802
+ /// // distance == 5.0
1803
+ ///
1804
+ /// - Returns: The square root of the value.
1680
1805
@_transparent
1681
1806
public func squareRoot( ) -> Self {
1682
1807
var lhs = self
1683
1808
lhs. formSquareRoot ( )
1684
1809
return lhs
1685
1810
}
1686
1811
1812
+ /// Returns the result of adding the product of the two given values to this
1813
+ /// value, computed without intermediate rounding.
1814
+ ///
1815
+ /// This method is equivalent to the C `fma` function and implements the
1816
+ /// `fusedMultiplyAdd` operation defined by the [IEEE 754
1817
+ /// specification][spec].
1818
+ ///
1819
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
1820
+ ///
1821
+ /// - Parameters:
1822
+ /// - lhs: One of the values to multiply before adding to this value.
1823
+ /// - rhs: The other value to multiply.
1824
+ /// - Returns: The product of `lhs` and `rhs`, added to this value.
1687
1825
@_transparent
1688
1826
public func addingProduct( _ lhs: Self , _ rhs: Self ) -> Self {
1689
1827
var addend = self
1690
1828
addend. addProduct ( lhs, rhs)
1691
1829
return addend
1692
1830
}
1693
1831
1832
+ /// Returns the lesser of the two given values.
1833
+ ///
1834
+ /// This method returns the minimum of two values, preserving order and
1835
+ /// eliminating NaN when possible. For two values `x` and `y`, the result of
1836
+ /// `minimum(x, y)` is `x` if `x <= y`, `y` if `y < x`, or whichever of `x`
1837
+ /// or `y` is a number if the other is a quiet NaN. If both `x` and `y` are
1838
+ /// NaN, or either `x` or `y` is a signaling NaN, the result is NaN.
1839
+ ///
1840
+ /// Double.minimum(10.0, -25.0)
1841
+ /// // -25.0
1842
+ /// Double.minimum(10.0, .nan)
1843
+ /// // 10.0
1844
+ /// Double.minimum(.nan, -25.0)
1845
+ /// // -25.0
1846
+ /// Double.minimum(.nan, .nan)
1847
+ /// // nan
1848
+ ///
1849
+ /// The `minimum` method implements the `minNum` operation defined by the
1850
+ /// [IEEE 754 specification][spec].
1851
+ ///
1852
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
1853
+ ///
1854
+ /// - Parameters:
1855
+ /// - x: A floating-point value.
1856
+ /// - y: Another floating-point value.
1857
+ /// - Returns: The minimum of `x` and `y`, or whichever is a number if the
1858
+ /// other is NaN.
1694
1859
public static func minimum( _ x: Self , _ y: Self ) -> Self {
1695
1860
if x. isSignalingNaN || y. isSignalingNaN {
1696
1861
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1700,6 +1865,33 @@ extension FloatingPoint {
1700
1865
return y
1701
1866
}
1702
1867
1868
+ /// Returns the greater of the two given values.
1869
+ ///
1870
+ /// This method returns the maximum of two values, preserving order and
1871
+ /// eliminating NaN when possible. For two values `x` and `y`, the result of
1872
+ /// `maximum(x, y)` is `x` if `x > y`, `y` if `x <= y`, or whichever of `x`
1873
+ /// or `y` is a number if the other is a quiet NaN. If both `x` and `y` are
1874
+ /// NaN, or either `x` or `y` is a signaling NaN, the result is NaN.
1875
+ ///
1876
+ /// Double.maximum(10.0, -25.0)
1877
+ /// // 10.0
1878
+ /// Double.maximum(10.0, .nan)
1879
+ /// // 10.0
1880
+ /// Double.maximum(.nan, -25.0)
1881
+ /// // -25.0
1882
+ /// Double.maximum(.nan, .nan)
1883
+ /// // nan
1884
+ ///
1885
+ /// The `maximum` method implements the `maxNum` operation defined by the
1886
+ /// [IEEE 754 specification][spec].
1887
+ ///
1888
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
1889
+ ///
1890
+ /// - Parameters:
1891
+ /// - x: A floating-point value.
1892
+ /// - y: Another floating-point value.
1893
+ /// - Returns: The greater of `x` and `y`, or whichever is a number if the
1894
+ /// other is NaN.
1703
1895
public static func maximum( _ x: Self , _ y: Self ) -> Self {
1704
1896
if x. isSignalingNaN || y. isSignalingNaN {
1705
1897
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1709,6 +1901,35 @@ extension FloatingPoint {
1709
1901
return y
1710
1902
}
1711
1903
1904
+ /// Returns the value with lesser magnitude.
1905
+ ///
1906
+ /// This method returns the value with lesser magnitude of the two given
1907
+ /// values, preserving order and eliminating NaN when possible. For two
1908
+ /// values `x` and `y`, the result of `minimumMagnitude(x, y)` is `x` if
1909
+ /// `x.magnitude <= y.magnitude`, `y` if `y.magnitude < x.magnitude`, or
1910
+ /// whichever of `x` or `y` is a number if the other is a quiet NaN. If both
1911
+ /// `x` and `y` are NaN, or either `x` or `y` is a signaling NaN, the result
1912
+ /// is NaN.
1913
+ ///
1914
+ /// Double.minimumMagnitude(10.0, -25.0)
1915
+ /// // 10.0
1916
+ /// Double.minimumMagnitude(10.0, .nan)
1917
+ /// // 10.0
1918
+ /// Double.minimumMagnitude(.nan, -25.0)
1919
+ /// // -25.0
1920
+ /// Double.minimumMagnitude(.nan, .nan)
1921
+ /// // nan
1922
+ ///
1923
+ /// The `minimumMagnitude` method implements the `minNumMag` operation
1924
+ /// defined by the [IEEE 754 specification][spec].
1925
+ ///
1926
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
1927
+ ///
1928
+ /// - Parameters:
1929
+ /// - x: A floating-point value.
1930
+ /// - y: Another floating-point value.
1931
+ /// - Returns: Whichever of `x` or `y` has lesser magnitude, or whichever is
1932
+ /// a number if the other is NaN.
1712
1933
public static func minimumMagnitude( _ x: Self , _ y: Self ) -> Self {
1713
1934
if x. isSignalingNaN || y. isSignalingNaN {
1714
1935
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1718,6 +1939,35 @@ extension FloatingPoint {
1718
1939
return y
1719
1940
}
1720
1941
1942
+ /// Returns the value with greater magnitude.
1943
+ ///
1944
+ /// This method returns the value with greater magnitude of the two given
1945
+ /// values, preserving order and eliminating NaN when possible. For two
1946
+ /// values `x` and `y`, the result of `maximumMagnitude(x, y)` is `x` if
1947
+ /// `x.magnitude > y.magnitude`, `y` if `x.magnitude <= y.magnitude`, or
1948
+ /// whichever of `x` or `y` is a number if the other is a quiet NaN. If both
1949
+ /// `x` and `y` are NaN, or either `x` or `y` is a signaling NaN, the result
1950
+ /// is NaN.
1951
+ ///
1952
+ /// Double.maximumMagnitude(10.0, -25.0)
1953
+ /// // -25.0
1954
+ /// Double.maximumMagnitude(10.0, .nan)
1955
+ /// // 10.0
1956
+ /// Double.maximumMagnitude(.nan, -25.0)
1957
+ /// // -25.0
1958
+ /// Double.maximumMagnitude(.nan, .nan)
1959
+ /// // nan
1960
+ ///
1961
+ /// The `maximumMagnitude` method implements the `maxNumMag` operation
1962
+ /// defined by the [IEEE 754 specification][spec].
1963
+ ///
1964
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
1965
+ ///
1966
+ /// - Parameters:
1967
+ /// - x: A floating-point value.
1968
+ /// - y: Another floating-point value.
1969
+ /// - Returns: Whichever of `x` or `y` has greater magnitude, or whichever is
1970
+ /// a number if the other is NaN.
1721
1971
public static func maximumMagnitude( _ x: Self , _ y: Self ) -> Self {
1722
1972
if x. isSignalingNaN || y. isSignalingNaN {
1723
1973
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1727,6 +1977,12 @@ extension FloatingPoint {
1727
1977
return y
1728
1978
}
1729
1979
1980
+ /// The classification of this value.
1981
+ ///
1982
+ /// A value's `floatingPointClass` property describes its "class" as
1983
+ /// described by the [IEEE 754 specification][spec].
1984
+ ///
1985
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
1730
1986
public var floatingPointClass : FloatingPointClassification {
1731
1987
if isSignalingNaN { return . signalingNaN }
1732
1988
if isNaN { return . quietNaN }
@@ -1748,12 +2004,55 @@ extension BinaryFloatingPoint {
1748
2004
/// let magnitude = x.significand * F.radix ** x.exponent
1749
2005
public static var radix : Int { return 2 }
1750
2006
2007
+ /// Creates a new floating-point value using the sign of one value and the
2008
+ /// magnitude of another.
2009
+ ///
2010
+ /// The following example uses this initializer to create a new `Double`
2011
+ /// instance with the sign of `a` and the magnitude of `b`:
2012
+ ///
2013
+ /// let a = -21.5
2014
+ /// let b = 305.15
2015
+ /// let c = Double(signOf: a, magnitudeOf: b)
2016
+ /// print(c)
2017
+ /// // Prints "-305.15"
2018
+ ///
2019
+ /// This initializer implements the IEEE 754 `copysign` operation.
2020
+ ///
2021
+ /// - Parameters:
2022
+ /// - signOf: A value from which to use the sign. The result of the
2023
+ /// initializer has the same sign as `signOf`.
2024
+ /// - magnitudeOf: A value from which to use the magnitude. The result of
2025
+ /// the initializer has the same magnitude as `magnitudeOf`.
1751
2026
public init ( signOf: Self , magnitudeOf: Self ) {
1752
2027
self . init ( sign: signOf. sign,
1753
2028
exponentBitPattern: magnitudeOf. exponentBitPattern,
1754
2029
significandBitPattern: magnitudeOf. significandBitPattern)
1755
2030
}
1756
2031
2032
+ /// Returns a Boolean value indicating whether this instance should precede the
2033
+ /// given value in an ascending sort.
2034
+ ///
2035
+ /// This relation is a refinement of the less-than-or-equal-to operator
2036
+ /// (`<=`) that provides a total order on all values of the type, including
2037
+ /// noncanonical encodings, signed zeros, and NaNs. Because it is used much
2038
+ /// less frequently than the usual comparisons, there is no operator form of
2039
+ /// this relation.
2040
+ ///
2041
+ /// The following example uses `isTotallyOrdered(below:)` to sort an array of
2042
+ /// floating-point values, including some that are NaN:
2043
+ ///
2044
+ /// var numbers = [2.5, 21.25, 3.0, .nan, -9.5]
2045
+ /// numbers.sort { $0.isTotallyOrdered(below: $1) }
2046
+ /// // numbers == [-9.5, 2.5, 3.0, 21.25, nan]
2047
+ ///
2048
+ /// The `isTotallyOrdered(belowOrEqualTo:)` method implements the total order
2049
+ /// relation as defined by the [IEEE 754 specification][spec].
2050
+ ///
2051
+ /// [spec]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
2052
+ ///
2053
+ /// - Parameter other: A floating-point value to compare to this value.
2054
+ /// - Returns: `true` if this value is ordered below `other` in a total
2055
+ /// ordering of the floating-point type; otherwise, `false`.
1757
2056
public func isTotallyOrdered( belowOrEqualTo other: Self ) -> Bool {
1758
2057
// Quick return when possible.
1759
2058
if self < other { return true }
0 commit comments