Consider a measured value which can be between +/- MAX_V. I want to use this measure as an index to an array of BUCKETS length. Like:
Shouldn't Measure + MAX_V always be the corresponding index? The reverse would be index - MAX_V is the measure? Or do I misunderstand your question? -- Kuuran
I don't quite follow. Measured values are doubles, I forgot to say that. Risking adding some bias I might also mention that in my application of these functions I often have BUCKETS == 41 and MAX_V == 0.8143399. -- PEZ
let's see if I understand the question. (Warning: The following formula's are off the top off my head and haven't been verified)
v maps to index = (int)((v+MAX_V)/(2*MAX_V)*BUCKETS)
and
index maps to v = ((index+0.5)/BUCKETS)*2*MAX_V-MAX_V
Is this what you wanted to know? -- FnH
I'd try something like:
measure += MAX_V; measure /= 2 * MAX_V; measure *= BUCKETS; index = Math.round(measure);Which I believe is in essence the same as what FnH posted while I was typing. -- Kuuran
heh :) - great minds think alike :) -- FnH
Thanks. That's about what I do. I think my bug was in my testing code (sigh...). What's (index + 0.5) about? -- PEZ
Just so you use the average value of the bucket instead of the max or min value of it ... -- FnH