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:
Commutative property: You're allowed to swap the operands.
Applies to addition and multiplication:
- a + b = b + a
- a * b = b * a
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)
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:
In mathematics and common programming languages operators are evaluated in this order:
- Paranthesis ()
- Exponents x2
- Multiplication * and Division /
- Addition + and Subtraction -
And then there is one other trick to it:
Express division in terms of multiplication:
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