jase71ds
Board Regular
- Joined
- Oct 23, 2006
- Messages
- 137
- Office Version
- 365
- Platform
- Windows
This isn't a question - just wanted to share a solution I came up with. Others have probably come up with the same solution, but I was pleased with myself for figuring it out on my own. Plus, it was fun!
If I want to constrain a formula output, say between +100% and -100%, one popular way to do it (there are many ways) is:
= IF( A1/B1 > 1, 1, IF( A1/B1<-1, -1, A1/B1 ))
This is not idea because you are potentially evaluating the formula three different times. And the odds are that your formula will be much messier than "A1/B1" so the computational overhead (and the formula ugliness) will grow.
You could use LET, and it would be better computationally, but still not as elegant (in my opinion).
So I considered MINs and MAXs and came up with this:
= MAX( MIN( A1/B1, 1 ), -1 )
In this example, A1/B1 is evaluated only once, and the output is constrained within the parameters of -100% and +100%.
It's rather elegant looking and far less computationally intensive for complex evaluations.
If I wanted to constrain within 0% and 100%, it would be:
= MAX( MIN( A1/B1, 1 ), 0 )
Or constrain within 500 and 10,000:
= MAX( MIN( A1/B1, 10000 ), 500 )
I remember it with this phrase: "Max Min, put your formula, Max Min"
That's always enough to jog my memory.
As I said, I'm sure others figured this out long ago, but it was fun for me to "discover" on my own.
Anyway, maybe this will be of use to someone.
Jase.
If I want to constrain a formula output, say between +100% and -100%, one popular way to do it (there are many ways) is:
= IF( A1/B1 > 1, 1, IF( A1/B1<-1, -1, A1/B1 ))
This is not idea because you are potentially evaluating the formula three different times. And the odds are that your formula will be much messier than "A1/B1" so the computational overhead (and the formula ugliness) will grow.
You could use LET, and it would be better computationally, but still not as elegant (in my opinion).
So I considered MINs and MAXs and came up with this:
= MAX( MIN( A1/B1, 1 ), -1 )
In this example, A1/B1 is evaluated only once, and the output is constrained within the parameters of -100% and +100%.
It's rather elegant looking and far less computationally intensive for complex evaluations.
If I wanted to constrain within 0% and 100%, it would be:
= MAX( MIN( A1/B1, 1 ), 0 )
Or constrain within 500 and 10,000:
= MAX( MIN( A1/B1, 10000 ), 500 )
I remember it with this phrase: "Max Min, put your formula, Max Min"
That's always enough to jog my memory.
As I said, I'm sure others figured this out long ago, but it was fun for me to "discover" on my own.
Anyway, maybe this will be of use to someone.
Jase.