user10641275
user10641275

Reputation:

Creating formula

I have, say, a variable X, a variable Y and a variable Z.

If Y = 1 then Z = 0.

Now all of these examples below are if Y is strictly higher than 1.

* If X = 1 and Y >= 2, then Z = 1

* If X = 2 and Y >= 4, then Z = 2
(If X = 2 and Y < 4 , then Z = 1)

* If X = 3 and Y >= 6, then Z = 3
(If X = 3 and Y < 6 but >= 4, then Z = 2)
(If X = 3 and Y < 4 but >= 2, then Z = 1)

* If X = 4 and Y >= 8, then Z = 4
(If X = 4 and Y < 8 but >= 6, then Z = 3)
(If X = 4 and Y < 6 but >= 4, then Z = 2)
(If X = 4 and Y < 4 but >= 2, then Z = 1)

etc etc  ...

I'm trying to create a formula to generate that variable Z depending on the information above.

I've tried doing the following :

$Z = $X > $Y ? $Y : floor($X / $Y);

At first I thought this was doing what I want, but apparently not. Any idea what I'm doing wrong and how can I solve this issue?

Upvotes: 0

Views: 62

Answers (1)

user4668606
user4668606

Reputation:

First we need to analyze the conditions and what each variable does.

  • X apparently provides the upper bound for Z, so Z = min(X, ???)
  • conditions are only provided for Y >= 2, with Z being fixed as 0, if Y = 1
  • floor(Y / 2) = Z within the above range

So Y and X must be at least 1 and the range of Z is [0, X].

Putting this all together:

if Y < 1 or X < 1:
    some error ???
elif Y == 1
    Z = 0
else
    Z = min(X, floor(Y / 2))

Upvotes: 1

Related Questions