Started using jMP today and I happened to notice something weird. In the loop below I always get the following error:
“Possible loss of precision, required: int, found double”
Here’s the code:
[java] private void create_elliptical_galaxy(double num)
{
if(Global.stars.length < num) { // create new stars
for(double i = 0; i<num; i++) {
Star ns = new Star();
Global.stars = ns.Star(
((Math.random() * Global.universe_size) - Global.universe_size_half),
((Math.random() * Global.universe_size) - Global.universe_size_half),
((Math.random() * Global.universe_size) - Global.universe_size_half));
}
}
}[/java]
To “fix” the problem, all you have to do is change:
[java]for(double i = 0; i<num; i++) {[/java]
to
[java]for(int i = 0; i<num; i++) {[/java]
The error goes away.
But that’s not a fix. Generating stars for a galaxy will take a lot more than what an Interger can take. For now it’s not a problem since I can easily use an int for testing purposes.
I have looked into the forums and couldn’t find a report stating that. Sorry if my eyes deceived me. Also, I know some projects use a “Bug list software or website” but I didn’t find an indication of that for jMP. If there is, I will gladly fill it in.
Anyway, I hope this can help someone.
Uh, the bug is not in jMP but in your code. The “double” is a 64bit floating point value, an array cannot have a floating point index. “long” is a 64bit integer value.
Cheers,
Normen
The loop is fine, take a look at which line the error occurs and you’ll see that thats the case.
The issue is that you can’t use a double as an array index, its in this line:
Code:
Global.stars[i] = ...
Code:
Global.stars[ (int) i ] = ...
normen said:
Uh, the bug is not in jMP but in your code. The "double" is a 64bit floating point value, an array cannot have a floating point index. "long" is a 64bit integer value.
Cheers,
Normen
Yeah, you're right. Silly me. No idea why I choked on that one.
Although you're right, I'm wondering if the jMP could give a better explanation. Or a warning right away about the assignment of a double to an array.
Momoko_Fan said:
The loop is fine, take a look at which line the error occurs and you'll see that thats the case.
The issue is that you can't use a double as an array index, its in this line:Cast it to an intCode:Global.stars[i] = ...Code:Global.stars[ (int) i ] = ...
I'm not sure if casting would be the best idea here. I will instead use BigInteger as I just noticed that I was using Double elsewhere where I shouldn't. I guess I've been careless. *sigh*.