Reputation: 613
I am implementing AudioVisualizer
and I am trying to calculate time of audio but I am getting below error
Binary operator '*' cannot be applied to operands of type 'Float' and 'Double'
Here is my code:
var lowPassReslts: Double = 0.0
var lowPassReslts1: Double = 0.0
let ALPHA: Float = 1.05
let averagePowerForChannel = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 0)))
lowPassReslts = ALPHA * averagePowerForChannel + (1.0 - ALPHA) * lowPassReslts //HERE i am getting error which i mention above
let averagePowerForChannel1 = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 1)))
lowPassReslts1 = ALPHA * averagePowerForChannel1 + (1.0 - ALPHA) * lowPassReslts1 //HERE i am getting error
This above code have converted from Objective-C. This code works perfect in Objective-C but it is not working in Swift. Can anyone please tell me how to solve this error?
Upvotes: 0
Views: 1908
Reputation: 2042
Make your parameter type of ALPHA as Double
or change your lowPassReslts as Float
.
Try the following code:
var lowPassReslts: Double = 0.0
var lowPassReslts1: Double = 0.0
let ALPHA: Double = 1.05
let averagePowerForChannel = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 0)))
lowPassReslts = ALPHA * averagePowerForChannel + (1.0 - ALPHA) * lowPassReslts
let averagePowerForChannel1 = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 1)))
lowPassReslts1 = ALPHA * averagePowerForChannel1 + (1.0 - ALPHA) * lowPassReslts1
In swift there is no need to declare the variable type. You can directly declare the variable as,
var lowPassReslts = 0.0
var lowPassReslts1 = 0.0
let ALPHA = 1.05
Try any of the above method. Hope it will help you.
Upvotes: 0
Reputation: 8914
Unlike in objective C, you can not do operation on 2-different type of variables (as Swift Language is Type-Safe).
To do any arithmetic calculation the type of both operand must be same.
If you do not specify the type of the variable then swift inferences the type from its value.
For eg.
let a = 10 //Swift consider it -> Int type
let b = 20.0 //Swift consider it -> Double type
let result = a * b //Gives compile time error for the operand type
let result = Double(a) * b //No error, as Int is converted to Double
Upvotes: 1
Reputation: 557
You need to make same data type for both like both are double or both are float.
Upvotes: 0
Reputation: 370
Pow(Double, Double) it accepts the double and returns the double.
let ALPHA: Float = 1.05, you explicitly declared the Float(change the naming convention, should not be all caps).
in swift, you cannot do any binary operation with two different types.
Solution: change the alpha type to double or don't declare the data type else convert it to double during operation.
Upvotes: 0
Reputation: 715
var lowPassReslts: Double = 0.0
var lowPassReslts1: Double = 0.0
let ALPHA: Float = 1.05
let averagePowerForChannel = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 0)))
lowPassReslts = Double(ALPHA * Float(averagePowerForChannel) + (1.0 - ALPHA) * Float(lowPassReslts))
let averagePowerForChannel1 = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 1)))
lowPassReslts1 = Double(ALPHA * Float(averagePowerForChannel1) + (1.0 - ALPHA) * Float(lowPassReslts1))
Upvotes: 1