I wonder isn't that easier in your particular case to precache the hash of indexes, like this:
var fpIndexes = ['0', '0.02', '0.04', '0.06'];
var fpIndexesHash = {};
for (var i = 0, l = fpIndexes.length; i < l; i++) {
fpIndexesHash[fpIndexes[i]] = i;
}
Then you'll be able to get the integer index out of floating-point value without doing Math.round
:
someValues[fpIndexesHash['0.02']]; // the same as someValues[1];
I've used strings here, but I actually think plain floating-point literals can be used here too: my understanding is that a specific floating-point literal will never be represented by different IEEE-754 values at the same machine during the lifetime of a script.
So even if 0.06
literal is actually represented as 0.060000001
number in memory, it'll be uniform - and will be converted to the same string value both in fpIndexesHash
assigning round and in the interpreting one.