gpt4 book ai didi

swift - 为什么是 CFloat 和 CDouble? func addNewValue(newVal : CFloat, atTime time: CDouble) -> CFloat {}

转载 作者:行者123 更新时间:2023-11-28 05:28:43 24 4
gpt4 key购买 nike

我转换了

-(float) addNewValue:(float) newVal atTime:(double) time {

func addNewValue(newVal: CFloat, atTime time: CDouble) -> CFloat {}

没有错误,但我不知道为什么会这样。为什么 FloatDouble 变成了 CFloatCDouble

也没有错误:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com