<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I’m with Nevin on this one. &nbsp;Perhaps the easiest thing to do is to add something to the FloatLiteral type that lets you get it as a string if desired.<div class=""><br class=""></div><div class="">Didn’t we have a discussion a while back on how to make Integer Literals work with BigInt? &nbsp;Maybe there is an idea from that discussion that would help.</div><div class=""><br class=""></div><div class="">Tl;dr: &nbsp;Literals shouldn’t be tied to a particular implementation of a single conforming type (though they can/should be optimized for common implementations). &nbsp;The issue here is that FloatLiteral is throwing out information which is given to it based on its underlying implementation. &nbsp;I view this as a bug.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon<br class=""><div class=""><br class=""></div><div class="">&nbsp;<br class=""><div><blockquote type="cite" class=""><div class="">On Jan 16, 2018, at 4:20 PM, Nevin Brackett-Rozinsky via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">On Tue, Jan 16, 2018 at 6:31 PM, Xiaodi Wu <span dir="ltr" class="">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank" class="">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br class=""></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><span class="gmail-">On Tue, Jan 16, 2018 at 4:30 PM, Nevin Brackett-Rozinsky <span dir="ltr" class="">&lt;<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank" class="">nevin.brackettrozinsky@gmail.<wbr class="">com</a>&gt;</span> wrote:<br class=""></span><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">The thing that is “broken” here is generic programming. If I constrain something to FloatingPoint, I cannot use a float literal in calculations with it:</div><div class="gmail_quote"><br class=""></div><div class="gmail_quote"><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span>&nbsp;centimeters&lt;T: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (inches: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></div><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">2.54</span> * inches<span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s5" style="color:rgb(0,132,0)">// Error</span></font></div><p class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">









</p><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class="">}</font></div></div></div></div></blockquote><div class=""><br class=""></div></span><div class="">Why not constrain it to `BinaryFloatingPoint`? What other types are you trying to use with this function?</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">We should not ask nor expect people to constrain their generic algorithms to BinaryFloatingPoint unless they are working with the radix.<br class=""></div><div class=""><br class=""></div><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"> so that eg. a Rational type could be used. And that gives a hint as to the workaround:</div><div class="gmail_quote"><br class=""></div><div class="gmail_quote"><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span>&nbsp;centimeters&lt;T: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (inches: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></div><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> (<span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">254</span> / <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">100</span>) * inches</font></div><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class="">}</font></div></div></div></div></blockquote><div class=""><br class=""></div></span><div class="">Yes, you *could* do that.</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">And it seems I *will* be doing that, as long as such a workaround is necessary. Though it does appear to have the unfortunate cost of an extra division operation.</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"></div><div class="gmail_quote">That only works for numbers which don’t overflow the integer literals though.</div></div></div></blockquote><div class=""><br class=""></div></span><div class="">Integer literals don't overflow until 2048 bits. The following compiles just fine:</div><span class="gmail-"><div class=""><br class=""></div><div class="">func moles&lt;T : FloatingPoint&gt;(particles: T) -&gt; T {<br class=""></div></span><div class="">&nbsp; let N_A: T = 602_214_085_774_000_000_000_<wbr class="">000</div><div class="">&nbsp; return particles / N_A</div><span class="gmail-"><div class="">}</div></span></div></div></div></blockquote><div class=""><br class=""></div><div class=""><div class="">When I write that in a playground it shows N_A as 1.67866967797794e+18.</div><div class=""><br class=""></div><div class="">(Also, you appear to have mistakenly concatenated the standard uncertainty in the last 2 digits, “74”, onto the accepted value for the constant.)</div></div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">If we want a really large or small value then we have to split it in pieces:</div><div class="gmail_quote"><br class=""></div><div class="gmail_quote"><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> moles &lt;T: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (particles: <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></div><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(39, 42, 216); background-color: rgb(255, 255, 255);" class=""><font size="1" class=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">&nbsp; &nbsp; </span></span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">let</span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style=""> avogadroNumber: </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style=""> = </span>6_022_140_857<span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style=""> * </span>100_000_000_000_000</font></div><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class=""><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> particles / avogadroNumber</font></div><p class="gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-p1" style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">










</p><div style="margin: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><font size="1" class="">}</font></div></div><div class="gmail_quote"><br class=""></div><div class="gmail_quote">It would be much nicer to write “let avogadroNumber: T = 6.022140857e23”.</div></div></div></blockquote><div class=""><br class=""></div></span><div class="">You could write:</div><div class=""><br class=""></div><div class="">func moles&lt;T : FloatingPoint &amp; LosslessStringConvertible&gt;(<wbr class="">particles: T) -&gt; T {</div><div class="">&nbsp; let N_A = T("6.02214085774e+23")!</div><div class="">&nbsp; return particles / N_A</div><span class="gmail-"><div class="">}</div></span></div></div></div></blockquote><div class=""><br class=""></div><div class="">…or I could write “T: FloatingPoint &amp; ExpressibleByFloatLiteral”. I could even make a typealias for that. But I shouldn’t have to.</div><div class=""><br class=""></div><div class="">Nevin</div></div></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></body></html>