ceylon / ceylon.language

DEPRECATED
Apache License 2.0
153 stars 57 forks source link

Add Integral.modulo #755

Closed jvasileff closed 9 years ago

jvasileff commented 9 years ago

In Ceylon, the result of remainder() takes the sign of the dividend:

-10 % 7 = -3

An alternate interpretation is sometimes useful, where the result is always positive:

-10 mod 7 = 4

For reference, Java's BigInteger offers both remainder and mod.

For completeness and consistence with Whole, it may make sense to add Integral.modulo. Description/possible implementation:

        if (!modulus.positive) {
            throw AssertionError("modulus must be positive");
        }
        return let (result = remainder(modulus))
               if (result.negative)
               then result + modulus
               else result;
FroMage commented 9 years ago

+1

gavinking commented 9 years ago

+1

FroMage commented 9 years ago

Done.