Bigger than int

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
mon.king
Posts: 4
Joined: Thu Nov 20, 2003 1:22 pm

Bigger than int

Post 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?
User avatar
s_reynisson
Posts: 939
Joined: Tue May 06, 2003 1:59 pm

Post by s_reynisson »

long

Edit: Had to look this up :oops:
long goes from/to around 9E-/+18, ie. -/+9 with 18 zero's after it
Then I open up and see
the person fumbling here is me
a different way to be
mon.king
Posts: 4
Joined: Thu Nov 20, 2003 1:22 pm

Post 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?
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post 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]
I choose to fight with a sack of angry cats.
Post Reply