Quantcast
Channel: User CPlus - Programming Language Design and Implementation Stack Exchange
Viewing all articles
Browse latest Browse all 65

Why is truncated or non-Euclidean division/modulo the norm?

$
0
0

Related to, but not the same question: Distinguishing modulo (Euclidean division) from remainder

There are multiple ways a modulo operation can be performed. Most implementations, in C, for example, use truncated division/modulo. The sign of the result equals the sign of the dividend:Truncated Division (Wikipedia Image)

However, most of the time in practice the Euclidean modulo is more useful. The result is always 0 or positive. This leads to idioms along the lines of:

unsigned euclidean_mod(int a, int b) {    int m = a % b;    return m < 0 ? m + b : m;}

This leads to Euclidean modulo:

Euclidean Division (Wikipedia Image)

My question is, what factors lead the former to be the 'default' in programming languages such as C, even if the latter is more useful in practice?


Viewing all articles
Browse latest Browse all 65

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>