- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我转换了
-(float) addNewValue:(float) newVal atTime:(double) time {
到
func addNewValue(newVal: CFloat, atTime time: CDouble) -> CFloat {}
没有错误,但我不知道为什么会这样。为什么 Float
和 Double
变成了 CFloat
和 CDouble
?
也没有错误:
func addNewValue(newVal: Float, atTime time: Double) -> Float {}
那么我应该使用哪个呢?
完整代码
let maxPeriod:Double = 1.5
let minPeriod:Double = 0.1
let invalidEntry = -100
let maxPeriodsToStore = 20
let averageSize:Int = 20
class Detector {
let session = AVCaptureSession()
var camera : AVCaptureDevice?
var upVals = [averageSize]
var downVals = [averageSize]
var upValIndex: Int?
var downValIndex: Int?
var lastVal: Float?
var periodStart: Float?
var periods = [maxPeriodsToStore]
var periodTimes = [maxPeriodsToStore]
var periodIndex: Int?
var started: Bool?
var freq: Float?
var average: Float?
var wasDown: Bool?
func reset() {
for var i:Int = 0; i < maxPeriodsToStore; i++ {
periods[i] = invalidEntry
}
for var i:Int = 0; i < averageSize; i++ {
upVals[i] = invalidEntry
downVals[i] = invalidEntry
}
freq = 0.5
periodIndex = 0
downValIndex = 0
upValIndex = 0
}
// SO Question is regarding this function
func addNewValue(newVal: Float, atTime time: Double) -> Float {
// we keep track of the number of values above and below zero
if newVal > 0 {
upVals[upValIndex!] = newVal
upValIndex!++
if upValIndex >= averageSize {
upValIndex = 0
}
}
if newVal < 0 {
downVals[downValIndex!] -= newVal
downValIndex!++
if downValIndex! >= averageSize as! Int {
downValIndex = 0
}
}
// work out the average value above zero
var count: Float
var total: Float
for var i=0; i < averageSize; i++ {
if upVals[i] != invalidEntry {
count++
total += upVals[i]
}
}
var averageUp = total/count
// and the average value below zero
count=0;
total=0;
for var i=0; i < averageSize; i++ {
if downVals[i] != invalidEntry {
count++
total+=downVals[i]
}
}
var averageDown = total/count
// is the new value a down value?
if newVal < (-0.5*averageDown) {
wasDown = true
}
// is the new value an up value and were we previously in the down state?
if (newVal >= (0.5*averageUp) && (wasDown) != nil) {
wasDown = false
// work out the difference between now and the last time this happenned
if (time - periodStart) < maxPeriod && (time - periodStart) > minPeriod {
periods[periodIndex]=time-periodStart
periodTimes[periodIndex]=time
periodIndex++
if periodIndex >= maxPeriodsToStore {
periodIndex = 0
}
}
// track when the transition happened
periodStart = time
}
// return up or down
if newVal < (-0.5*averageDown) {
return -1
} else if newVal > (0.5*averageUp) {
return 1
}
return 0
}
最佳答案
So which am I supposed to use?
浮点型和 double 型。术语 CFloat 和 CDouble 只是它们的类型别名(同义词),因此没有必要使用它们;它们的存在仅仅是为了兼容性。
关于swift - 为什么是 CFloat 和 CDouble? func addNewValue(newVal : CFloat, atTime time: CDouble) -> CFloat {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873606/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我一直在做一个我继承的 angular 项目,我无法联系到原作者。有一个我不确定的 watch 表达式,我想知道是否有人可以帮助我理解代码: $scope.$watch(calculator.valu
我定义了一个模态窗口,用户在其中选择与之前选择的值不同的值。模态窗口应该在关闭后保留该值。我尝试使用 $scope.$watch 方法来实现此目的,但它似乎不起作用。我定义了下面的 Controlle
我有以下代码,其中包含 WebStorm 检查 Binary operation argument type newVal is not compatible with type string出现:
我正在解决必须找到列表中最长的字符串并用该字符串替换所有其他列表项的问题: The longest string in the list Inside the given method you sho
我转换了 -(float) addNewValue:(float) newVal atTime:(double) time { 到 func addNewValue(newVal: CFloat, a
我是一名优秀的程序员,十分优秀!