Jump to content

If Then function


---
 Share

Recommended Posts

I am trying to use a result element to change a result to zero if it is negative. Can someone tell me how to fix this formula or where I can find a list of functions that can be used in a result element.

IfThen.pdf

Link to comment
Share on other sites

Edit: Norberts method is way better
Use a variable.

//in Presettings of the Result Element
result = getActual("Result Element1").actual

if result < 0 then
	result = 0
endif

Then put the variable "result" or whatever you want to call it in the result element formula
Link to comment
Share on other sites

If / then phrases do not work in formula fields, only in PCM parameter fields and files

To get your your desired result without using parameters, use the following alternative:
getActual("Result Element1").actual * ord(getActual("Result Element1").actual >= 0)
This multiplies the value of the first result element with the result of a test on whether it's positive.
Since comparisons return only "true" or "false", you need the ord() function to convert the boolean value into a number (true = 1, false = 0)
Link to comment
Share on other sites

Please sign in to view this quote.

So let me explain (because constructs like this one are extremely useful sometimes):

The formula works in three steps (marked in red):

1.) get the actual value of result element 1:
getActual("Result Element1").actual * ord(getActual("Result Element1").actual >= 0)

2.) Test whether the value of result element 1 is positive (it's the opposite of testing for "negativeness")
getActual("Result Element1").actual * ord(getActual("Result Element1").actual >= 0)

Why test for the opposite? Because we need the correct boolean value for the third step:

3.) Convert boolean result to a number and multiply it with the result of step 1:
getActual("Result Element1").actual * ord(getActual("Result Element1").actual >= 0)

So, if step 1 gives us a positive result, let's say 25.2, then step 2 returns "true". Step 3 converts the "true" to the number "1" and multiplies it with step 1: 25.2 * 1 = 25.2

If step 1 returns a negative number, say -25.2, then step 2 returns "false", which converts to "0" in step 3.
The following multilpication thus gives us -25.2 * 0 = 0
Link to comment
Share on other sites

Norbert: Nice job! I've never thought of that usage for ord(). I will remember that and probably use it sometime.

However in this case a simple max() would do the trick.

max(0,getActual("Result Element1").actual)

If getActual("Result Element1").actual is positive it will be bigger than 0 and the max will return the value. If it is smaller than 0 the max will return 0.
Link to comment
Share on other sites

Please sign in to view this quote.

Some programming languages with automatic datatype conversion even allow you to multiply with the boolean directly. I tried that first, but failed of course. I onlyl recently found ord() in the list of functions.

Please sign in to view this quote.

Very true. Amazing how even a short piece of code can be optimized even further 😃
Thumbs up!
Link to comment
Share on other sites

  • 2 weeks later...
Hello. I wonder if it is possible, without the PCM function, to construct a formula that, for example, will check getActual("Result Element1").actual > getActual("Result Element2").actual and if it is true, it will substitute getActual("Result Element3"). actual and if not, it will substitute getActual("Result Element4").actual. Do you have any idea for this?
Regards.
Link to comment
Share on other sites

Please sign in to view this quote.

I solved this problem using patterns in Result Element. Before I tell you how I did it, let me explain why I needed it. Well, I had the Max and Min dimensions selected from a certain number of points. Of these points, I had to choose the one that was the furthest from the deviation, for example 2. Therefore, I needed to calculate the absolute value for the Min and Max dimensions. And when I already knew the absolute value of the farthest deviation, I needed to replace it with the Min or Max deviation corresponding to the largest absolute value. For this purpose I used the function:

1 Max Result
2 Min Result

3 Result Element Absolute value Max
sqrt(squared(getActual("Max_1").actual-(getActual("2").actual)))
4 Result Element Absolute value Min
sqrt(squared(getActual("Min_1").actual-(getActual("2").actual)))
5 Result Element ORD Max
getActual("Max").actual * ord(getActual("War Bez_Max").actual >  getActual("War Bez_Min").actual)
6 Result Element ORD Max
getActual("Min").actual * ord(getActual("War Bez_Max").actual <  getActual("War Bez_Min").actual
7 Deviation
(getActual("ORD_Max").actual+getActual("ORD_Min").actual)
It is possible that there is an easier way to determine this deviation, but I haven't found it yet. I will add that I do not have the PCM function.

Regards
Link to comment
Share on other sites

This is a PCM solution but the last line is everything compressed in a single line which can be written into a single resultelement.

//Get the two values
Value1 = getActual("MAX").actual
Value2 = getActual("MIN").actual

//Create a list with the absolute values. indexOfMaxValue will give you the index of the largest absolute value (the biggest deviation).
WorstIndex = list(abs(Value1),abs(Value2)).indexOfMaxValue

//The worst value with its sign will be the same index of the same list but without the absolute values
WorstValue = getParameterNamed(list(Value1,Value2),WorstIndex)


//Now this can be substituted into one single long formula in a result element
getParameterNamed(list(getActual("MAX").actual,getActual("MIN").actual),list(abs(getActual("MAX").actual),abs(getActual("MIN").actual)).indexOfMaxValue)
Link to comment
Share on other sites

 Share

×
×
  • Create New...