1

Error is with the multiplier variable. Why is it too complex for the compiler? How would I rewrite it? Should I post the entire drawRect func?

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

// Progress is a value between 1.0 and -0.5, determined by the current wave idx, which is used to alter the wave's amplitude.
        var progress = CGFloat(1.0 - Float(i) / Float(numberOfWaves))
        var normedAmplitude = (1.5 * progress - 0.5) * amplitude

        var multiplier = CGFloat(min(1.0, (progress / 3.0 * 2.0) + (1.0 / 3.0))) // error point
        waveColor.colorWithAlphaComponent(multiplier * CGColorGetAlpha(waveColor.CGColor)).set()
TokyoToo
  • 926
  • 2
  • 10
  • 21

2 Answers2

3

Just make the calculation of progress / 3.0 * 2.0 separately.

let calc = progress / 3.0 * 2.0
var multiplier = CGFloat(min(1.0, (calc) + (1.0 / 3.0)))

This error can occur when it´s too much calculations in a single row.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Hi. Your answer is very good because it's the shortest. I marked Mischa's answer as correct because it was broken down further which is what I need. I still up-voted your answer though. Thank you very much. – TokyoToo Jan 19 '16 at 21:16
  • @TokyoToo, np choose the answer that suits you best. – Rashwan L Jan 19 '16 at 21:39
2

Answer to the question

One way to rewrite your code and break it into smaller pieces:

let firstValue = CGFloat(1.0)
let secondValue = ((progress * 2.0) + 1.0) / 3
var multiplier = min(firstValue, secondValue)
waveColor.colorWithAlphaComponent(multiplier * CGColorGetAlpha(waveColor.CGColor)).set()

The compiler won't complain anymore.

In general, it's a good idea to write shorter lines of code because it's not only helping the compiler to resolve your expressions, it's also making it a lot easier for you or other programmers to understand what the code is doing. Would you know at the first glance, what CGFloat(min(1.0, (progress / 3.0 * 2.0) + (1.0 / 3.0))) means and why you're adding, multiplying and dividing by all those numbers if you look at the code in a month or two?

Here's a good explanation why this error occurs in the first place.


Algebraic excursion ;)

How to mathematically transform the algebraic expression for secondValue

You'll need these mathematical properties of algebraic operations:

  1. Commutative property: You're allowed to swap the operands.

    Applies to addition and multiplication:

    • a + b = b + a
    • a * b = b * a
  2. Associative property: The order in which you evaluate the expressions doesn't matter. You can add or remove parenthesis as you like.

    Applies to addition and multiplication:

    • (a + b) + c = a + (b + c)
    • (a * b) * c = a * (b * c)
  3. Distributive property: You're allowed to pull common factors out of parentheses.

    Applies to addition of two products with a common factor:

    • (a * c) + (b * c) = (a + b) * c

Furthermore you'll need the rules of operator precedence:

  1. In mathematics and common programming languages operators are evaluated in this order:

    1. Paranthesis ()
    2. Exponents x2
    3. Multiplication * and Division /
    4. Addition + and Subtraction -

And then there is one other trick to it:

  1. Express division in terms of multiplication:

    • a / b = a * (1 / b)

Now let's use these properties to transform your algebraic expression:

     (progress / 3 * 2)         +  (1 / 3)         
  =   progress / 3 * 2          +   1 / 3          | removed parentheses (4)
  =   progress * (1 / 3) * 2    +   1 / 3          | (5)
  =   progress * 2  * (1 / 3)   +   1 / 3          | swapped factors (1)
  =   progress * 2  * (1 / 3)   +   1 * (1 / 3)    | 1 * x = x
  =  (progress * 2) * (1 / 3)   +   1 * (1 / 3)    | added parenthesis (2)
  = ((progress * 2) + 1) * (1 / 3)                 | pulled common factor out (3)
  = ( progress * 2  + 1) * (1 / 3)                 | removed parenthesis (4)
  = ( progress * 2  + 1) / 3                       | (5)

And thus,

 (progress / 3.0 * 2.0) + (1.0 / 3.0) = ((progress * 2.0) + 1.0) / 3
Community
  • 1
  • 1
Mischa
  • 15,816
  • 8
  • 59
  • 117
  • Actually, I changed the correct answer to yours because it breaks it down the most which is what I need most during my coding journey. It's familiar to me because I do the same for creating MKCoordinateRegion. Set each piece of info as a separate property. – TokyoToo Jan 19 '16 at 21:14
  • The only thing I worry about when doing this is making sure the equation works out to be the same since it's like an algorithm I'm dealing with. Whereas the other answer keeps the same equation for progress. – TokyoToo Jan 19 '16 at 21:19
  • Not quite getting the algebraic expression you did. How is did you get to `((progress * 2.0) + 1.0) / 3`? You've switch division for multiplication which I get but how does that allow you to end up with only one divided by 3 operator? – TokyoToo Jan 19 '16 at 21:34
  • I only simplified the algebraic expression for `secondValue` because it's another opportunity to make the code a bit easier to read. It's not necessary though. Of course, you can simply assign the original expression `(progress / 3.0 * 2.0) + (1.0 / 3.0)` to secondValue and it will work as well. I'll add an explanation how to transform the algebraic expression to my answer in a minute. – Mischa Jan 19 '16 at 22:16
  • Yup Sorry I made you write all of that out lol I was just going to post that I already got it on paper. I always use PEDMAS. I just factored out 1/3. I know the algebraic excursion types. I mistakenly made it `(progress / (3.0 * 2.0)) + (1.0 / 3.0)` in my head. Thank you for explaining it though. It will be useful for someone who needs a refresher in Algebra. – TokyoToo Jan 20 '16 at 00:45
  • FYI, [it's not their code](https://github.com/jyunderwood/SiriWaveformView-Swift/blob/master/SiriWaveformView/SiriWaveformView.swift) that they're trying to compile. –  Jan 20 '16 at 02:51
  • @PetahChristian: As the SiriWaveformView library you posted which contains that particular piece of code is [open source](https://github.com/jyunderwood/SiriWaveformView-Swift/blob/master/LICENSE) and TokyoToo didn't claim it was his code in the question I think it doesn't matter who wrote it. Thanks for the information though. – Mischa Jan 20 '16 at 18:27
  • @PetahChristian lol 25% of all the questions on SO come from problems with Swift updates for open source Git projects. Are you the update police? You have a problem with people fixing code? – TokyoToo Jan 21 '16 at 20:03