swift - How to multiply, add etc... SignedNumber with Double in Swift?
问题描述
I try to build a bit universal interpolate function:
func interpolateNumber<T:SignedNumeric> (_ x0:T, with x1:T, bounds:ClosedRange<Double>, at:Double) -> Double{
return x0 + (x1-x0) * (at-bounds.lowerBound)/(bounds.upperBound-bounds.lowerBound)
}
but compiler complains:
Binary operator '*' cannot be applied to operands of type 'T' and 'Double'
Bounds.lowerBound and .upperBound are Double and they should be. How to apply '*' operator to SignedNumeric and Double?
解决方案
Unfortunately SignedNumeric
protocol doesn't come with /
and *
operators. So you'd have to cast your T
type to a more concrete one. Here's a not so pretty way of doing this:
func interpolateNumber<T: SignedNumeric>(_ x0: T, with x1: T, bounds: ClosedRange<Double>, at: Double) -> Double {
var x0Final: Double!
var x1Final: Double!
switch T.self {
case is Double.Type:
x0Final = x0 as! Double
x1Final = x1 as! Double
case is Float.Type:
x0Final = Double(x0 as! Float)
x1Final = Double(x1 as! Float)
case is Float32.Type:
x0Final = Double(x0 as! Float32)
x1Final = Double(x1 as! Float32)
case is Float64.Type:
x0Final = Double(x0 as! Float64)
x1Final = Double(x1 as! Float64)
case is Float80.Type:
x0Final = Double(x0 as! Float80)
x1Final = Double(x1 as! Float80)
case is Int.Type:
x0Final = Double(x0 as! Int)
x1Final = Double(x1 as! Int)
case is Int8.Type:
x0Final = Double(x0 as! Int8)
x1Final = Double(x1 as! Int8)
case is Int16.Type:
x0Final = Double(x0 as! Int16)
x1Final = Double(x1 as! Int16)
case is Int64.Type:
x0Final = Double(x0 as! Int64)
x1Final = Double(x1 as! Int64)
default:
fatalError("Binary operator '*' cannot be applied to operands of type '\(T.self)' and 'Double'")
}
return x0Final + (x1Final - x0Final) * (at - bounds.lowerBound) / (bounds.upperBound - bounds.lowerBound)
}
print(interpolateNumber(3.0, with: 4, bounds: 0...10, at: 0.7)) // Prints 3.07
print(interpolateNumber(3, with: 4, bounds: 0...10, at: 0.7)) // Prints 3.07
推荐阅读
- aws-cli - 使用 putty 在 aws 中将 .pem 文件转换为 .ppk 文件
- javascript - 如何访问 JavaScript 函数中生成的访问值?
- javascript - 有没有办法替换 JSON 中的所有负值?
- vue.js - 是否可以使用 nuxt js 生成正确的缩进代码
- c - CS50 问题集 4 - 过滤器(边缘)中的逻辑缺陷?
- node.js - 是否可以使用 Playwright 将 webkit 浏览器带到前台?
- c++ - 错误:“[”之前的预期 unqualified-id 和错误:“用户”未在此范围内声明
- pandas - 如何从数据集中找到异常值并使用 Z 分数绘图
- c - Git Bash 中的 C 程序无法正确接受输入
- swashbuckle - 使用 c.CustomOperationIds 时检查 OperationId 的唯一性?