This is a bit embarassing, but I get so strange results with my methods for it so I just have to ask you others how you would do it.

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:

- Measure == -MAX_V => index = 0
- Meaure == MAX_V => index = BUCKETS - 1;
- Measure == 0 => index = (BUCKETS - 1) / 2

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 ... (in order to compensate for the rounding in the other direction) -- FnH