Skip to content

Commit beeb816

Browse files
committed
[stdlib] Manually propagate floating point documentation
1 parent 311e1dc commit beeb816

File tree

2 files changed

+883
-1
lines changed

2 files changed

+883
-1
lines changed

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,10 +1596,48 @@ public protocol BinaryFloatingPoint: FloatingPoint, ExpressibleByFloatLiteral {
15961596

15971597
extension FloatingPoint {
15981598

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.
15991604
public static var ulpOfOne: Self {
16001605
return Self(1).ulp
16011606
}
16021607

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`.
16031641
@_transparent
16041642
public func rounded(_ rule: FloatingPointRoundingRule) -> Self {
16051643
var lhs = self
@@ -1658,39 +1696,166 @@ extension FloatingPoint {
16581696
round(.toNearestOrAwayFromZero)
16591697
}
16601698

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`.
16611709
@_transparent
16621710
public var nextDown: Self {
16631711
return -(-self).nextUp
16641712
}
16651713

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.
16661745
@_transparent
16671746
public func truncatingRemainder(dividingBy other: Self) -> Self {
16681747
var lhs = self
16691748
lhs.formTruncatingRemainder(dividingBy: other)
16701749
return lhs
16711750
}
16721751

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`.
16731784
@_transparent
16741785
public func remainder(dividingBy other: Self) -> Self {
16751786
var lhs = self
16761787
lhs.formRemainder(dividingBy: other)
16771788
return lhs
16781789
}
16791790

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.
16801805
@_transparent
16811806
public func squareRoot( ) -> Self {
16821807
var lhs = self
16831808
lhs.formSquareRoot( )
16841809
return lhs
16851810
}
16861811

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.
16871825
@_transparent
16881826
public func addingProduct(_ lhs: Self, _ rhs: Self) -> Self {
16891827
var addend = self
16901828
addend.addProduct(lhs, rhs)
16911829
return addend
16921830
}
16931831

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.
16941859
public static func minimum(_ x: Self, _ y: Self) -> Self {
16951860
if x.isSignalingNaN || y.isSignalingNaN {
16961861
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1700,6 +1865,33 @@ extension FloatingPoint {
17001865
return y
17011866
}
17021867

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.
17031895
public static func maximum(_ x: Self, _ y: Self) -> Self {
17041896
if x.isSignalingNaN || y.isSignalingNaN {
17051897
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1709,6 +1901,35 @@ extension FloatingPoint {
17091901
return y
17101902
}
17111903

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.
17121933
public static func minimumMagnitude(_ x: Self, _ y: Self) -> Self {
17131934
if x.isSignalingNaN || y.isSignalingNaN {
17141935
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1718,6 +1939,35 @@ extension FloatingPoint {
17181939
return y
17191940
}
17201941

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.
17211971
public static func maximumMagnitude(_ x: Self, _ y: Self) -> Self {
17221972
if x.isSignalingNaN || y.isSignalingNaN {
17231973
// Produce a quiet NaN matching platform arithmetic behavior.
@@ -1727,6 +1977,12 @@ extension FloatingPoint {
17271977
return y
17281978
}
17291979

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
17301986
public var floatingPointClass: FloatingPointClassification {
17311987
if isSignalingNaN { return .signalingNaN }
17321988
if isNaN { return .quietNaN }
@@ -1748,12 +2004,55 @@ extension BinaryFloatingPoint {
17482004
/// let magnitude = x.significand * F.radix ** x.exponent
17492005
public static var radix: Int { return 2 }
17502006

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`.
17512026
public init(signOf: Self, magnitudeOf: Self) {
17522027
self.init(sign: signOf.sign,
17532028
exponentBitPattern: magnitudeOf.exponentBitPattern,
17542029
significandBitPattern: magnitudeOf.significandBitPattern)
17552030
}
17562031

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`.
17572056
public func isTotallyOrdered(belowOrEqualTo other: Self) -> Bool {
17582057
// Quick return when possible.
17592058
if self < other { return true }

0 commit comments

Comments
 (0)