Converting back to Double

Is there a way to create a Double from an AlgebraicField or Real type?

I have a use case where a have a function like

func f<X: AlgebraicField>(_ x: X, minimum a: X, peak b: X, maximum c: X) -> Double {
    switch x {
    case a...b: return (x - a) / (b - a)
    case b...c: return (c - u) / (x - b)
    default: return 0
    }
}

where I have constraint X: AlgebraicField but at the same time I need to be sure that I return a Double.

AlgebraicField is not the protocol you want here (in particular, "minimum", "maximum", and ranges don't make sense for an arbitrary algebraic field, since it may not be ordered).

You probably want something like BinaryFloatingPoint instead, which would make this Just Work.

2 Likes