<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jun 10, 2016 at 1:24 PM, Darren Mo via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Today, one can get <font face="Menlo" style="font-size:11px">max</font>/<font face="Menlo" style="font-size:11px">min</font> by doing:<div><br></div><div><font face="Menlo"><span style="font-size:11px">let max = Float.greatestFiniteMagnitude</span></font></div><div><font face="Menlo"><span style="font-size:11px">let min = -Float.greatestFiniteMagnitude</span></font></div></div></blockquote><div><br></div><div>On review of the proposal for the new FloatingPoint, I too commented on the lack of `max` and `min`. You've pointed out the issue with infinity. But also, FLT_MIN (from your local friendly C universe and available in Swift, obviously) is actually the smallest representable positive value, so `Float.min` is of ambiguous meaning. It was therefore decided not to use those words `max` and `min`.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><font face="Menlo"><span style="font-size:11px"><br></span></font></div><div>I propose that the floating point types expose properties for <font face="Menlo" style="font-size:11px">max</font>/<font face="Menlo" style="font-size:11px">min</font>.</div><div><br></div><div><font face="Menlo" style="font-size:11px">max</font>, in particular, is used quite a lot in UI code for fixed-width layout of text. But having to spell out <font face="Menlo" style="font-size:11px">greatestFiniteMagnitude</font> every time is a pain. For example…</div><div><br></div><div>Compare this:</div><div><font face="Menlo" style="font-size:11px">extension NSTextView {<br> func configureForFixedWidth() {<br> minSize = NSSize.zero<br> maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)<br> isHorizontallyResizable = false<br> isVerticallyResizable = true<br><br> textContainer?.containerSize = NSSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude)<br> textContainer?.widthTracksTextView = true<br> }<br>}</font></div><div><font><br></font></div><div>To this:</div><div><span style="font-family:Menlo;font-size:11px">extension </span><span style="font-family:Menlo;font-size:11px">NSTextView</span><span style="font-family:Menlo;font-size:11px"> {</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">func</span><span style="font-family:Menlo;font-size:11px"> configureForFixedWidth() {</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">minSize</span><span style="font-family:Menlo;font-size:11px"> = </span><span style="font-family:Menlo;font-size:11px">NSSize</span><span style="font-family:Menlo;font-size:11px">.zero</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">maxSize</span><span style="font-family:Menlo;font-size:11px"> = </span><span style="font-family:Menlo;font-size:11px">NSSize</span><span style="font-family:Menlo;font-size:11px">(width: CGFloat.max</span><span style="font-family:Menlo;font-size:11px">, height:</span><span style="font-family:Menlo;font-size:11px"> CGFloat.max</span><span style="font-family:Menlo;font-size:11px">)</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">isHorizontallyResizable</span><span style="font-family:Menlo;font-size:11px"> = </span><span style="font-family:Menlo;font-size:11px">false</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">isVerticallyResizable</span><span style="font-family:Menlo;font-size:11px"> = </span><span style="font-family:Menlo;font-size:11px">true</span><br style="font-family:Menlo;font-size:11px"><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">textContainer</span><span style="font-family:Menlo;font-size:11px">?.</span><span style="font-family:Menlo;font-size:11px">containerSize</span><span style="font-family:Menlo;font-size:11px"> = </span><span style="font-family:Menlo;font-size:11px">NSSize</span><span style="font-family:Menlo;font-size:11px">(width: </span><span style="font-family:Menlo;font-size:11px">bounds</span><span style="font-family:Menlo;font-size:11px">.</span><span style="font-family:Menlo;font-size:11px">width</span><span style="font-family:Menlo;font-size:11px">, height:</span><span style="font-family:Menlo;font-size:11px"> CGFloat.max</span><span style="font-family:Menlo;font-size:11px">)</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">textContainer</span><span style="font-family:Menlo;font-size:11px">?.</span><span style="font-family:Menlo;font-size:11px">widthTracksTextView</span><span style="font-family:Menlo;font-size:11px"> = </span><span style="font-family:Menlo;font-size:11px">true</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px">}</span><br style="font-family:Menlo;font-size:11px"><span style="font-family:Menlo;font-size:11px">}</span></div><div><br></div><div>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 <font face="Menlo" style="font-size:11px">Int.max</font>/<font face="Menlo" style="font-size:11px">Int.min</font> for <font face="Menlo" style="font-size:11px">CGFloat</font>.</div><div><br></div><div>One of the concerns with naming them <font face="Menlo" style="font-size:11px">max</font>/<font face="Menlo" style="font-size:11px">min</font> is that <font face="Menlo" style="font-size:11px">infinity</font>/<font face="Menlo" style="font-size:11px">-infinity</font> is technically the real max/min. We could name them <font face="Menlo" style="font-size:11px">finiteMax</font>/<font face="Menlo" style="font-size:11px">finiteMin</font>, but I think keeping the names consistent with <font face="Menlo" style="font-size:11px">Int</font> et al. is important since they serve the same purpose. Besides, I think dealing with <font face="Menlo" style="font-size:11px">infinity</font> is rare in real-world usage. Those that are using <font face="Menlo" style="font-size:11px">infinity</font> know that it is obviously the true max.</div><div><br></div><div>I think adding these floating point properties is in line with Swift 3’s goals of consistency and refinement.</div><div><br></div><div>Thoughts?</div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>