> On 27 Jun 2016, at 16:23, Stephen Canon <[email protected]> wrote:
> 
> 
>> On Jun 25, 2016, at 05:06, Karl via swift-evolution 
>> <[email protected] <mailto:[email protected]>> 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
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to