By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

[(int)Math.floor(x)] or just [x]?

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
nos
I would go with Math.floor(x) cuz when I was a
baby learning FORTRAN my teacher told us not to use mixed mode
and to always put the dot in floating point numbers or
the compiler will get cha.

"Murat Tasan" <ta***@eecs.cwru.edu> wrote in message
news:Pine.SOL.4.53.0310301707210.3471@homer...
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

Jul 17 '05 #2

P: n/a
Murat Tasan wrote:
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,

Thanks for asking...I was tempted to weigh in on this to reply to your
post on maps and the toArray() method.

Personally, I would not use either method but instead use the
Random.nextInt(int) method:

Random random = new Random();
for (;;)
{
obj = myArray[random.nextInt(myArray.length)];
}

I would use this for two reasons:

1) It is clearer to read.
2) Using manipulations like multiplying upon random numbers to get a
range can sometimes result in the generated numbers not being "truly
random" (whatever that means...). I do not know off the top of my head
if the Math.random() algorithm used the way you propose has this problem
or not, but I do know that Random.nextInt(int) promises to be uniformly
distributed. So I would use Random.nextInt(int).

Now, are you still hiding your member variables with local variables? :)

Ray

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.