Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't Octave or Matlab use % (percent symbol) as a modulo operator [closed]

Tags:

matlab

octave

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...

like image 946
JobJob Avatar asked Oct 23 '14 09:10

JobJob


1 Answers

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.

like image 62
Dan Avatar answered Dec 04 '22 04:12

Dan