[swift-users] FloatingPoint/BinaryFloatingPoint protocol and concrete FloatingPoint types
Antonino Ficarra
antonino.ficarra at gmail.com
Wed Nov 29 05:45:38 CST 2017
Suppose that I have a generic function that makes a complex floating calculation
with a generic floating point type.
Suppose that calculation require the max floating point machine precision so:
1) I need to convert the generic floating point type to Float80
2) make the complex calculation with Float80
3) convert back the Float80 result to the generic floating point type
func maxPrecisionCalculation( input:Float80 ) -> Float80 {
// ....
}
Let's try with FloatingPoint:
func someComplexCalculation<T:FloatingPoint>( input:T ) -> T {
let input80 = Float80( input ) // Error: Cannot invoke initializer for type 'Float80' with an argument list of type '(T)'
let input80 = input as Float80 // Error 'T' is not convertible to 'Float80'; did you mean to use 'as!' to force downcast?
let output80 = maxPrecisionCalculation( input:input80 )
return output80 as T // Error: 'Float80' is not convertible to 'T'; did you mean to use 'as!' to force downcast?
return T(output80) // Error: Non-nominal type 'T' does not support explicit initialization
}
How convert a generic FloatingPoint to a concrete floating point type and then convert it back?
And now let's try with BinaryFloatingPoint:
func someComplexCalculation <T:BinaryFloatingPoint>( input:T ) -> T {
let input80 = Float80( input ) // Error: Cannot invoke initializer for type 'Float80' with an argument list of type '(T)'
let input80 = input as Float80 // Error 'T' is not convertible to 'Float80'; did you mean to use 'as!' to force downcast?
let output80 = maxPrecisionCalculation( input:Float80(0) )
return T(output80) // Ok, now this work
return output80 as T // Error: 'Float80' is not convertible to 'T'; did you mean to use 'as!' to force downcast?
}
How convert a generic BinaryFloatingPoint to a concrete floating point type?
In the opposite direction the conversion work.
NOTE: Using Double instead of Float80 don't make any difference at all.
ADDENDUM: The Float80 type exists from the first Swift version. Now we have Swift 4
and math function over Float80 are still unsupported, making this type substantially useless.
I still can’t call sin(x) and log(x) with a Float80 value.
There is a plan to resolve the issue?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171129/e25fe2c1/attachment.html>
More information about the swift-users
mailing list