[swift-evolution] [Proposal] Add floor() and ceiling() functions to FloatingPoint
Saagar Jha
saagarjha28 at gmail.com
Mon Jun 27 13:48:37 CDT 2016
On Mon, Jun 27, 2016 at 9:34 AM Karl via swift-evolution <
swift-evolution at swift.org> wrote:
> 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> wrote:
>
> Proposal: 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?
>
>
(-4.5).truncate == -4
(-4.5).roundedDown == -5
> 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
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
--
-Saagar Jha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160627/415c6e27/attachment.html>
More information about the swift-evolution
mailing list