[swift-evolution] Add max/min to floating point types

Darren Mo darren.mo at me.com
Fri Jun 10 15:44:32 CDT 2016


Interesting. I didn’t know that you couldn’t use CGFloat.max/CGFloat.min (aka CGFloat.greatestFiniteMagnitude/CGFloat.leastNormalMagnitude) with auto layout. Good to know.

However, I was not referring to auto layout, but to the calculation of text height based on a fixed width. Like:

let fixedWidthSize = CGSize(width: myWidth, height: CGFloat.greatestFiniteMagnitude)
let textHeight = myLabel.sizeThatFits(fixedWidthSize).height

I have done this many times before with no errors and I have seen other people do this too. (Surely you have as well!)

> On Jun 10, 2016, at 3:32 PM, Erica Sadun <erica at ericasadun.com> wrote:
> 
> CGFloat has .max and .min. And if you use them, you get:
> 2016-06-10 13:32:14.185 Untitled Page 10[18435:13174627] This NSLayoutConstraint is being configured with a constant that exceeds internal limits.  A smaller value will be substituted, but this problem should be fixed. Break on void _NSLayoutConstraintNumberExceedsLimit() to debug.  This will be logged only once.  This may break in the future.
> 
> -- E
> 
>> On Jun 10, 2016, at 12:24 PM, Darren Mo via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Today, one can get max/min by doing:
>> 
>> let max = Float.greatestFiniteMagnitude
>> let min = -Float.greatestFiniteMagnitude
>> 
>> I propose that the floating point types expose properties for max/min.
>> 
>> max, in particular, is used quite a lot in UI code for fixed-width layout of text. But having to spell out greatestFiniteMagnitude every time is a pain. For example…
>> 
>> Compare this:
>> extension NSTextView {
>>    func configureForFixedWidth() {
>>       minSize = NSSize.zero
>>       maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
>>       isHorizontallyResizable = false
>>       isVerticallyResizable = true
>> 
>>       textContainer?.containerSize = NSSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude)
>>       textContainer?.widthTracksTextView = true
>>    }
>> }
>> 
>> To this:
>> extension NSTextView {
>>    func configureForFixedWidth() {
>>       minSize = NSSize.zero
>>       maxSize = NSSize(width: CGFloat.max, height: CGFloat.max)
>>       isHorizontallyResizable = false
>>       isVerticallyResizable = true
> 
>> 
>>       textContainer?.containerSize = NSSize(width: bounds.width, height: CGFloat.max)
>>       textContainer?.widthTracksTextView = true
>>    }
>> }
>> 
>> The latter snippet is much more understandable (and less typing). It is more understandable because users don’t have to know exactly how floating point works in order to get the equivalent of Int.max/Int.min for CGFloat.
>> 
>> One of the concerns with naming them max/min is that infinity/-infinity is technically the real max/min. We could name them finiteMax/finiteMin, but I think keeping the names consistent with Int et al. is important since they serve the same purpose. Besides, I think dealing with infinity is rare in real-world usage. Those that are using infinity know that it is obviously the true max.
>> 
>> I think adding these floating point properties is in line with Swift 3’s goals of consistency and refinement.
>> 
>> Thoughts?
>> _______________________________________________
>> 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/20160610/cbf781a0/attachment.html>


More information about the swift-evolution mailing list