简体   繁体   中英

Find step to divide sum of two very big ints on equal chunks

I have two limits - start and end which are long values. I need to find step to divide distance between start and end on equal chunks.

So the formula actually is following:

(end - start)/1000

where 1000 is amount of chunks. The problem comes when start is Long.MIN_VALUE or near to it and end is Long.MAX_VALUE.

Thanks

You could use a BigInteger instead:

BigInteger bigStart = BigInteger.valueOf(start);
BigInteger bigEnd = BigInteger.valueOf(end);
BigInteger bigDiv = BigInteger.valueOf(1000);

BigInteger step = bigEnd.subtract(bigStart).divide(bigDiv);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM