[swift-evolution] Proposal: floating point static min / max properties

Matthew Johnson matthew at anandabits.com
Sat Dec 5 14:58:21 CST 2015


Hi Steve,

Thanks for looking at my proposal and sharing the internal consensus.  I do like the more explicit names.

One of the reasons I proposed this was to follow up with a proposal for a RangeDiscoverable protocol that would be conformed to by all numeric types:

protocol RangeDiscoverable {
    static var min: Self { get }
    static var max: Self { get }
}

A better interface would looks like this (not sure why I didn’t think of this before):

public protocol RangeDiscoverable {
	static var representableRange: Range<Self> { get }
}

With conformances that look like this:

extension Int: RangeDiscoverable {
	static let representableRange: Range<Int> = Int.min…Int.max
}

extension Float: RangeDiscoverable {
	// not sure it would be better to use -Float. greatestFiniteMagnitude..Float. greatestFiniteMagnitude here or not
	static let representableRange: Range<Float> = -Float.infinity...Float.infinity
}

This would require numeric types to conform to ForwardIndexType.  Integer types already conform to RandomAccessIndexType.  Floating point types do not currently conform to ForwardIndexType but could conform not just to ForwardIndexType, but also BidirectionalIndexType.

My original implementation looked like this:

extension Float: BidirectionalIndexType {
    public func predecessor() -> Float {
        return nextafterf(self, Float.min)
    }
    public func successor() -> Float {
        return nextafterf(self, Float.max)
    }
}

With the nextUp and nextDown properties I see in test/Prototypes/FloatingPoint.swift this could change to:

extension Float: BidirectionalIndexType {
    public func predecessor() -> Float {
        return nextDown
    }
    public func successor() -> Float {
        return nextUp
    }
}

What is your thought on adding a protocol similar to RangeDiscoverable (conformed to by all numeric types) and the supporting BidirectionalIndexType conformance for floating point types?  Is this something that would be considered?




> On Dec 5, 2015, at 11:41 AM, Stephen Canon <scanon at apple.com> wrote:
> 
> Hi Matthew —
> 
> This is something that we’ve discussed quite a bit internally, and are close to consensus on.  Many people feel that “max” and “min” are confusing or misleading, as they are not actually the largest / smallest values of the type, so the plan is to use much more explicit names (you can see an early sketch of this in test/Prototypes/FloatingPoint.swift, though there are a number of things that will be changed as well).  I’ve excerpted the relevant section here for convenience:
> 
>   /// Positive infinity.
>   ///
>   /// Compares greater than all finite numbers.
>   static var infinity: Self { get }
>   
>   /// The greatest finite value.
>   ///
>   /// Compares greater than or equal to all finite numbers, but less than
>   /// infinity.
>   static var greatestFiniteMagnitude: Self { get }
>   
>   /// The least positive normal value.
>   ///
>   /// Compares less than or equal to all positive normal numbers.  There may
>   /// be smaller positive numbers, but they are "subnormal", meaning that
>   /// they are represented with less precision than normal numbers.
>   static var leastNormalMagnitude: Self { get }
>   
>   /// The least positive value.
>   ///
>   /// Compares less than or equal to all positive numbers, but greater than
>   /// zero.  If the target supports subnormal values, this is smaller than
>   /// `leastNormalMagnitude`; otherwise they are equal.
>   static var leastMagnitude: Self { get }
> 
> – Steve
> 
>> On Dec 5, 2015, at 11:10 AM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>> 
>> In the spirit of small commits and incremental change I have a very small proposal.  I am not sure if this belongs on the list or if small changes like this are ok as pull requests, but am starting here because it involves adding public API in the standard library.  
>> 
>> Integer types have static min / max properties, but floating point types currently do not.  The Darwin implementation is very straightforward.  
>> 
>> import Darwin
>> 
>> public extension Float {
>>   static let min = -FLT_MAX
>>   static let max = FLT_MAX
>> }
>> 
>> public extension Double {
>>   static let min = -DBL_MAX
>>   static let max = DBL_MAX
>> }
>> 
>> Is there interest in adding this?  If so, what is the right way to proceed?
>> 
>> Matthew
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/d48ecfbc/attachment.html>


More information about the swift-evolution mailing list