Can I force precision loss?

S

stevek

CODE SEGMENT:

for(int i = 0; i < cycles ; ++i) {
int counterarray[] = new int[cycles];
counterarray[] = counterarray[Math.round(Math.random()*i)];
// if(i==0) continue;

ERROR MSG:
E:\javastuff\dev\javac RandomCheck.java
RandomCheck.java:21: possible loss of precision
found : long
required: int
counterarray[ Math.round(Math.random()*i) ]++;
1 error
 
M

Mike Schilling

Ryan Stewart said:
[...]
RandomCheck.java:21: possible loss of precision
[...]
It's called casting:

long l = 500;
int i = (int) l;


Casting long to int doesn't affect precsision, of course; either the
conversion is exact or the result is completely different.

double d;
float f = (float)d;

would reduce precision.
 
T

Tony Morris

Mike Schilling said:
Ryan Stewart said:
[...]
RandomCheck.java:21: possible loss of precision
[...]
It's called casting:

long l = 500;
int i = (int) l;


Casting long to int doesn't affect precsision, of course; either the
conversion is exact or the result is completely different.

double d;
float f = (float)d;

would reduce precision.

The compiler is issuing you with an error because your type conversion loses
information (64-bit type can't fit in a 32-bit type obviously).
By providing the explicit cast, you acknowledge the "loss of precision"
(theoretically speaking).
 
Y

Yu SONG

stevek said:
CODE SEGMENT:

for(int i = 0; i < cycles ; ++i) {
int counterarray[] = new int[cycles];
counterarray[] = counterarray[Math.round(Math.random()*i)];
// if(i==0) continue;

ERROR MSG:
E:\javastuff\dev\javac RandomCheck.java
RandomCheck.java:21: possible loss of precision
found : long
required: int
counterarray[ Math.round(Math.random()*i) ]++;
1 error

Why not use java.util.Random?

--
Song

/* E-mail.c */
#define User "Yu.Song"
#define At '@'
#define Warwick "warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top