Page 1 of 1
Bigger than int
Posted: Thu Nov 20, 2003 1:31 pm
by mon.king
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?
Posted: Thu Nov 20, 2003 1:46 pm
by s_reynisson
long
Edit: Had to look this up
long goes from/to around 9E-/+18, ie. -/+9 with 18 zero's after it
Posted: Thu Nov 20, 2003 2:28 pm
by mon.king
have tried long but i still get the same outcome as i did while using int. are you able give me an example of how to use long?
Posted: Thu Nov 20, 2003 4:22 pm
by talleyrand
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.
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());
}
}
[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]