Someone new to the language might reasonably expect 7%4
to return 3
, i.e. the result of mod(7,4)
, as it does in many other languages. Instead it returns 7
- "Why is this so?" s/he may ask...
Why should you reasonably expect 7%4
to return 3
? %
isn't a mathematical symbol for the modulo operation. If you read the Wikipedia article, you'll see that as many languages use mod
as use %
so Matlab isn't an odd one out here. It's just a different language from where ever you were coming from.
I would guess that %
came from C and Matlab was initially aimed at 'competing' with Fortran rather than C. Fortran uses MOD
and not %
. I think the only reason you expect %
to mean modulo is because of the influence of C and that the question of why modulo gets a special operator character in many languages is more relevant. Why modulo over floor for example? Or over absolute?
Matlab uses %
for comments but that cannot be said to be the reason that it doesn't use it for modulo. I think that it's more fair to say the reason that it doesn't use %
for modulo is because there really isn't any reason for it to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With