Bigger than int
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
Bigger than int
i'm new to java and have been trying to compile a application to work out the average number of heart beats a human has. the file works but int is not big enough for the data i'm using. is anyone able to help me with a command that is bigger than int?
- s_reynisson
- Posts: 939
- Joined: Tue May 06, 2003 1:59 pm
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
Well, you can use a floating point value. While I believe you will lose precision (low order numbers), it will keep track of the ballpark (high order numbers). If you really, really need to know the exact number, you'd need a really big integer, which is conveniently provided by the BigInteger class.
[edit]Added code to show max values. If you're still having issues, post your code and somebody will probably take a look at it.[/edit]
Code: Select all
import java.math.BigInteger;
public class Test
{
public static void main(String args[])
{
int i = 0;
long l = 0l;
double d = 0d;
BigInteger bi = new BigInteger("0");
i += 1;
l += 1;
d += 1;
bi = bi.add(new BigInteger("1"));
System.out.println("int = " + i);
System.out.println("long = " + l);
System.out.println("double = " + d);
System.out.println("BigInt = " + bi.toString());
//max out values
i = Integer.MAX_VALUE;
l = Long.MAX_VALUE;
d = Double.MAX_VALUE;
//I can't remember how to do a cast so I'll fudge it
bi = new BigInteger(Long.toString(l));
//Just to show bi can go beyond a long
bi = bi.add(new BigInteger("1"));
System.out.println("int = " + i);
System.out.println("long = " + l);
System.out.println("double = " + d);
System.out.println("BigInt = " + bi.toString());
}
}
I choose to fight with a sack of angry cats.