A long bigger than Long.MAX_VALUE

前端 未结 4 1485
天涯浪人
天涯浪人 2020-12-10 00:37

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

I want this method to return true:

boolean isBiggerThanMaxLong(long val) {
    r         


        
相关标签:
4条回答
  • 2020-12-10 00:48

    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.

    0 讨论(0)
  • 2020-12-10 00:51

    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.

    0 讨论(0)
  • 2020-12-10 00:57

    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;
      }
    
    0 讨论(0)
  • 2020-12-10 01:06

    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
    
    0 讨论(0)
提交回复
热议问题