[(int)Math.floor(x)] or just [x]?  
Author Message
Murat Tasan





PostPosted: 2003-10-31 6:15:00 Top

java-programmer, [(int)Math.floor(x)] or just [x]? 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
email***@***.com
email***@***.com
email***@***.com
http://genomics.cwru.edu

 
nos





PostPosted: 2003-10-31 7:48:00 Top

java-programmer >> [(int)Math.floor(x)] or just [x]? 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" <email***@***.com> 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
> email***@***.com
> email***@***.com
> email***@***.com
> http://genomics.cwru.edu
>


 
Raymond DeCampo





PostPosted: 2003-10-31 10:20:00 Top

java-programmer >> [(int)Math.floor(x)] or just [x]? 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