Files
swift-numerics/Sources/IntegerUtilities/RoundingRule.swift
T

69 lines
2.9 KiB
Swift

//===--- RoundingRule.swift -----------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// A rule that defines how to select one of the two representable results
/// closest to a given value.
///
/// [Wikipedia](https://en.wikipedia.org/wiki/Rounding#Rounding_to_integer)
/// provides a good overview of different rounding rules.
public enum RoundingRule {
/// Produces the closest representable value that is less than or equal
/// to the value being rounded.
///
/// This is the default rounding mode for integer shifts, including the
/// shift operators defined in the standard library.
case down
/// Produces the closest representable value that is greater than or equal
/// to the value being rounded.
case up
/// Produces the closest representable value whose magnitude is less than
/// or equal to that of the value being rounded.
case towardZero
/// Produces the closest representable value whose magnitude is greater than
/// or equal to that of the value being rounded.
case awayFromZero
/// If the value being rounded is representable, that value is returned.
/// Otherwise, whichever of the two closest representable values has its
/// least significant bit set is returned.
///
/// This is also called _sticky rounding_, and it is useful as an
/// implementation detail because it has the property that if we do
/// rounding in two steps, first to intermediate precision p₁ with .toOdd,
/// then to the final precision p₂ with any other rounding mode, the result
/// we get is the same as if we rounded directly to p₂ in the desired mode
/// so long as p₂ + 1 < p₁. Other rounding modes do not have this property,
/// and admit _double roundings_ when interoperating with some modes.
case toOdd
/// Produces the representable value that is closest to the value being
/// rounded. If two values are equally close, the one that has greater
/// magnitude is returned.
case toNearestOrAwayFromZero
/// Produces the representable value that is closest to the value being
/// rounded. If two values are equally close, the one whose least
/// significant bit is not set is returned.
case toNearestOrEven
/// Adds a uniform random value from [0, d) to the value being rounded,
/// where d is the distance between the two closest representable values,
/// then rounds the sum downwards.
case stochastically
/// If the value being rounded is representable, that value is returned.
/// Otherwise, a precondition failure occurs.
case requireExact
}