62

How can I get a long number bigger than Long.MAX_VALUE?

I want this method to return true:

boolean isBiggerThanMaxLong(long val) {
    return (val > Long.MAX_VALUE);
}
0

6 Answers 6

101

That method can't return true. That's the point of Long.MAX_VALUE. It would be really confusing if its name were... false. Then it should be just called Long.SOME_FAIRLY_LARGE_VALUE and have literally zero reasonable uses. Just use Android's isUserAGoat, or you may roll your own function that always returns false.

Note that a long in memory takes a fixed number of bytes. From Oracle:

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

As you may know from basic computer science or discrete math, there are 2^64 possible values for a long, since it is 64 bits. And as you know from discrete math or number theory or common sense, if there's only finitely many possibilities, one of them has to be the largest. That would be Long.MAX_VALUE. So you are asking something similar to "is there an integer that's >0 and < 1?" Mathematically nonsensical.

If you actually need this for something for real then use BigInteger class.

6
  • 8
    Someone should work on breaking isUserAGoat API ether by inventing teleportation or at least create an android gadget for goats.
    – Mr.Me
    May 14, 2013 at 14:44
  • @gongshw assuming triangle.lborderA is a long then you can delete that check. I wouldn't be surprised if it optimizes out.
    – djechlin
    May 14, 2013 at 14:56
  • The source code to test contains the judgment "triangle.lborderA > 0 && triangle.lborderA <= Long.MAX_VALUE". So it seems that i cannot make my unit test 100% coverage.
    – gongshw
    May 14, 2013 at 14:57
  • 2
    @gongshw try lborderA < 0 to fail that check. Similarly you cannot fail a check that says if(Boolean.TRUE).
    – djechlin
    May 14, 2013 at 15:00
  • 1
    @Mr.Me Well, according to this answer, if you download the package com.coffeestainstudios.goatsimulator, then the teleportation of goats can commence!
    – MC Emperor
    Oct 19, 2017 at 11:05
14

You can't. If you have a method called isBiggerThanMaxLong(long) it should always return false.

If you were to increment the bits of Long.MAX_VALUE, the next value should be Long.MIN_VALUE. Read up on twos-complement and that should tell you why.

6

Firstly, the below method doesn't compile as it is missing the return type and it should be Long.MAX_VALUE in place of Long.Max_value.

public static boolean isBiggerThanMaxLong(long value) {
      return value > Long.Max_value;
}

The above method can never return true as you are comparing a long value with Long.MAX_VALUE , see the method signature you can pass only long there.Any long can be as big as the Long.MAX_VALUE, it can't be bigger than that.

You can try something like this with BigInteger class :

public static boolean isBiggerThanMaxLong(BigInteger l){
    return l.compareTo(BigInteger.valueOf(Long.MAX_VALUE))==1?true:false;
}

The below code will return true :

BigInteger big3 = BigInteger.valueOf(Long.MAX_VALUE).
                  add(BigInteger.valueOf(Long.MAX_VALUE));
System.out.println(isBiggerThanMaxLong(big3)); // prints true
4

If triangle.lborderA is indeed a long then the test in the original code is trivially true, and there is no way to test it. It is also useless.

However, if triangle.lborderA is a double, the comparison is useful and can be tested. isBiggerThanMaxLong(1e300) does return true.

  public static boolean isBiggerThanMaxLong(double in){
    return in > Long.MAX_VALUE;
  }
2
  • 1
    isBiggerThanMaxLong(Long.MAX_VALUE + 1.0) will return false. isBiggerThanMaxLong(Long.MAX_VALUE + 1024.0) will also return false. You get true with doubles >= Long.MAX_VALUE + 1025.0
    – Samil
    Nov 21, 2014 at 12:06
  • @Samil True. It would return true for any double that is greater than Long.MAX_VALUE, of which there are many. Because of floating point rounding, Long.MAX_VALUE+1.0 is not one of them. Nov 21, 2014 at 14:32
1

Just remember BigInteger is a class and long is a primitive. If you want to compare large values than long_max_value, wrap your value in the BigInteger class and treat them as objects.

0

A "long" greater than the maximum value of long just doesn't exist , rather i suggest you use the biginteger class , and caste the maximum value of long to a biginteger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.