public class mjm
{
public static void main(String args[])
{
int a=48271;
int m=2147483647;
int s=1024;
double jm;// Jump Multiplier
double mjm=0;// Maximum Jump Multiplier
double aj;//a^j
int j=0;
int i;
int fms;
fms= (int)Math.floor(m/s);
for(i=fms-20000;i
aj=Math.pow(a,i);
jm=aj % m;
System.out.println(i+" jump multiplier: "+jm);
System.out.println((m % jm)+" , "+(m/jm));
if((m % jm)< (m/jm)){
j=i;
mjm=jm;
}
}
}
aj becomes too big as j increases.
When j>2, aj becames NaN(not a number).
How can I avoid this problem and calculate aj correctly?
2006-08-13
05:49:24
·
7 answers
·
asked by
JNz30_0
2
in
Computers & Internet
➔ Programming & Design
Thank you for everyone.
I have used long instead double and int.
It's not NaN anymore. It shows number.
however, when i>5.
aj becomes 9223372036854775807.
And it does not get bigger, it stays the same
Is there way to solve this problem?
Even If I use another approach,
I still have to compute a^i..
2006-08-13
10:09:41 ·
update #1
aj = 9223372036854775807 = 0x7FFFFFFFFFFFFFFF (HEX) (64 bit number)
you are hitting the biggest number a "double" can handle.
try making aj an unsigned double, or long double, or use float and use powers.
2006-08-14 02:15:40
·
answer #1
·
answered by justme 7
·
0⤊
0⤋
I don't think the problem is with the double. With my onscreen calculator, I tried 48271^10, and I got a valid result. So, a little debugging is in order. Print out the value of a and i right before you calculate aj. There might be something funny going on there.
An aside, the line
fms= (int)Math.floor(m/s);
can be written more simply as
fms= (int)(m/s);
Of course, if you don't want integer division, you might consider:
fms= (int)((double)m/s);
Lastly, the value of j is never actually used in the code. Did you know that? Perhaps that's part of your trouble.
Edit:
Java.lang.Long is not magic as celesoft seems to think. It is just a wrapper for the primitive type long, and you don't need the wrapper at all. Any floating point value will be truncated to an integer when cast to a long. However, using long, not java.lang.Long, might fix your troubles if they were due to integer overflow. I'd start with a little debugging, though. Good luck.
2006-08-13 13:33:04
·
answer #2
·
answered by arbeit 4
·
0⤊
0⤋
No, don't settle for just redesigning your code as some people suggest here. Java is a VERY robust language and certainly has tools for this kind of stuff.
In this case, use the java.lang.Long class.
A long can hold a value as high as 2^63-1, that's plenty.
It can hold floats, ints, doubles, etc.
Since you have some doubles, just call its floatValue() or doubleValue() method.
When it's an int, call its intValue() method.
Simply replace all your ints and doubles in your code with java.lang.Long objects and you're good to go.
2006-08-13 14:15:43
·
answer #3
·
answered by celesoft 1
·
0⤊
0⤋
well Im pretty sure your getting the NAN error because, like you say, the number your trying to store into the double is too large a number for the size of a double, causing overflow in the memory space which would distort your number if you were doing this in C++, but im assuming java has the safeguard and posts NaN instead.
Your best bet is to come up with another algorithm that dosen't allow the numbers to get so big. it is possible to break up large numbers into smaller groups of numbers, run your calculation sand merge them again, thereby avoiding the monster sized number that will cause the NaN error... good luck
2006-08-13 13:13:44
·
answer #4
·
answered by plainwolf 3
·
0⤊
0⤋
Maybe use a long instead of an int. Or using BigInt in java.util
2006-08-13 13:10:45
·
answer #5
·
answered by Brady 3
·
0⤊
0⤋
Huh!! is the answer even a number i will guess maybe 4 or 67498 thanx 2 points
i helped right?
2006-08-13 12:53:55
·
answer #6
·
answered by woman QandA 2
·
0⤊
3⤋
Hi, you should try using java.math.BigInteger or java.math.BigDecimal... it can handle very very large values.
2006-08-13 21:25:21
·
answer #7
·
answered by muoreh 2
·
0⤊
0⤋