Make a function handle Float and Double

You need to use BinaryFloatingPoint because FloatingPoint itself doesn't conform to ExpressibleByFloatLiteral. That's why you see these errors: Swift defaults to making your literal values of type Double.

pow isn't generic, but you can write your own extension to BinaryFloatingPoint that calls the right pow. It's not pretty, though. The Swift standard library doesn't actually offer floating-point math functions [yet]. You're relying on the C standard library here (although as an implementation detail pow specifically is actually intercepted by Swift and calls through to the LLVM intrinsic).

2 Likes