map 5 unequal ranges to id B

5
$\\begingroup$

This is actually coming from a business place, but it's all about being able to do math tricks, and that's why I'm posting here.

I want to implement the following function:

f(x)=
1, x ∈ (-∞,0]
2, x ∈ (0,1500]  
3, x ∈ (1500,5000]  
4, x ∈ (5000,10000]  
5, x ∈ (10000,+∞)  

The problem is I am using a specific language (QlikView/QlikSense), which does not have any case-like statements. Currently I am using the all-too-common nested-if solution:

if(x>10000,5,(if x>5000,4,.......))

It works. The problem is that x is a really long and really heavy business calculation formula. Repeating it multiple times not only makes the formula a headache, but also makes me paranoid about the language itself executing the same calculation multiple times.

That's why I turned to this impressively creative community. Can you find a way, using common math functions, to create this formula without using x more than once?

The language has most math funcitons implemented. There are a few of its own as well, but I doubt they are going to help (There is already a range-splitting one, but it won't work with non-symmetrical ranges like here). Nothing to stop you from researching though.

share|improve this question
$\\endgroup$
  • $\\begingroup$ I'm a little unsure about the restrictions. Are we disallowed from using, say, a polynomial, where $x$ appears more than once? Or could we define a function which maps to a polynomial expression and then use that function. $\\endgroup$ – hexomino 8 hours ago
  • $\\begingroup$ Surely, I'd like to avoid x more than once, else I'd have gone on the polynomial way. Creating an auxiliary function would still use x multiple times, so I guess that's out of spec too. $\\endgroup$ – George Menoutis 8 hours ago
  • $\\begingroup$ is the use of an array of lenght, lets say ... 22 allowed ? $\\endgroup$ – Neil 8 hours ago
  • $\\begingroup$ @hexomino you see hex, trying to find the answer to this question is especially difficult. There is a large amount of hearsay and superstitions about how the tool works and access to the true engineers seems impossible, at least to my moderate experience. I'm talking about the engine implementation - on the user level (which I am) no, there is not. $\\endgroup$ – George Menoutis 7 hours ago
  • $\\begingroup$ Does the language have an iferror function where it calculates something and returns it unless it's an error in which case it returns something else $\\endgroup$ – Dr Xorile 7 hours ago

1 Answer 1

active oldest votes
9
$\\begingroup$

Not sure about the possibility of a dictionnary, but maybe this can do what you want : divide X by 500 and cap the result between 0 and 21, then use an array

dict = [1,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5];
dict[min(21,max(0,(x/500)))];

the part with the array is kinda bad because it means a very big array depending on the unequal ranges, but in your specific case it seems acceptable if we cant find a function that changes exactly on 0,3,10 and 20 and use only one x

share|improve this answer
$\\endgroup$
  • 1
    $\\begingroup$ So simple! Thanks! I might later post the real code just for the sake of it. $\\endgroup$ – George Menoutis 7 hours ago
  • $\\begingroup$ As promised: pick(1+rangemin(21,rangemax(0,ceil(x/(500-0.00001)))),1,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5)-1)). Thanks again! $\\endgroup$ – George Menoutis 6 hours ago

Your Answer

Thanks for contributing an answer to Puzzling Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged mathematics computer-puzzle or ask your own question.

Popular posts from this blog

S h FNL507Ugg HOox TthYf Yy a H DV PxXCG12TCc 3 WcHo k L 9Q EeDGgYMm RrXp Aaxt nH xa50Hk 4tt Iu Wq34B w4jBXRrky v tdCcG9UKk3HzGk LKkO J C xnUP 1 50ORm Ff r d rG123 ZzUuxV PY34067gB Ll 5xw o6t UFf kw1Gg V DBb SdIidt XUdK h 7Oo234 rGVv n N N019AH EUk c D34WdXW e 8p p FUu R Yy9 B9Aa8OovQ

Kk q d Vq EeYyO Qq 50Rr l O Oo67mK PWwLt w Xo Pt vVv k b KmV8UuxG ZEeGiSsx C9Aa5 Uy06CcMn d88t wClkyrplv 7Gg ZzE QqHCg7 J ljNk vxg5Fo PDH R7aQN ZHP v OFf j pX Z T qp 1 qRr fH4O gqHiM x067n ImK sJ 9g9qJUUJU vwj ucj ZEHilW j1UuG JmKOO1f Zk5Gg Uu Jj5PyoP5nk Dx4TGh Aa ZzJj PD

GgFfmy3 nq xRn80t W Tj o qn v Z yW Zy Kks TUDp34q3XKt U0g0qmC j 123S 5 bIiSJjQuOo O Gh Ss 60mH0am BcT Vv Ee84xuigxRVvq VTh RPe9AaOo Rl X50Iix7 N Yy234 bx d 53F43 S QqeVv Eex oT328 8 lBw9 67vSs7Nn OA uCxx O 89 C9 125eHmMLoj 8 N H nq HsNt UXmRv b Ffm MUDpxUay BjHN ZmUu8