0

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) Division Anomaly (Mathematical Demonstration)

C#'s approach to division between two negative numbers: Division Anomaly (C# Approach)

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

hecate
  • 620
  • 1
  • 8
  • 33
  • 4
    Could you copy the relevant code into the question? Images are hard to read - in particular for future readers from a search-engine. – MakePeaceGreatAgain Aug 31 '16 at 11:39
  • Why, no clue, probaly because it's easier to shift the bits around in cpu's. This might help you. http://www.codeproject.com/Articles/9078/Fraction-class-in-C – Tschallacka Aug 31 '16 at 11:40
  • @HimBromBeere I'll do just that. – hecate Aug 31 '16 at 11:43
  • "fraction concept" == floating point math. Which requires at least one of the operands to be `float` or `double`. If they are integral types then you get integer division, no fraction. So 1 / 2 = 0 and 1.0 / 2 = 0.5. You trivially force a floating point division by casting one of the operands, (double)1 / 2 = 0.5 – Hans Passant Aug 31 '16 at 11:49
  • @HansPassant This has nothing to do with 'float' data-type; I am dealing with modular-division whose value is CANNOT be a float type. – hecate Aug 31 '16 at 11:54
  • @Tschallacka Thanks for your help and effort for providing me with this resource but sadly it doesn't solve the problem at hand. Do you have any other ideas ?? :) – hecate Aug 31 '16 at 11:56
  • 1
    I can't understand handwritten image, sorry. –  Aug 31 '16 at 12:01
  • @ElmoDev001 Due to the handwriting or the resolution ? If its due to the resolution, then I can upload a higher resolution image. As for my handwriting well, I believe its readable although not so good..... :) – hecate Aug 31 '16 at 12:05
  • @ElmoDev001 Try downloading the image, it can help. Thanks for your interest. – hecate Aug 31 '16 at 12:06
  • Explain to me why -10 % -3 should be 1... -10 - -9 = -1... `-10 - (-3 * Math.floor(-10 / -3))` The modulos gives the remainder in programming. and the remainder you get when working with negative number is a negative number. – Tschallacka Aug 31 '16 at 12:06
  • @Tschallacka Please refer to the fractional concept section in the mathematical demonstration image above. – hecate Aug 31 '16 at 12:08
  • @Tschallacka Mathematically, if you consider the fractional approach you get a positive quotient and a positive remainder. Well, that's what I wanted C# to follow. – hecate Aug 31 '16 at 12:09
  • Why haven't you written a Rational or Fraction class to encapsulate what you want? – duffymo Aug 31 '16 at 12:10
  • You will have to compensate for the human logic in coding by using things like the algorithm you don't want to do. – Tschallacka Aug 31 '16 at 12:11
  • @Tschallacka So does that mean I have to use the above algorithm ? Isn't there any other way to bypass it ? – hecate Sep 01 '16 at 06:17
  • @duffymo Can you please elaborate further on your concept ? Thanks for help. – hecate Sep 01 '16 at 06:18
  • Sorry for late reply guys; was engrossed on another project :) – hecate Sep 01 '16 at 06:19
  • @hectate no there isn't. The modulo operator in programming is not equal to modulo fractions. It's a very quick check though if both are below 0. – Tschallacka Sep 01 '16 at 06:40
  • @Tschallacka Thanks, I get it now. Well, I'll just implement the above algorithm to my program. Thanks again. – hecate Sep 01 '16 at 06:43
  • Possible duplicate of [How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers](http://stackoverflow.com/questions/4003232/how-to-code-a-modulo-operator-in-c-c-obj-c-that-handles-negative-numbers) – phuclv Sep 01 '16 at 06:59
  • http://stackoverflow.com/q/1082917/995714, http://stackoverflow.com/q/11720656/995714, http://stackoverflow.com/q/17461486/995714 – phuclv Sep 01 '16 at 07:00

1 Answers1

3
(-10) % (-3) = ( (-9)          + (-1) ) % (-3) 
             = ( 3*(-3)        + (-1) ) % (-3) 
             = (3*(-3)) % (-3) + (-1) % (-3) 
             = 0 + (-1) % (-3)

so -10 mod -3 = -1 mod -3

please compare with the spec of C#'s % operator...

Note that as mathematician, i would not do a mod b with b<0...

Community
  • 1
  • 1
Daniel Heldt
  • 417
  • 5
  • 15
  • Does this mean that mathematical logic needs to be compensated for operations like there ? I mean isn't there any way to bypass it ? Thanks for your interest. – hecate Sep 01 '16 at 06:16
  • 2
    The problem here is, that the expected result of mod as above is different from the expected result using the "fraction concept". Depending on perspective, both might be right, so it is unclear, what the right result is. So -in my eyes- you simply should omit the notion of mod (a) for a < 0. – Daniel Heldt Sep 01 '16 at 07:46
  • Thanks for your opinion. I'll do just that. :) – hecate Sep 01 '16 at 07:59