Why does the modulo operation result take the dividend’s type instead of the divisor’s type?
This fact is not affecting the daily work of Scala users, but it looks like it is not in sync with the mathematical law following the modulo operation.
I am not a mathematician and I don’t know in deep the internal implementation of this operation, so pretty sure that there is an explanation behind it.
As I asked in Stackoverflow days ago, why Scala use these signatures for the module?
For example, in
def %(x: Byte): Long def %(x: Short): Long def %(x: Char): Long def %(x: Int): Long def %(x: Long): Long def %(x: Float): Float def %(x: Double): Double
I think that the response type should be always the same type of
x, because the remainder after the division always is going to fit into the divisor type.
So why the response type is taken from the dividend?
Should be next one the right signature for
def %(x: Byte): Byte def %(x: Short): Short def %(x: Char): ??? def %(x: Int): Int def %(x: Long): Long def %(x: Float): Float def %(x: Double): Double
It is curious that when
Float, the signature is right.
In Stackoverflow, one response is pointing to Java compatibility. So this response generates another question: Is it necessary to keep this compatibility even if the definition does not match with the mathematical concept that it is implementing?