There is an anomaly between the usual approach and the programming approach in the division of a negative number by a negative number as mentioned in the pictures bellow.
Mathematical demonstration of anomaly: (Sorry for bad handwriting :) Hope it is readable)
C#'s approach to division between two negative numbers:
How do I enforce C# to follow the fraction concept when I divide two negative numbers with the usual syntax? And please do not suggest me with the following algorithm:
// 'num1' and 'num2' are the two numbers of 'int' data-type to be divided where Math.Abs(num1) > Math.Abs(num2)
if (num1<0 && num2<0) // both numbers are negative
Console.WriteLine(Math.Abs(num1)%Math.Abs(num2));
else
Console.WriteLine(num1%num2);
PS: Please note that no elaboration is required in the mathematic anomaly regarding the two answers yielded by the division between two negative numbers. Just suggest me a way to enforce C# to follow the fraction concept mentioned above (if possible).
PPS: An explanation to why programming languages follow the normal division approach instead of fraction concept would be helpful too :)
EDIT: Code of the C# Approach picture above (for people who find images tough to read :) )
// Tests to see C#'s return values for signed and unsigned "modular division"
Console.WriteLine("-10 MOD 3 = "+ ((-10)%(3))); // Displays -1 (as expected)
Console.WriteLine("10 MOD -3 = "+ ((10)%(-3))); // Displays 1 (as expected)
Console.WriteLine("10 MOD 3 = "+ ((10)%(3))); // Displays 1 (as expected)
Console.WriteLine("-10 MOD -3 = "+ ((-10)%(-3))); // Displays -1 (should have displayed a positive 1)
Console.WriteLine("-10 MOD -3 (with no brackets placed for numbers) = "+ (-10%-3)); // Same output as that of above statement i.e. -1
UPDATE: Considering the opinions of the Stack Overflow community, I believe that no programming language can be enforced to follow the "fraction concept". But anyone in the community is welcome to contribute their opinions on "Why programming languages use the 'division approach' ?" question. Also please post the solution if anyone finds a way to enforce the "fraction concept" in C# in near future. Thanks. :)