This site requires JavaScript, please enable it in your browser!
Greenfoot back
151mewtwo
151mewtwo wrote ...

2011/12/8

Java project

151mewtwo 151mewtwo

2011/12/8

#
can anyone help in for my project in java? i need to create a program that adds large numbers that doesn't normally fit on a calculator.. so my question is, How would i go about doing calculations with extremely large numbers in Java? i have tried long but that maxes out at 4869382068174930241, and when using an integer it does not save enough digits and therefore is not accurate enough for what i need. can anyone help me?
danpost danpost

2011/12/8

#
Did you try using String variables to hold the numbers, and work on the digits one at a time? (or in moderate sized groups of numbers).
darkmist255 darkmist255

2011/12/8

#
Well, instead of using an integer use a double, that has a much higher number limit and saves every digit. If you find a way to max out a double, I will mail you my house.
151mewtwo 151mewtwo

2011/12/8

#
this is the code i have though it can only hold 2147483647.. any number higher than that i get the error message stating "integer number too large" import java.math.BigDecimal; public class AddTwoBigNumbers{ public static void main() { BigDecimal num1, num2; num1 = new BigDecimal(2147483647); num2 = new BigDecimal(2147483647); Sum(num1, num2); } public static void Sum(BigDecimal val1, BigDecimal val2){ BigDecimal sum = val1.add(val2); System.out.println("Sum of two BigDecimal numbers: "+ sum); } }
151mewtwo 151mewtwo

2011/12/8

#
this is the code i have though it can only hold 2147483647.. any number higher than that i get the error message stating "integer number too large" import java.math.BigDecimal; public class AddTwoBigNumbers{ public static void main() { BigDecimal num1, num2; num1 = new BigDecimal(2147483647); num2 = new BigDecimal(2147483647); Sum(num1, num2); } public static void Sum(BigDecimal val1, BigDecimal val2){ BigDecimal sum = val1.add(val2); System.out.println("Sum of two BigDecimal numbers: "+ sum); } }
Builderboy2005 Builderboy2005

2011/12/8

#
darkmist255 wrote...
Well, instead of using an integer use a double, that has a much higher number limit and saves every digit. If you find a way to max out a double, I will mail you my house.
Double does *not* save every digit, it has a precision of 64 bits only, and does not save any more digits that that. It does use a floating point notation which allows it to achieve the high values that it does. To answer 151mewtwo's question, there is a class called BigInteger, which can store integers as large as you want. I think this will solve your problem, just make sure your instructor doesn't actually want you to write your own class instead of implementing another.
davmac davmac

2011/12/8

#
it has a precision of 64 bits only
Technically the precision is only 53 bits :) Only 52 bits of significand are actually stored because the first bit is always assumed to be 1 (except in the case of zero or "not-a-number" values); the rest of the 64 bits is comprised of 11 exponent bits and 1 sign bit. But that's getting off track... :) 151mewtwo, try: BigDecimal num1 = new BigDecimal("1000000000000000000000000000000000"); (for example). I'd also question whether it's BigDecimal or BigInteger that you should be using - if you don't need to support numbers with a decimal point, use BigInteger as it's slightly more efficient.
darkmist255 darkmist255

2011/12/9

#
:o. I learneded today. :D
151mewtwo 151mewtwo

2011/12/9

#
hey davmac, BigDecimal num1 = new BigDecimal("1000000000000000000000000000000000"); this code really worked... thanks.. and thanks also to all those who replied... appreciate it.. but i have one more thing, how to ask for user input for two numbers? and how to check if the user input is using decimals or just integers.. -thanks
You need to login to post a reply.