<div dir="ltr">As Erica mentioned about Angle, this seems to be a perfect fit for an appropriately focused third-party library, but I'm not sure I appreciate why this should be part of the standard library. In large part, you seem to have reinvented a decimal type, which is already available in the form of Foundation.Decimal on all supported platforms.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 13, 2018 at 9:07 PM, Jonathan Hull 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">Here is the code I use for percentage myself. (I incorrectly said I use a UInt64… I use a UInt32):<div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,128,0);background-color:rgb(255,255,255)">///Represents a percentage with the precision of millionths of 1 (i.e. 4 decimal places: XX.XXXX%). The value is always positive (or zero), but may be greater than 100%</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#0433ff">struct</span> Percentage {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">fileprivate(set)</span> <span style="color:#0433ff">var</span> millionths:<span style="color:#3495af">UInt32</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">fileprivate</span> <span style="color:#0433ff">init</span>(storage:<span style="color:#3495af">UInt32</span>){</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#3495af">millionths</span> = storage</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">var</span> quarter = <span style="color:#3495af">Percentage</span>(storage: 250_000)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">var</span> half = <span style="color:#3495af">Percentage</span>(storage: 500_000)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">var</span> threeQuarters = <span style="color:#3495af">Percentage</span>(storage: 750_000)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">var</span> full = <span style="color:#3495af">Percentage</span>(storage: 1_000_000)</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">init</span>(millionths:<span style="color:#3495af">Int</span>) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(millionths)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">init</span>(<span style="color:#0433ff">_</span> double:<span style="color:#3495af">Double</span>, range:<span style="color:#3495af">ClosedRange</span><<span style="color:#3495af">Double</span>> = 0...1) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">if</span> range == 0...1 {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>(double * 1_000_000, 0))</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }<span style="color:#0433ff">else</span> <span style="color:#0433ff">if</span> range == 0...100{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>(double * 10_000, 0))</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }<span style="color:#0433ff">else</span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>((double - range.<span style="color:#3495af">lowerBound</span>)/(range.<span style="color:#3495af">upper<wbr>Bound</span> - range.<span style="color:#3495af">lowerBound</span>),0))</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">init</span>(<span style="color:#0433ff">_</span> num:<span style="color:#3495af">Num</span>, range:<span style="color:#3495af">ClosedRange</span><<span style="color:#3495af">Num</span>> = 0...1) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">if</span> range == 0...1 {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>(num * 1_000_000, 0).<span style="color:#3495af">integerValue</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }<span style="color:#0433ff">else</span> <span style="color:#0433ff">if</span> range == 0...100{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>(num * 10_000, 0).<span style="color:#3495af">integerValue</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }<span style="color:#0433ff">else</span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">self</span><span style="color:#000000">.</span>millionths<span style="color:#000000"> = </span>UInt32<span style="color:#000000">(</span>max<span style="color:#000000">((num - range.</span>lowerBound<span style="color:#000000">)/(range.</span>upper<wbr>Bound<span style="color:#000000"> - range.</span>lowerBound<span style="color:#000000">),0).</span>integerVa<wbr>lue<span style="color:#000000">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">init</span>(<span style="color:#0433ff">_</span> decimal:<span style="color:#3495af">Decimal</span>, range:<span style="color:#3495af">ClosedRange</span><<span style="color:#3495af">Decimal</span>> = 0...1) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">if</span> range == 0...1 {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">NSDecimalNumber</span>(decimal: <span style="color:#3495af">max</span>(decimal * 1_000_000, 0)).<span style="color:#3495af">uint32Value</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }<span style="color:#0433ff">else</span> <span style="color:#0433ff">if</span> range == 0...100{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">NSDecimalNumber</span>(decimal: <span style="color:#3495af">max</span>(decimal * 10_000, 0)).<span style="color:#3495af">uint32Value</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }<span style="color:#0433ff">else</span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">let</span> shifted = <span style="color:#3495af">max</span>((decimal - range.<span style="color:#3495af">lowerBound</span>)/(range.<span style="color:#3495af">upper<wbr>Bound</span> - range.<span style="color:#3495af">lowerBound</span>),0)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">self</span><span style="color:#000000">.</span>millionths<span style="color:#000000"> = </span>NSDecimalNumber<span style="color:#000000">(decimal: shifted).</span>uint32Value</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">init</span>(hundredths:<span style="color:#3495af">Int</span>) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>(hundredths * 10_000, 0))</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">init</span>(thousandths:<span style="color:#3495af">Int</span>) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>(thousandths * 1_000, 0))</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> isFull:<span style="color:#3495af">Bool</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> >= 1_000_000</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> doubleValue:<span style="color:#3495af">Double</span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Double</span>(<span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span>)/1_000_<wbr>000</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> cgfloatValue:<span style="color:#3495af">CGFloat</span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">CGFloat</span>(<span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span>)/1_<wbr>000_000</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> decimalValue:<span style="color:#3495af">Decimal</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Decimal</span>(<span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span>)/1_<wbr>000_000</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> numValue:<span style="color:#3495af">Num</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Num</span>(numerator: <span style="color:#3495af">Int32</span>(<span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span>), denominator: 1_000_000)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,143,0);background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> hundredths:<span style="color:#3495af">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Int</span>(<span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span>/10_000)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> thousandths:<span style="color:#3495af">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Int</span>(<span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span>/1_000)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> tenThousandths:<span style="color:#3495af">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Int</span>(<span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span>/100)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">func</span> map(to range:<span style="color:#3495af">ClosedRange</span><<span style="color:#3495af">Num</span>>) -> <span style="color:#3495af">Num</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#0433ff">self</span>.<span style="color:#3495af">numValue</span> * (range.<span style="color:#3495af">upperBound</span> - range.<span style="color:#3495af">lowerBound</span>) + range.<span style="color:#3495af">lowerBound</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">mutating</span> <span style="color:#0433ff">func</span> clip() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">if</span> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> > 1_000_000 {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = 1_000_000</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">func</span> clipped()-><span style="color:#3495af">Percentage</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">if</span> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> > 1_000_000 {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Percentage</span>.<span style="color:#3495af">full</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(4,51,255);background-color:rgb(255,255,255)"><span style="color:#000000"> </span>return<span style="color:#000000"> </span>self</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#0433ff">extension</span><span style="color:#000000"> </span>Percentage<span style="color:#000000">:</span>CustomStringConvert<wbr>ible<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> description: <span style="color:#3495af">String</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">let</span> num = <span style="color:#0433ff">self</span>.<span style="color:#3495af">numValue</span> * 100</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">if</span> num.<span style="color:#3495af">isInteger</span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#b4261a">"</span>\<span style="color:#b4261a">(</span>num<span style="color:#b4261a">)%"</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#b4261a">"</span>\<span style="color:#b4261a">(</span>num.<span style="color:#3495af">decimalValue</span><span style="color:#b4261a">)%"</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#0433ff">extension</span><span style="color:#000000"> </span>Percentage<span style="color:#000000">:</span>ExpressibleByIntege<wbr>rLiteral<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">init</span>(integerLiteral value: <span style="color:#3495af">IntegerLiteralType</span>) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">self</span>.<span style="color:#3495af">millionths</span> = <span style="color:#3495af">UInt32</span>(<span style="color:#3495af">max</span>(value * 10_000, 0))</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#0433ff">extension</span><span style="color:#000000"> </span>Percentage<span style="color:#000000">:</span>Hashable<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">var</span> hashValue: <span style="color:#3495af">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">return</span><span style="color:#000000"> </span><span style="color:#0433ff">self</span><span style="color:#000000">.</span>millionths<span style="color:#000000">.</span>hashValue</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">func</span> == (lhs:<span style="color:#3495af">Percentage</span>, rhs:<span style="color:#3495af">Percentage</span>)-><span style="color:#3495af">Bool</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs.<span style="color:#3495af">millionths</span> == rhs.<span style="color:#3495af">millionths</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#0433ff">extension</span><span style="color:#000000"> </span>Percentage<span style="color:#000000">:</span>Comparable<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">func</span> < (lhs:<span style="color:#3495af">Percentage</span>, rhs:<span style="color:#3495af">Percentage</span>) -> <span style="color:#3495af">Bool</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs.<span style="color:#3495af">millionths</span> < rhs.<span style="color:#3495af">millionths</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#0433ff">extension</span><span style="color:#000000"> </span>Percentage<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">static</span><span style="color:#000000"> </span><span style="color:#0433ff">func</span><span style="color:#000000"> + (lhs:</span>Percentage<span style="color:#000000">, rhs:</span>Percentage<span style="color:#000000">)-></span>Percentage<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Percentage</span>(storage: lhs.<span style="color:#3495af">millionths</span> + rhs.<span style="color:#3495af">millionths</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">static</span><span style="color:#000000"> </span><span style="color:#0433ff">func</span><span style="color:#000000"> - (lhs:</span>Percentage<span style="color:#000000">, rhs:</span>Percentage<span style="color:#000000">)-></span>Percentage<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">if</span> rhs > lhs {<span style="color:#0433ff">return</span> 0}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> <span style="color:#3495af">Percentage</span>(storage: lhs.<span style="color:#3495af">millionths</span> - rhs.<span style="color:#3495af">millionths</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">func</span> * (lhs:<span style="color:#3495af">Percentage</span>, rhs:<span style="color:#3495af">Double</span>)-><span style="color:#3495af">Double</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs.<span style="color:#3495af">doubleValue</span> * rhs</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">func</span> * (lhs:<span style="color:#3495af">Double</span>, rhs:<span style="color:#3495af">Percentage</span>)-><span style="color:#3495af">Double</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs * rhs.<span style="color:#3495af">doubleValue</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">static</span><span style="color:#000000"> </span><span style="color:#0433ff">func</span><span style="color:#000000"> * (lhs:</span>Percentage<span style="color:#000000">, rhs:</span>CGFloat<span style="color:#000000">)-></span>CGFloat<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs.<span style="color:#3495af">cgfloatValue</span> * rhs</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">static</span><span style="color:#000000"> </span><span style="color:#0433ff">func</span><span style="color:#000000"> * (lhs:</span>CGFloat<span style="color:#000000">, rhs:</span>Percentage<span style="color:#000000">)-></span>CGFloat<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs * rhs.<span style="color:#3495af">cgfloatValue</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">func</span> * (lhs:<span style="color:#3495af">Percentage</span>, rhs:<span style="color:#3495af">Num</span>)-><span style="color:#3495af">Num</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs.<span style="color:#3495af">numValue</span> * rhs</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">static</span> <span style="color:#0433ff">func</span> * (lhs:<span style="color:#3495af">Num</span>, rhs:<span style="color:#3495af">Percentage</span>)-><span style="color:#3495af">Num</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#0433ff">return</span> lhs * rhs.<span style="color:#3495af">numValue</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">static</span><span style="color:#000000"> </span><span style="color:#0433ff">func</span><span style="color:#000000"> * (lhs:</span>Percentage<span style="color:#000000">, rhs:</span>Percentage<span style="color:#000000">)-></span>Percentage<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,149,175);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#0433ff">return</span><span style="color:#000000"> </span>Percentage<span style="color:#000000">(lhs.</span>decimalValue<span style="color:#000000"> * rhs.</span>decimalValue<span style="color:#000000">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-2401119500926847804webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div></div><div><div class="h5"><div><br></div><div><br></div><div> <br><div><blockquote type="cite"><div>On Jan 13, 2018, at 6:26 PM, Jonathan Hull via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_-2401119500926847804Apple-interchange-newline"><div><div>Hi Evolution,<br><br>I was wondering if we would consider adding a percentage type to Swift. This would be a type with a value between 0 & 1.<br><br>I know we can and do use doubles or floats for this now, but there really is a semantic difference between most parameters that take a value between 0 & 1 and those that take any floating point value. It would be nice to have a type that semantically means that the value is from 0 to 1.<br><br>It could even just wrap a Double for speed (in my own code I wrap a UInt64 for decimal accuracy… and to avoid issues around comparisons).<br><br>Thanks,<br>Jon<br>______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></div></blockquote></div><br></div></div></div></div><br>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>