# Finding the closest number - How to make it show the two options when just in the middle of two numbers?

Mario Fernando Cardoso

1月 21 2022 | 3:46 午前

These are very good ways to find the closest number to certain number.

However, when the input number (the one you want to go to its closest number) its exactly in the middle, just between two other numbers (for example, 7 is just between 5 and 9) it will always go to the lowest closest number.
Any ideas of how to make it choose the upper one??? For example, 7 going to 9 instead of 5. Or way better... Any ideas of how to make it show the two options?
Make it show the two options will be way better.
Thanks!

schlam

1月 21 2022 | 1:08 午後

hello
I don't know if it's useful..

Mario Fernando Cardoso

1月 21 2022 | 5:28 午後

Hi.
It is not useful for what I'm looking for.
The goal is to still be able to show the closest number and if the input is just in the middle of two numbers, then, show the two options.

Mario Fernando Cardoso

1月 21 2022 | 5:31 午後

This is the best solution so far... Provided by Italo Lombardo
The toggle option to show the two closest numbers (the upper and the lower ones) will only work when the input number is just in the middle of two numbers (e.g. 7 is just between 5 and 9), but that was the goal actually, so its perfect.

Roman Thilenius

1月 21 2022 | 7:21 午後

if x is greater than y (or equal to) AND also smaller than the middle between y and z, then output y, alternatively, if x is smaller than z AND also greater than the middle between y and z, then output z

[expr ( (\$f1>=\$f2) && (\$f1<((\$f2+\$f3)/2.)) ) * \$f2 + ( (\$f1>\$f3) && (\$f1> ((\$f2+\$f3)/2.)) ) * \$f3]

Roman Thilenius

4月 12 2023 | 3:44 午後

"output whichever of A, B, C or D is closest to X"

-> which is the smallest: the difference of a and x, b and x., c and x, or d and x?

difference to x:
(abs(\$f1-\$f5))

output only the smallest of 4 numbers:
min((min(\$f1, \$f2)), (min(\$f3, \$f4)))

afte comparing all to find the smallest, we now need to check which of the input values causes that smallest difference in order to send that out.

[expr (((min((min((abs(\$f1-\$f5)),(abs(\$f2-\$f5)))),(min((abs(\$f3-\$f5)),(abs(\$f4-\$f5))))))==(abs(\$f1-\$f5)))*\$f1) + (((min((min((abs(\$f1-\$f5)),(abs(\$f2-\$f5)))),(min((abs(\$f3-\$f5)),(abs(\$f4-\$f5))))))==(abs(\$f2-\$f5)))*\$f2) + (((min((min((abs(\$f1-\$f5)),(abs(\$f2-\$f5)))),(min((abs(\$f3-\$f5)),(abs(\$f4-\$f5))))))==(abs(\$f3-\$f5)))*\$f3) + (((min((min((abs(\$f1-\$f5)),(abs(\$f2-\$f5)))),(min((abs(\$f3-\$f5)),(abs(\$f4-\$f5))))))==(abs(\$f4-\$f5)))*\$f4)]

not too convincing, because it breaks the size limit of expr.

so i would either exclude the abs(...) in 4 extra expr objects, or build it around pack/vexpr/minimum (the latter takes lists, too)

Roman Thilenius

4月 12 2023 | 4:03 午後

splitting it into 3 parts looks much better.