Java tutorial
//package com.java2s; import java.math.BigInteger; public class Main { public static BigInteger modNear(BigInteger a, BigInteger b) { BigInteger res = a.mod(b); if (res.compareTo(b.shiftRight(1)) == 1) res = res.subtract(b); return res; } public static BigInteger mod(BigInteger a, BigInteger b) { BigInteger res = a.mod(b); return res; } }