@genovad
Somewhere, there's a lengthy discussion about using formulas with IF (I probably contributed heavily to it), but I don't remember where, so I'll try a short explanation here and hopefully someone can find the other one if necessary.
The IF() function needs a boolean condition, a true or a false. Mathematically, those are normally 1 and 0, but anything other than 0 is considered true.
You can not use relational operators like < or >.
The logic here gets a little convoluted
- If 3x < y then 3x - y < 0. If 3x > y then 3x - y > 0.
- The signum(x) function, often written as SIGN(x), returns -1 if x < 0, +1 if x > 0, and 0 if x = 0.
- SIGN(3x-y) is -1 when 3x<y, +1 when 3x>y, and 0 when 3x=y
- Both -1 and +1 are considered true
- 1+SIGN(3x-y) is 0 (false) when 3x<y, 2 (true) when 3x>y, and 1 (true) when 3x=y
- SIGN(3x-y)-1 is -2 (true) when 3x<y, 0 (false) when 3x>y, and -1 (true) when 3x=y
- Canvas does not have the SIGN(x) function, so you have to create your own. SIGN(x)=x/ABS(x)
- 1+(3x-y)/ABS(3x-y) is 0 (false) when 3x<y, 2 (true) when 3x>y, and undefined because you get division by 0 when 3x=y
- To avoid division by 0, first check to see if 3x=y or 3x-y=0 (false) or not zero (true).
- Nest the check for less than inside the check for equality
IF(3x-y,IF(1+(3x-y)/ABS(3x-y),do_when_3x>y,do_when_3x<y),do_when_3x=y)
Replace the do_when_ blocks with whatever formula you need.
If you can guarantee that 3x is never y, then you can omit the outside IF() and just use the inner.
IF(1+(3x-y)/ABS(3x-y),do_when_3x>y,do_when_3x<y)
I would include both since it doesn't cost any extra except at the time you generate the question.
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.