+ 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?
20 Réponses
+ 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💃
+ 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.
+ 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 :)
+ 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
+ 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)
+ 9
it's interesting
+ 4
mod operation for any negative operand is platform/language (and person 😀) dependent
https://en.m.wikipedia.org/wiki/Modulo_operation
+ 4
it's interesting
+ 3
-17/5 gives -3.4 and int(-17/5) gives -3 in python idle 3.5.4
+ 3
it isn't necessary to use an integral.
+ 3
https://www.cprogramming.com/tutorial/modulus.html
it is converted into integer numbers then calculate
using boxing unboxing concepts......
+ 3
What is the difference between languages C# and C++?
+ 2
you are the question
you are the answer. @4rontender
+ 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
+ 1
Это Точно что интересно
+ 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.
- 1
because of bigger precedence
- 2
Привет
- 3
36