Hints on getting basic arithmetic expressions to be parsed in Swift - swift

Consider the following expression:
let N = 2048
var c = (0..<N).map{ f -> Float in sin( 2 * .pi * f / (N/2)) }
Swift can not really parse it:
This is already a very small expression: it's absurd to break it into even smaller pieces. So I am trying to use type-casts. But I am getting weary of adding many explicit type casts :
let N = 2048
var c: [Float] = (0..<N).map{ f -> Float in
Float(sin( 2.0 * .pi * f / (Float(N/2)))) }
Even with the above the error continues
Why is swift so weak in parsing these simple arithmetic expressions? What can I do short of breaking it into pieces of the form
let c = a * b
let f = c * d
That is just too simplistic to be practical for signal processing. I am guessing that there were tricks to get the compiler to be a bit more intelligent: please do share.

The issue is that the arithmetic operators (+,-,* and /) have a lot of overloads. Hence, when you write expressions containing a lot of those operators, the compiler cannot resolve them in time.
This is especially true when you have type errors. The compiler tries to find the correct overload, but cannot do so, since your types are mismatching and there's no matching overload. However, by the time the compiler could infer this, it's already past the timeout for resolving expressions and hence you get that error instead of the actual type error.
As soon as you resolve the type errors by casting all Ints to Float, the single line expression compiles just fine.
let c = (0..<N).map{ f -> Float in sin( 2 * .pi * Float(f) / Float(N/2)) }
Once you do that, you don't even need the named closure argument and type annotation of the return value anymore.
let c = (0..<N).map{ sin(2 * .pi * Float($0) / Float(N/2)) }

That looks like java. What about
let N = 2048
var c = (0..<N).map{ f in
sin( 2.0 * .pi * Float(f) / Float(N/2))
}

Related

"The compiler is unable to type-check this expression in reasonable time" for a simple formula

I have what appears to be a rather simple arithmetic expression:
let N = 2048
// var c = (0..<N).map{ sin( 2.0 * .pi * Float($0) / (Float(N)/2.0)) }
let sinout = (0..<N * 10).map { x in
sin(2 * .pi * Float(x) / Float(N / 2))
}
But this is generating:
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
Why is such a simple equation not parse-able by the Swift compiler? How do we write equations that Swift can actually parse? This must be a major headache for persons writing DSP and/or linear algebra libraries: what workarounds or patterns do you use?
You just have to explicitly set the return type of your map expression:
map { x -> Float in
Sometimes it is hard for Swift to compile some seemingly easy code. The best thing you can do in those cases is modulate it in smaller chunks. I honestly think that this is an error that should be fixed but that for some reason is still there.

Swift Compiler Error: “Expression too complex” on a mathematical equation

When I add this equation
colViewHeight = (colItemSize * CGFloat(Counts)) + (colLineSpace *
CGFloat(Counts)) + (colViewTopSpace+colViewBottomSpace) as CGFloat
I get the below mentioned error.
The compiler is unable to type-check this expression in reasonable
time; try breaking up the expression into distinct sub-expressions
How to solve this issue? I am using xcode 10.01 version
Split it into multiple subexpressions and check if type casting is working fine
Such as:
let first = (colItemSize * CGFloat(Counts))
let second = (colViewTopSpace + colViewBottomSpace) as CGFloat
colViewHeight = first + second
Just delete the redundant bridge cast as CGFloat and the redundant parentheses
colViewHeight = colItemSize * CGFloat(Counts) + colLineSpace * CGFloat(Counts) + colViewTopSpace + colViewBottomSpace

Swift: Multiplication and brackets calculation doesn't work

This is a example equation which I want to be solved:
let equation = (5-2) * (10-5) / (4-2) * (10-5)
print (equation)
//35
The result which is printed is 35. But the right result would be 1,5. Whats wrong?
your expression is incorrect I hope you want the result 1.5
put '(' correctly * and / Precedence to execution are same but () is greater than * and /
let equation = ((5-2) * (10-5)) / ((4-2) * (10-5))
print (equation)
if you put the multiplication in another '()' then you will get result one perhaps the right part is integer so its auto conver to integer type
let equation = Double ( (5 - 2) * (10 - 5)) / Double ((4 - 2) * ( 10 - 5 ))
print (equation)
this code will print 1.5
Just look out operators Precedence in programming language
This should work:
let numerator: Double = (5-2) * (10-5)
let denumerator: Double = (4-2) * (10-5)
Fist you calculate the numerator and denumerator. And finally the result:
print(result)
let result: Double = numerator/denumerator
//1.5
As #araf has answered you should look out for the operator precedence in programming language.
Which follow a simple rule of the BODMAS evaluated in following order:
Brackets
Orders
Division and Multiplication (left to right)
Addition and Subtraction (left to right)
In your scenario:
let equation = (5-2) * (10-5) / (4-2) * (10-5)
the output is as follows:
3*5/2*4 = 15/2*5 = 7*5 = 35
#L.Stephan has suggested a better approach of calculating numerator and denumerator separately and then perform the division part.
To know more you can check this link:
https://en.wikipedia.org/wiki/Order_of_operations

CGFloat * 2 compiles, CGFloat * Variable where variable is an integer fails

I have to wrap my ints in CGFloat() to compile in Swift 2.3
If I just did * 2, then it would compile. Why does this happen?
Is this fixed in Swift 3?
var multiplier = CGFloat(3)
let y = collectionView.frame.origin.y + (cellSize() * multiplier)
Swift does not directly support mixed type arithmetic. Check what the type of 2 is in Swift, it's probably not what you assume. Your use of CGFloat() is converting the value so the operands of * have the same type.
HTH
type inference. When you write out * 2 without explicitly declaring it an int Swift infers it to be a CGFloat. However when you've already declared it to be an int you can't multiply a CGFloat with an Int.

How can I make this binary operator work with CGFloats?

I'm a beginner programmer, and I'm making a game at the moment. I haven't run into many errors like this, but I know it's really easy to fix.
Heres the code:
func randInRange(range: Range<Int>) -> Int {
return Int(arc4random_uniform(UInt32(range.endIndex - range.startIndex))) + range.startIndex }
Here is the constant I'm trying to work with:
let random = randInRange(self.frame.size.width * 0.3...self.frame.size.width * 0.6)
The error comes out as this: Binary operator '...' be applied to 2 CGFloat operands.
Your method randInRange is expecting a range of Integers, so you need to convert the result of your expression from CGFloat to Integer.
let random = randInRange(Int(self.frame.size.width * 0.3)...Int(self.frame.size.width * 0.6))