473,508 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[(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
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
2 6108
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2459
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a -...
6
7316
by: Simon | last post by:
Hi all I am writing a small app that uses real numbers all over the place for calculations. But when it comes to displaying values it is better to only display an it, (especially when it...
13
2528
by: ern | last post by:
Anybody know a quick and dirty function for going from "547.6458679" to the integer version 548 ? i.e. int returnIntegerEquivalent(char * charNum){ int intNum; //Do some stuff... return...
7
2586
by: Wernfried Schwenkner | last post by:
I've found the discussion about Math.Log and the error with Math.Log(8,2) on Google. Unfortunatly the full thread isn't on my news server, so I can't reply. The problem doesn't only depend...
3
3674
by: Dave | last post by:
How do I convert a decimal number to an integer.
5
1591
by: Tom Gurath | last post by:
http://osnews.com/story.php?news_id=5602&page=2 This benchmark tests the Math & File I/O of 9 languages/run-times. Visual C++ (Version 7 - not managed) Visual C# gcc C Visual Basic.NET Visual...
8
2320
by: brad | last post by:
How does one make the math module spit out actual values without using engineer or scientific notation? I get this from <code>print math.pow(2,64)</code>: 1.84467440737e+19 I want this:...
5
1494
by: Jon Slaughter | last post by:
I wrote a routine to replace Math's Exp method but it turns out to be almost 2x slower ;/ (well, actually its about 1.5x in release) I'm essentially using a lookup table and interpolate between...
13
6937
by: raylopez99 | last post by:
Refer to Int32.TryParse method: http://msdn.microsoft.com/en-us/library/zf50za27.aspx if you have to, specifically this example: numericString = "10345.72" styles = NumberStyles.Integer Or...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.