Proposal: https://gist.github.com/karwa/273db66cd8a5fe2c388ccc7de9c4cf31 
<https://gist.github.com/karwa/273db66cd8a5fe2c388ccc7de9c4cf31>

Introduction, Motivation

The standard library lacks equivalents to the floor() and ceil() functions 
found in the standard libraries of most other languages. Currently, we need to 
import Darwin or Glibc in order to access the C standard library versions.

They are essential for many algorithms, and adding them would allow more basic 
algorithms to be written platform-independently (or at least without #if (os) 
flags at the top).

 
<https://gist.github.com/karwa/273db66cd8a5fe2c388ccc7de9c4cf31#proposed-solution>Proposed
 Solution

Add floor and ceiling functions (and mutating variants) to FloatingPoint

protocol FloatingPoint {

    ...

    /// Returns the largest integral value not greater than `self`
    func floor() -> Self
    /// Mutating form of `floor`
    mutating func formFloor()

    /// Returns the smallest integral value not less than `self`
    func ceiling() -> Self
    /// Mutating form of `ceiling`
    mutating func formCeiling()
}
 
<https://gist.github.com/karwa/273db66cd8a5fe2c388ccc7de9c4cf31#impact-on-existing-code>Impact
 on existing code

This change is additive, although we may consider suppressing the imported, 
global-level C functions and automatically migrating them to instance method 
calls.

 
<https://gist.github.com/karwa/273db66cd8a5fe2c388ccc7de9c4cf31#alternatives-considered>Alternatives
 considered

floor() and ceil(), exactly like C. ceiling() is more descriptive and is a 
mathematical term of art <http://mathworld.wolfram.com/CeilingFunction.html>.
nextIntegralUp() and nextIntegralDown() are more descriptive still, but 
possibly misleading as (4.0).nextIntegralUp() == 4.0
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to