kee*_*n3d 3 generics math swift
我试图围绕Swift泛型的来龙去脉,并制作一些通用的数学函数。我正在尝试实现一个mod功能,但不太确定使用泛型使其工作的最佳方法。
这是我的mod函数:
func mod<N: NumericType, I: IntegerType>(_ x: N, _ y: I) -> N {
return x - y * floor(x/y)
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
error: binary operator '/' cannot be applied to operands of type 'N' and 'I'
return x - y * floor(x/y)
Run Code Online (Sandbox Code Playgroud)
这是我NumericType对十进制和整数类型数字的声明:
protocol NumericType: Comparable {
static func +(lhs: Self, rhs: Self) -> Self
static func -(lhs: Self, rhs: Self) -> Self
static func *(lhs: Self, rhs: Self) -> Self
static func /(lhs: Self, rhs: Self) -> Self
static func %(lhs: Self, rhs: Self) -> Self
}
protocol DecimalType: NumericType {
init(_ v: Double)
}
protocol IntegerType: NumericType {
init(_ v: Int)
}
extension CGFloat : DecimalType { }
extension Double : DecimalType { }
extension Float : DecimalType { }
extension Int : IntegerType { }
extension Int8 : IntegerType { }
extension Int16 : IntegerType { }
extension Int32 : IntegerType { }
extension Int64 : IntegerType { }
extension UInt : IntegerType { }
extension UInt8 : IntegerType { }
extension UInt16 : IntegerType { }
extension UInt32 : IntegerType { }
extension UInt64 : IntegerType { }
Run Code Online (Sandbox Code Playgroud)
从Swift 3开始,所有浮点类型均符合FloatingPoint,所有整数类型均符合Integer。两种协议都定义了基本的算术运算,例如+,-,*,/。该floor()函数也为FloatingPoint
参数定义。
因此,在您的情况下,我将定义两种实现,一种用于整数,一种用于浮点值:
func mod<N: Integer>(_ x: N, _ y: N) -> N {
return x - y * (x/y) // or just: return x % y
}
func mod<N: FloatingPoint>(_ x: N, _ y: N) -> N {
return x - y * floor(x/y)
}
Run Code Online (Sandbox Code Playgroud)
FloatingPoint还有一个truncatingRemainder方法,
a.truncatingRemainder(b)就是a % b整数的“浮点等效” 。mod如果两个操作数具有相同的符号,则给出的结果与函数相同。