[swift-evolution] [Proposal] Add floor() and ceiling() functions to FloatingPoint
Karl
razielim at gmail.com
Mon Jun 27 11:34:10 CDT 2016
> On 27 Jun 2016, at 16:23, Stephen Canon <scanon at apple.com> wrote:
>
>
>> On Jun 25, 2016, at 05:06, Karl via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>>> Proposal: https://gist.github.com/karwa/273db66cd8a5fe2c388ccc7de9c4cf31 <https://gist.github.com/karwa/273db66cd8a5fe2c388ccc7de9c4cf31>
> Karl, thanks for writing this up. It should be extended to include not only floor( ) and ceiling( ), but also:
>
> /// Returns the integral value closest to `self` whose magnitude is not greater than that of `self`.
> func truncate( ) -> Self
>
> /// Returns the integral value closest to `self`. If two integrers are equally close, the even one
> /// is returned.
> // NOTE: The name of this function requires bike-shedding. I’ve chosen a deliberately poor
> // name as a straw-man.
> func roundToNearestTiesToEven( ) -> Self
>
> /// Returns the integral value closest to `self`. If two integrers are equally close, the one with
> /// greater magnitude is returned.
> // NOTE: The name of this function requires bike-shedding. I’ve chosen a deliberately poor
> // name as a straw-man.
> func roundToNearestTiesAway( ) -> Self
>
> and mutating versions of those.
>
I was trying to add these, but working out the names of the mutating functions is difficult. How is truncate different to floor if it returns an integral value and can never round up?
Perhaps for the other functions, we could have a general `round` function with a tiebreak-enum parameter (it would be great if we could embed enums in protocols, but I’m not sure if that’s even on the roadmap):
enum FloatingPointRoundingStrategy { // or something to that effect
case preferEven
case preferGreatest
}
func rounded(inTiebreak: FloatingPointRoundingStrategy) -> Self
I think `(4.5).rounded(inTiebreak: .preferGreatest) == 5.0` looks quite nice.
Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160627/96ec1eae/attachment.html>
More information about the swift-evolution
mailing list