Author |
Message |
wuZheng
|
Posted: Tue Apr 11, 2006 5:14 pm Post subject: Multiplication Issue |
|
|
Alright, I'm in a computer science class for grade 11 so I pretty much know only the basics of java. But yet I have found a problem. I wish to know why when using the operator for multiplication (*) that apparently the maximum result possible is 2147441940. (46340 by 46341, this took painstakingly long to figure out). Using figures bigger than the ones above to create a product results in java printing a negative number to the screen for some reason. Any help would be much appreciated.
Thanks.
wuZheng
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
MysticVegeta
|
Posted: Tue Apr 11, 2006 5:23 pm Post subject: (No subject) |
|
|
Please post your code
|
|
|
|
|
|
wtd
|
Posted: Tue Apr 11, 2006 5:30 pm Post subject: (No subject) |
|
|
You will need to understand how integers are handled by computers.
|
|
|
|
|
|
wuZheng
|
Posted: Tue Apr 11, 2006 5:31 pm Post subject: (No subject) |
|
|
public class test{
public static void main(String[] args){
// Variables
int intValue;
int intValue2;
int intAnswer;
int intAnswer2;
int intAnswer3;
// Proof
intAnswer = 46341 * 46340;
intAnswer2 = 46341 * 46341;
intAnswer3 = 50000 * 50000;
System.out.println(intAnswer);
}
}
That would be my input, this is the result when ran. (Command line):
2147441940
-2147479015
-1794967296
Press any key to exit
See how the first answer is totally right but the second one is totally wrong in every way. And that the figures sent in only differ by one in only one of the terms used? I put a higher number to make another example. I haven't encountered this until now where I find that I need it.
This is a simple code which will eventually become part of a much larger assignment. (Attached in case you would like to view it)
Description: |
Assignment in which multiplication is incorporated. |
|
Download |
Filename: |
MathTrainingGame.java |
Filesize: |
5.95 KB |
Downloaded: |
161 Time(s) |
|
|
|
|
|
|
wuZheng
|
Posted: Tue Apr 11, 2006 8:17 pm Post subject: (No subject) |
|
|
Errr since I couldn't find the edit button, what do you mean by learning how integers are handled by computers? The calculator, Google, Windows Calculator, etc. all find the correct answer, and parallel each other but java churns out negatives.
|
|
|
|
|
|
wtd
|
Posted: Tue Apr 11, 2006 9:19 pm Post subject: (No subject) |
|
|
How are integers represented in a computer's memory?
|
|
|
|
|
|
wuZheng
|
Posted: Tue Apr 11, 2006 9:31 pm Post subject: (No subject) |
|
|
Oh okay I get it, so integers are limited to the amount of addressing size the memory has so pretty much 32bit.... maximum of 46340.95 squared... I see so in java do I use long to overcome this?
|
|
|
|
|
|
wtd
|
Posted: Tue Apr 11, 2006 9:48 pm Post subject: (No subject) |
|
|
Well, an integer is stored in 32 bits of computer memory. What happens when you try to store a number in an "int" that requires more than 32 bits? Good question. The result won't be meaningful in any particular way.
So, if you need more space than a 32-bit int can provide.... then the answer should be fairly obvious.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Justin_
|
Posted: Tue Apr 11, 2006 10:27 pm Post subject: (No subject) |
|
|
yeah long would be the answer... Also i was under the impression that it loops the answer, so if you exceed that maximum possible 16 bit (signed int) by one then it should spew out the lowest negative number that can be represented by 16 bits. If it were unsigned then it should be 0, if you exceeded the maximum number that can be represented by 32 bits by one.
At least this is how it works under certain conditions in C++, but Java is likely a lot different in this regard.
|
|
|
|
|
|
wuZheng
|
Posted: Tue Apr 11, 2006 10:46 pm Post subject: (No subject) |
|
|
Thanks to you all for your help, was a bit confused at first and then you all reminded me of the limitations of int and 32-bit.... ><" I feel pretty out of it lol. Oh well, I guess I can get a level five in my multiplication game after all .
|
|
|
|
|
|
[Gandalf]
|
Posted: Tue Apr 11, 2006 11:17 pm Post subject: (No subject) |
|
|
You may find this useful in the future:
Java: | public class TestLimits {
public static void main (String[] args ) {
System. out. println("Max byte value is " + Byte. MAX_VALUE);
System. out. println("Max short value is " + Short. MAX_VALUE);
System. out. println("Max integer value is " + Integer. MAX_VALUE);
System. out. println("Max long value is " + Long. MAX_VALUE);
System. out. println("Max float value is " + Float. MAX_VALUE);
System. out. println("Max double value is " + Double. MAX_VALUE);
}
} |
|
|
|
|
|
|
|