here is the situation...
i have an array... and i select something from random from it.
i pick a random number x by using Math.random() and multiplying it by the
length of the array.
but this gives me a double, not an int.
so when i go to use the array, it needs an int.
in the language spec, it tells me that round-towards-zero is used when
going from floating point to integer, and the casting is done
automatically if possible.
so, given that x is a double, both of these methods are basically
equivalent:
obj = myArray[(int)Math.floor(x)]
and
obj = myArray[x]
only the top version is "safer" in some sense... it helps explain to
anyone who would ever look at the code (including the author when having
to back to it) what is going on.
now, intution says the bottom one is NO SLOWER than the top, because the
top may incur extra overhead due to the function call stack (for
Math.floor)... but i'm not sure. when the runtime environment does the
automatic rounding toward zero, does it basically go through the same
operations as Math.floor anyhow?
which one would most people here use... and most importantly why?
(note, i may come up with a small test program and try to benchmark the
two to get a quantitative answer, and i will post when i do.)
thanks again for all the help,
murat
--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu