From 92eb76200d034cd0e3e536493eee30e8f5427d58 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Sun, 26 Sep 2021 21:43:02 -0400 Subject: [PATCH] Cursory documentation for shifted(right:). --- Sources/IntegerUtilities/Rounding.swift | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Sources/IntegerUtilities/Rounding.swift b/Sources/IntegerUtilities/Rounding.swift index e037db1..b455568 100644 --- a/Sources/IntegerUtilities/Rounding.swift +++ b/Sources/IntegerUtilities/Rounding.swift @@ -65,6 +65,32 @@ public enum RoundingRule { } extension BinaryInteger { + /// self / 2^(count), rounded the results according to the specified rule. + /// + /// The default rounding rule is `.down`, which matches the behavior of + /// the `>>` operator from the standard library. + /// + /// Some examples of different rounding rules: + /// + /// // 3/2 is 1.5, which rounds (down by default) to 1. + /// 3.shifted(right: 1) + /// + /// // 1.5 rounds up to 2. + /// 3.shifted(right: 1, rounding: .up) + /// + /// // The two closest values are 1 and 2, 1 is returned because it + /// // is odd. + /// 3.shifted(right: 1, rounding: .toOdd) + /// + /// // 7/2^2 = 1.75, so the result is 1 with probability 1/4, and 2 + /// // with probability 3/4. + /// 7.shifted(right: 2, rounding: .stochastic) + /// + /// // 4/2^2 = 4/4 = 1, exactly. + /// 4.shifted(right: 2, rounding: .trap) + /// + /// // 5/2 is 2.5, which is not exact, so this traps. + /// 5.shifted(right: 1, rounding: .trap) public func shifted( right count: Count, rounding rule: RoundingRule = .down