如何在swift中为浮点值编写通用函数 [英] How to write a generic function for floating point values in swift
问题描述
func示例< T:FloatingPoint>( _ x:T) - > T {
return cos(cbrt(x + 1))
}
有没有比这更好的方式呢?
协议支持浮点:FloatingPoint {
var cubeRoot:Self {get }
var cosine:Self {get}
}
扩展名Double:SupportsFloatingPoint {
var cubeRoot:Double {return Darwin.cbrt(self)}
var cosine:Double {return Darwin.cos(self)}
}
扩展名Float:SupportsFloatingPoint {
var cubeRoot:Float {return Darwin.cbrt(self)}
var cosine:Float {return Darwin.cos(self)}
}
func cbrt< T:SupportsFloatingPoint>(_ x:T) - > T {
return x.cubeRoot
}
func cos< T:SupportsFloatingPoint>(_ x:T) - > T {
return x.cosine
}
func示例< T:SupportsFloatingPoint>(_ x:T) - > T {
return cos(cbrt(x-1))
}
请注意,添加运算符在此丢失。您可以做 -
, *
和 /
但不能 +
关于SupportsFloatingPoint类型。 您不需要需要一个新的协议。您可以扩展支持类型的现有 FloatingPoint
协议:
//只是为了好玩:)
前缀运算符√
前缀运算符∛
扩展名FloatingPoint其中self == Double {
var squareRoot:Self {return sqrt(self)}}
var cubeRoot:self {return cbrt(self)}
var sine:Self {return sin(self)}
var cosine:self {return cos(self)}
静态前缀func√(_ x:Self) - > Self {return x.squareRoot}
静态前缀func∛(_ x:Self) - > Self {return x.cubeRoot}
}
扩展FloatingPoint其中Self == Float {
var squareRoot:Self {return sqrt(self)}
var cubeRoot: self {return cbrt(self)}
var sine:self {return sin(self)}
var cosine:self {return cos(self)}
static prefix func√ (_ x:Self) - > Self {return x.squareRoot}
静态前缀func∛(_ x:Self) - >自{{x.cubeRoot}
}
print(Double.pi.cosine)
print(√25.0)
print(∛8.0)
无可否认,代码重复有很多,目前我正在研究如何最小化。在光明的一面,至少这是所有静态的,可内联的代码,可以产生非常快速的机器代码。
I would like to call floating point methods on floating point types in swift.
func example<T : FloatingPoint>(_ x:T) -> T {
return cos(cbrt(x + 1))
}
Is there a better way to do so than this?
protocol SupportsFloatingPoint : FloatingPoint {
var cubeRoot:Self { get }
var cosine:Self { get }
}
extension Double : SupportsFloatingPoint {
var cubeRoot:Double { return Darwin.cbrt(self) }
var cosine:Double { return Darwin.cos(self) }
}
extension Float : SupportsFloatingPoint {
var cubeRoot:Float { return Darwin.cbrt(self) }
var cosine:Float { return Darwin.cos(self) }
}
func cbrt<T : SupportsFloatingPoint>(_ x:T) -> T {
return x.cubeRoot
}
func cos<T : SupportsFloatingPoint>(_ x:T) -> T {
return x.cosine
}
func example<T : SupportsFloatingPoint>(_ x:T) -> T {
return cos(cbrt(x - 1))
}
Note that the addition operator is lost here. You can do -
, *
and /
but not +
on SupportsFloatingPoint types.
You don't need a new protocol. You can extend the existing FloatingPoint
protocol for supported types:
// Just for fun :)
prefix operator √
prefix operator ∛
extension FloatingPoint where Self == Double {
var squareRoot: Self { return sqrt(self) }
var cubeRoot: Self { return cbrt(self) }
var sine: Self { return sin(self) }
var cosine: Self { return cos(self) }
static prefix func √(_ x: Self) -> Self { return x.squareRoot }
static prefix func ∛(_ x: Self) -> Self { return x.cubeRoot }
}
extension FloatingPoint where Self == Float {
var squareRoot: Self { return sqrt(self) }
var cubeRoot: Self { return cbrt(self) }
var sine: Self { return sin(self) }
var cosine: Self { return cos(self) }
static prefix func √(_ x: Self) -> Self { return x.squareRoot }
static prefix func ∛(_ x: Self) -> Self { return x.cubeRoot }
}
print(Double.pi.cosine)
print(√25.0)
print(∛8.0)
Admittedly, there's a lot of code duplication, which I'm currently investigating how to minimize. On the bright side, at least this is all static, inlineable code that'll produce very fast machine code.
这篇关于如何在swift中为浮点值编写通用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!