473,569 Members | 2,761 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.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.edu
http://genomics.cwru.edu

Jul 17 '05 #1
2 6111
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.cwr u.edu> wrote in message
news:Pine.SOL.4 .53.03103017072 10.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.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.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
2469
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 - b. But for float the results would have to be normalize some how to a 32bit range. I understand there would be percision errors. Thanks Tom
6
7323
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 comes to % values and so on). so I first thought that the best way would be... ....
13
2536
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 intNum; }
7
2591
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 on representation of floating numbers. When You execute
3
3680
by: Dave | last post by:
How do I convert a decimal number to an integer.
5
1602
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 J# Java 1.3.1 Java 1.4.2
8
2324
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: 18,446,744,073,709,551,616 I'm lazy... I don't want to convert it manually :)
5
1501
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 integer values(but even just looking up its still as slow). public static double Exp(double x) { int n = (int)Math.Floor(x); //if ((n 2351) ||...
13
6955
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 NumberStyles.AllowDecimalPoint CallTryParse(numericString, styles) Which failed to convert the string "10345.72" to the int 10345, because of the...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.