LongRangeSet (LongRangeSet divisor) { if (divisor.isEmpty() || divisor.equals(ZERO)) return empty(); if (myValue == 0) return this; if (divisor instanceof Point) { return LongRangeSet.point(myValue % ((Point)divisor).myValue); } if (myValue != Long.MIN_VALUE) { long abs = Math.abs(myValue); if (!divisor.intersects(LongRangeSet.range(-abs, abs))) { // like 10 % [15..20] == 10 regardless on exact divisor value return this; } } LongRangeSet addend = empty(); if (divisor.contains(Long.MIN_VALUE)) { divisor = divisor.subtract(point(Long.MIN_VALUE)); addend = point(myValue); } long max = Math.max(0, Math.max(Math.abs(divisor.min()), Math.abs(divisor.max())) - 1); if (myValue < 0) { return LongRangeSet.range(Math.max(myValue, -max), 0).join(addend); } else { // 10 % [-4..7] is [0..6], but 10 % [-30..30] is [0..10] return LongRangeSet.range(0, Math.min(myValue, max)).join(addend); } }