• Post Reply Bookmark Topic Watch Topic
  • New Topic

can java support tough calculations like this one?????  RSS feed

 
jatin rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had the following piece of code:
for(int l=a;l>=0;l--){
arg+= (int)Math.pow(3,l);
System.out.println("arg= "+arg+" l= "+l);
}
it worked fine for a=19, but after I made a=20 it gave negative results.
What's the reason & remedy to it?
1 more thing & that is that I tried 2 create 3 arrays of the size of arg but then it says out of memory error.
Well I'm using win 98 with 128 MB of RAM & I wasn't runing much of my programs while running this. So where has all the memory been consumed??
Please reply soon.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your first question - for a = 20 the correct result is 3890875847. This is too big to fit into an int value (maximum value 2147483647) so roundoff error occurrs. The value is converted to an int value by truncating all but the lowest 32 bits - which, as it happens, leaves a negative value in this case. Basically, if you overflow the value of an int (or other integral type), the value is meaningless. The simple workaround in this case is to declare arg as long rather than int. If that eventaully overflows as well, switch to BigInteger (which will require other code changes as well).
As for your second question - let's see, for a = 19 the value of arg is 1743392200. Are you saying that you tried to create three arrays of size 1743392200, and you're not sure why you would run out of memory? Perhaps I have misunderstood your question.
 
jatin rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
For your first question - for a = 20 the correct result is 3890875847. This is too big to fit into an int value (maximum value 2147483647) so roundoff error occurrs. The value is converted to an int value by truncating all but the lowest 32 bits - which, as it happens, leaves a negative value in this case. Basically, if you overflow the value of an int (or other integral type), the value is meaningless. The simple workaround in this case is to declare arg as long rather than int. If that eventaully overflows as well, switch to BigInteger (which will require other code changes as well).
As for your second question - let's see, for a = 19 the value of arg is 1743392200. Are you saying that you tried to create three arrays of size 1743392200, and you're not sure why you would run out of memory? Perhaps I have misunderstood your question.


thanx for your answer. Just lemme know that how u reached that final value for int? I mean how can I calculate that? the other thing is that is it possible to create an array with long as it's size??? What is BigInteger? I don't think that it's a primitive datatypein java! Is it a class implementation or what? Please elaborate!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, the simple workaround in this case is to declare arg as long rather than int. This allows you to calculate answers for much higher values.
Regarding BigInteger - did you try looking it up in the API? This should always be your first choice for answering questions like this - it's the Java programmers single most valuable resource, I think.

[This message has been edited by Jim Yingst (edited July 06, 2001).]
 
jatin rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx. but u left out 1 point & that is can we make an array with long variable giving it's size!!!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well of course you can. But if you try to create an array of size 1743392200 (or other huge number, it won't matter if you declare the varible which holds the 1743392200 as int or long - it's just too big to create an array that size without running out of memory. I think you will need to reconsider your design.
Also, if you want to know if an array can be declared with a long, it's really easy to test this yourself. You need to read responses carefully, and then try to test new ideas, rather than simply asking more questions immediately. You will gain knowledge much more quickly this way, and you will be better prepared to function in a world where others can't always give you the answers you need.
 
jatin rai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot jim for ya advice & answers. Hey I've 1 more question which I've long tested. When I try to declare float a= 2.0 compiler dosen't allow me 2 do that but I can declare it as a double. What's the reason behind this?

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, a literal numeric value with a decimal point in it (like "2.0") is assumed to be a double. To convert a double to a float, you would need a cast:
<code><pre> float a = (float) 2.0;</pre></code>
However, the more "correct" solution in this case is to declare the 2.0 as a float to begin with, using the "f" (or "F") which indicates this for numeric literals:
<code><pre> float a = 2.0f;</pre></code>
[This message has been edited by Jim Yingst (edited July 09, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!