# Can I set a min or max value on the result of an expression? I want to keep it within a range

#1

Is there a way to make it so that the result of my expression outputs within a min/max range?

For example, I have a bunch of calculations in an expression that typically report a range of 100 to 200. On some occasions, conditions are such that the calculated result ends up outside of that range, say 94 or 210. Is there a way to make it so that the result of the expression will “round” to be within a min/max range?

#2

Ternary?

{((x + 50) > 200 ? 200 : ((x + 50) < 100 ? 100 : (x+50)))}

Edit: better fit to the example…

#3

Interesting! I’ll give that a go!

#4

You may want to set a variable with the calculation first… more efficient.

x = a + b + c + d + e + f + g
x = {(x > 200 ? 200 : (x < 100 ? 100 : x))}

#5

Got it. Seems to be working out… Thanks folks!

#6

I am not sure this is what you are after, but maybe it will help someone.

Here is a different approach method that I love and use frequently.
(Step 1-4 is done once on paper w/calculator, step 5 is done in webCoRE)

1. Find/decide on the absolute highest result
2. Find/decide on the absolute lowest result
3. Highest - Lowest = Difference
4. 100 / Difference = varMultiplier
5. Take your output, subtract Lowest, and then multiply by varMultiplier

This method will force all your outputs to be in the range of 0-100.
If you want it to be in the range of 100-200, simply add 100 to the final results.

So here is an example from one of my pistons. It monitors the distance between the Moon and the Earth, and stores it in my variable called ‘distance’. I know from a bit of research, that the nearest the Moon will be to earth is 221,441 miles, and the farthest will be 252,724 miles. So my preliminary math is:
Highest - Lowest = Difference
252,724 - 221,441 = 31,283 miles difference

Then, I decided that I want all results to be displayed somewhere between 0 and 100, so my next math is:
100 / Difference = varMultiplier
100 / 31,283 = 0.00319662 <-- This last number will be used in webCoRE

Ok, so finally, we can work on the piston. Get your output like normally, and save to a variable. (In my example below, it’s called ‘distance’) Then, the code in webCoRE will be:
(yourOutput - Lowest) * varMultiplier
(226758 - 221441) * 0.00319662

That is basically it! This code works for anything you can think of. The key elements is getting the absolute maximum and minimum correct, and then deciding on what scale you want it displayed. (I like 0-100 personally) Also note, if you tweak or change the min/max numbers, you will have to do steps 3 & 4 again, and place the new varMultiplier into your webCoRE piston.

I have highlighted the code (yourOutput - Lowest) * varMultiplier in the picture below.

I went a step farther because I wanted to round my results to two decimals, and invert the answer, so that’s why there is the extra “round(100 - ( … ),2)” in the picture above…

And for those curious, here is an example of my outputs. It will ALWAYS be between 0 and 100.

Gauge for Length of Day (showing Solstices & Equinoxes)