+ 45

Modulo and integer division in C++ and C languages.

From the point of view of C++, C: -17 % 5 = -2 -17 / 5 = -3 But from the point of view of mathematics (and python😁): -17 % 5 = 3 -17 / 5 = -4 Explain inner logic of compiler (e. g. gcc) to me. How it calculates? Why designers chose such logic in compiler, instead of mathematical approach?

8th Jan 2018, 10:51 AM
\__(° = °)__/
20 Answers
+ 26
Maybe this approach suits for old languages(they didn't notice difference and didn't want to aim to design the language more consistently and of course can't solve and notice all features related with mathematics)? But the in modern (don't argue🙃) languages they decided to make it more correctly from the point of view of mathematics🤔 Honestly, I dislike these implicit things in languages zoo, but nevertheless we have what we have. For example, in JS we have strange thing as **null**. And they can't fix it because of backward compatibility in legacy codes😁 A surprising, but very interesting world of programming languages💃
8th Jan 2018, 11:18 AM
\__(° = °)__/
+ 16
-- found this which maybe of interest. Originally, MOD(n, m) was defined only for positive values of both m and n, and leaves the result to be implementation-dependent when either of m or n is negative.
8th Jan 2018, 12:41 PM
jay
jay - avatar
+ 14
In Mathematics we have : Dividend = Divisor * Quotient + Remainder so according to your words :) : In C++ we have: (-17) = (+5) * (-3) + (-2) => (-17) = (-15) + (-2) =>(-17) = (-17) => So its correct :) In Python we have : (-17) = (+5) * (-4) + (+3) => (-17) = (-20) + (+3) =>(-17) = (-17) => So its correct :) So don't say Mathematics and Python says other answer :) because both of them (C++ & Python) are correct just having different processes :)
12th Jan 2018, 1:46 PM
Navid Fathi Amirkhiz
Navid Fathi Amirkhiz - avatar
+ 10
Non euclidean division is used in c and it's variants. You can use euclidean division, but you have to do so manually. https://code.sololearn.com/ct8gPZL2Qj8R/?ref=app
10th Jan 2018, 8:43 AM
jay
jay - avatar
+ 9
At first glance, I'd attempt to implement what is referred to as 'typecasting'. Perhaps try something *similar* to the following (toy around with it some) ... *C++ : int var1 = (int)(-17 % 5); int var2 = (int)(-17 / 5); *Python : var1, var2 = int(-17 % 5), int(-17 / 5)
9th Jan 2018, 2:36 PM
Fox
Fox - avatar
+ 9
it's interesting
14th Jan 2018, 8:47 AM
Zangar Egetay
Zangar Egetay - avatar
+ 4
mod operation for any negative operand is platform/language (and person 😀) dependent https://en.m.wikipedia.org/wiki/Modulo_operation
9th Jan 2018, 8:23 PM
KrOW
KrOW - avatar
+ 4
it's interesting
10th Jan 2018, 1:53 AM
Somasundaram R
Somasundaram R - avatar
+ 3
-17/5 gives -3.4 and int(-17/5) gives -3 in python idle 3.5.4
10th Jan 2018, 6:21 AM
Yunus Emre Erinç
Yunus Emre Erinç - avatar
+ 3
it isn't necessary to use an integral.
12th Jan 2018, 3:26 PM
Algirdas Papievis
+ 3
https://www.cprogramming.com/tutorial/modulus.html it is converted into integer numbers then calculate using boxing unboxing concepts......
13th Jan 2018, 10:17 AM
sunil bhise
sunil bhise - avatar
+ 3
What is the difference between languages C# and C++?
17th Jan 2018, 5:39 PM
Uelton Lima
Uelton Lima - avatar
+ 2
you are the question you are the answer. @4rontender
10th Jan 2018, 1:51 AM
Somasundaram R
Somasundaram R - avatar
+ 2
I think what 4rontender is pointing out clearly is that -n%m in .cpp != -n%m in .py & -n/m in .cpp != -n/m in .py
16th Jan 2018, 1:40 PM
E_E Mopho
E_E Mopho - avatar
+ 1
Это Точно что интересно
15th Jan 2018, 2:20 AM
Богдан Фокин
Богдан Фокин - avatar
+ 1
c & c++ are hardware related languages. it is searched for the fastest and most direct method to compute modulo. IEEE allows you to: IEEE 754 defines a remainder function where the quotient is a/n rounded according to the round to nearest convention. Thus, the sign of the remainder is chosen to be nearest to zero.
17th Jan 2018, 9:18 AM
#Mozart
#Mozart - avatar
- 1
because of bigger precedence
10th Jan 2018, 6:16 PM
Prerit Ameta (CJ)
Prerit Ameta (CJ) - avatar
- 2
Привет
13th Jan 2018, 7:54 PM
Иванушко Иван
Иванушко Иван - avatar
- 3
36
11th Jan 2018, 11:01 AM
Master
Master - avatar