Length is longer than Long.MAX_VALUE

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

I want this method to return true :

 boolean isBiggerThanMaxLong(long val) { return (val > Long.MAX_VALUE); } 
+23
source share
5 answers

This method cannot return true . This is the point of Long.MAX_VALUE . It would be very strange if his name were ... false. Then it should just be called Long.SOME_FAIRLY_LARGE_VALUE and have literally zero sensible uses. Just use Android isUserAGoat , or you can roll your own function, which always returns false .

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

long: the long data type is a 64-bit signed integer from two additions. 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 ​​that is wider than the one that int provides.

As you may know from basic computer science or discrete mathematics, there are 2 ^ 64 possible values ​​for long, since it is 64 bits. And, as you know from discrete mathematics or number theory or common sense, if there is only a finite number of possibilities, one of them should be the largest. That would be Long.MAX_VALUE . So you ask something like "is there an integer that is> 0 and <1?" Mathematically pointless.

If you really need it for something real, use the BigInteger class.

+53
source

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

If you must increment the Long.MAX_VALUE bit, the next value should be Long.MIN_VALUE . Read on for a two-supplement, and this should tell you why.

+11
source

Firstly, the method below does not compile, because it lacks a return type and should be Long.MAX_VALUE instead of Long.Max_value .

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

The above method will never be able to return true since you are comparing a long value with Long.MAX_VALUE , see the signature of the method that you can only pass long Any long can be as large as Long.MAX_VALUE , it cannot be larger than this,

You can try something like this with the BigInteger class:

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

The code below will return true :

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

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

However, if triangle.lborderA is double, the comparison is useful and can be verified. isBiggerThanMaxLong(1e300) returns true.

  public static boolean isBiggerThanMaxLong(double in){ return in > Long.MAX_VALUE; } 
+2
source

You can try BigInteger even though its use is complicated ...

0
source

Source: https://habr.com/ru/post/1480737/


All Articles