[swift-users] FloatingPoint/BinaryFloatingPoint protocol and concrete FloatingPoint types

Jens Persson jens at bitcycle.com
Fri Dec 1 15:18:34 CST 2017


protocol Float80Convertible : BinaryFloatingPoint {
    init(_ value: Float80)
    var float80: Float80 { get }
}
extension Double : Float80Convertible {
    var float80: Float80 { return Float80(self) }
}
extension Float : Float80Convertible {
    var float80: Float80 { return Float80(self) }
}

func maxPrecisionCalculation(input:Float80) -> Float80 {
    return input // but something actually reauiring high precision ...
}

func someComplexCalculation<T:Float80Convertible>(input: T) -> T {
    let input80 = input.float80
    let output80 = maxPrecisionCalculation(input: input80)
    return T(output80)
}


On Wed, Nov 29, 2017 at 12:45 PM, Antonino Ficarra via swift-users <
swift-users at swift.org> wrote:

> 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?
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171201/b46574bd/attachment.html>


More information about the swift-users mailing list