<div dir="ltr"><div><br></div><div>protocol Float80Convertible : BinaryFloatingPoint {</div><div>    init(_ value: Float80)</div><div>    var float80: Float80 { get }</div><div>}</div><div>extension Double : Float80Convertible {<br></div><div>    var float80: Float80 { return Float80(self) }</div><div>}</div><div><div>extension Float : Float80Convertible {</div><div>    var float80: Float80 { return Float80(self) }</div><div>}</div></div><div><br></div><div>func maxPrecisionCalculation(input:Float80) -&gt; Float80 {</div><div>    return input // but something actually reauiring high precision ...</div><div>}</div><div><br></div><div>func someComplexCalculation&lt;T:Float80Convertible&gt;(input: T) -&gt; T {</div><div>    let input80 = input.float80</div><div>    let output80 = maxPrecisionCalculation(input: input80)</div><div>    return T(output80)</div><div>}</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 29, 2017 at 12:45 PM, Antonino Ficarra via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</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;line-break:after-white-space"><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">Suppose that I have a generic function that makes a complex floating calculation</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">with a generic floating point type.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">Suppose that calculation require the max floating point machine precision so:</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">1) I need to convert the generic floating point type to Float80</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">2) make the complex calculation with Float80</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">3) convert back the Float80 result to the generic floating point type</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">func</span> maxPrecisionCalculation( input:<span style="color:#703daa">Float80</span> ) -&gt; <span style="color:#703daa">Float80</span> {</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span>// ....</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">Let&#39;s try with FloatingPoint:</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">func</span> someComplexCalculation&lt;T:<span style="color:#703daa">Float<wbr>ingPoint</span>&gt;( input:<span style="color:#4f8187">T</span> ) -&gt; <span style="color:#4f8187">T</span> {</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><span style="font-family:Menlo;font-size:13px"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="font-family:Menlo;font-size:13px;color:rgb(186,45,162)">let</span><span style="font-family:Menlo;font-size:13px"> input80<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span>= </span><span style="font-family:Menlo;font-size:13px;text-decoration:underline;color:rgb(112,61,170)">F</span><span style="font-family:Menlo;font-size:13px;color:rgb(112,61,170)">loat80</span><span style="font-family:Menlo;font-size:13px">( input )<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="color:rgb(0,132,0);font-family:Menlo;font-size:13px">// Error: Cannot invoke initializer for type &#39;Float80&#39; with an argument list of type &#39;(T)&#39;</span></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="color:#ba2da2">let</span><span style="color:#000000"> </span><span style="text-decoration:underline;color:#000000">i</span><span style="color:#000000">nput80 <span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span>= input </span><span style="text-decoration:underline;color:#ba2da2">a</span><span style="color:#ba2da2">s</span><span style="color:#000000"> </span><span style="color:#703daa">Float80</span><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">                </span></span>// Error &#39;T&#39; is not convertible to &#39;Float80&#39;; did you mean to use &#39;as!&#39; to force downcast?</div><p style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:15px"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><br class="m_-823676821745130380webkit-block-placeholder"></p><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><span style="color:#ba2da2">let</span> output80<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span>= <span style="color:#31595d">maxPrecisionCalculation</span>( input:input80 )</div><p style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:15px"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><br class="m_-823676821745130380webkit-block-placeholder"></p><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="color:#ba2da2">return</span><span style="color:#000000"> output80 </span><span style="color:#ba2da2">as</span><span style="color:#000000"> </span><span style="color:#4f8187">T</span><span style="color:#000000"> <span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span>// Error: &#39;Float80&#39; is not convertible to &#39;T&#39;; did you mean to use &#39;as!&#39; to force downcast?</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="color:#ba2da2">return</span><span style="color:#000000"> </span><span style="color:#4f8187">T</span><span style="color:#000000">(output80)<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span>// Error: Non-nominal type &#39;T&#39; does not support explicit initialization</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">How convert a generic FloatingPoint to a concrete floating point type and then convert it back?</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><span style="font-family:Menlo;font-size:13px">And now let&#39;s try with BinaryFloatingPoint:</span></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2"><br></span></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">func</span> someComplexCalculation &lt;<wbr>T:<span style="color:#703daa">BinaryFloatingPoint</span>&gt;( input:<span style="color:#4f8187">T</span> ) -&gt; <span style="color:#4f8187">T</span> {</div><p style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:15px"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><span style="color:rgb(186,45,162)">let</span> input80<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span>= <span style="text-decoration:underline;color:rgb(112,61,170)">F</span><span style="color:rgb(112,61,170)">loat80</span>( input )<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><span style="color:rgb(0,132,0)">// Error: Cannot invoke initializer for type &#39;Float80&#39; with an argument list of type &#39;(T)&#39;</span></p><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="color:#ba2da2">let</span><span style="color:#000000"> </span><span style="text-decoration:underline;color:#000000">i</span><span style="color:#000000">nput80 <span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span>= input </span><span style="text-decoration:underline;color:#ba2da2">a</span><span style="color:#ba2da2">s</span><span style="color:#000000"> </span><span style="color:#703daa">Float80</span><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span>// Error &#39;T&#39; is not convertible to &#39;Float80&#39;; did you mean to use &#39;as!&#39; to force downcast?</div><p style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:15px"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><br class="m_-823676821745130380webkit-block-placeholder"></p><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><span style="color:#ba2da2">let</span> output80<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span>= <span style="color:#31595d">maxPrecisionCalculation</span>( input:<span style="color:#703daa">Float80</span>(<span style="color:#272ad8">0</span>) )</div><p style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:15px"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span><br class="m_-823676821745130380webkit-block-placeholder"></p><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="color:#ba2da2">return</span><span style="color:#000000"> </span><span style="color:#4f8187">T</span><span style="color:#000000">(output80)<span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span>// Ok, now this work</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"><span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span><span style="color:#ba2da2">return</span><span style="color:#000000"> output80 </span><span style="text-decoration:underline;color:#ba2da2">a</span><span style="color:#ba2da2">s</span><span style="color:#000000"> </span><span style="color:#4f8187">T</span><span style="color:#000000"> <span class="m_-823676821745130380Apple-tab-span" style="white-space:pre-wrap">        </span></span>// Error: &#39;Float80&#39; is not convertible to &#39;T&#39;; did you mean to use &#39;as!&#39; to force downcast?</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">How convert a generic BinaryFloatingPoint to a concrete floating point type?</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">In the opposite direction the conversion work.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">NOTE: Using Double instead of Float80 don&#39;t make any difference at all.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">ADDENDUM: The Float80 type exists from the first Swift version. Now we have Swift 4</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">and math function over Float80 are still unsupported, making this type substantially useless.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">I still can’t call sin(x) and log(x) with a Float80 value.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">There is a plan to resolve the issue?</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>