[swift-evolution] [Proposal] Add floor() and ceiling() functions to FloatingPoint

Karl razielim at gmail.com
Sat Jun 25 05:06:53 CDT 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160625/c05ae674/attachment.html>


More information about the swift-evolution mailing list