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

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

Related

Swift is unable to type-check expression in reasonable time [duplicate]

This question already has answers here:
The compiler is unable to type-check this expression swift 4?
(15 answers)
Closed 26 days ago.
The following function triggers the error: "The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions"
func calculateCalSpent(age: Int, height: Int, currentWeight: Int, activityLevel: Int ) -> Int{
var cal = Int(66.47 + (13.75 * currentWeight) + (5.003 * height) - (6.755 * age))
print(cal)
return cal
// Men kg cm
}
When I put 1 instead of cal in return, everything is okay. What should I do?
You're mixing integers and floating point values. That seems to pose a problem for the type checker in this particular case.
Since you need floating point calculations here, explicitly cast all Int to Double to fix/work around this:
var cal = Int(66.47 + (13.75 * Double(currentWeight)) + (5.003 * Double(height)) - (6.755 * Double(age)))

"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.

Hints on getting basic arithmetic expressions to be parsed in 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))
}

type-check error after upgrading to Xcode 10

After upgrading to Xcode 10 and building my code
let nearestWedge: Int = Int(round(((currentRadians + (radiansPerWedge / 2)) + snappingPositionRadians) / radiansPerWedge))
I got this error:
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
This error appears when the swift compiler finds the expression too heavy to calculate it in a reasonable time
just break your expression in sub expression; one example could be:
let firstHalfOfExpression = (currentRadians + (radiansPerWedge / 2)) + snappingPositionRadians
let nearestWedge: Int = Int(round(firstHalf / radiansPerWedge))

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