Is the modulo operation signature in sync with the mathematical definition?

Only to point into the side-effect that I suffered from the current approach. The result of this operation is taking more space than necessary.
In my case, Long.mod(Int) = Long, the response is using 64bits when only 32bits are used. It means, double of necessary space used.

If the result you get is Long but you want Int, just case it to Int and rely on the compiler to optimize as needed.

Most likely, only same-type operations are supported on the backend, and all mixed type operations are convenience wrappers using casts under the hood.

1 Like

Honestly the best way to aleviate your concern would be to use something like https://github.com/typelevel/spire which provides unbounded number types (such as Real) i.e. types that are equivalent to their mathematical definitions.

Otherwise you end up bikeshedding, it makes sense to either have types that reflect the underlying hardware (i.e. Int/Long) or types that reflect their mathematical definition (i.e. Real/Natural). if you mix both of them then you get issues with subtle overflow/underflow bugs when you aren’t expecting them.

3 Likes

Thanks everybody. I love this forum and the Scala community.

1 Like