News for hackers. Stuff that matters.
Re:Microfeatures I'd like to see in more languages by recursive on Thursday January 05, 2023 @08:31PM (#34269761)
In c# -1 % 2 == -1. Doesn't help.
Re:Microfeatures I'd like to see in more languages by KMag on Friday January 06, 2023 @12:44AM (#34271436)
C#'s % is explicitly the remainder operator, not the modulo operator.
Re:Microfeatures I'd like to see in more languages by recursive on Friday January 06, 2023 @01:02AM (#34271535)
Not according to reflection.

    Expression<Func<int, int>> lambda = n => n % 2;
    Console.WriteLine(((dynamic)lambda).Body.NodeType);
Output is "Modulo".
Re:Microfeatures I'd like to see in more languages by KMag on Friday January 06, 2023 @08:00AM (#34274153)
Hmm... I haven't read the official spec, just the Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/csharp/language-ref... In any case, the behavior is rem, not mod.

Ada has both rem and mod operatiors. I'm not sure how many other languages have operators for both.

Re:Microfeatures I'd like to see in more languages by cpurdy on Saturday January 07, 2023 @07:27PM (#34294653)
Ecstasy uses % for modulo, and /% for divrem (division and remainder). So "a = b % c" for calculating the modulo, but "(a, r) = b /% c" to get the divisor and remainder.
Re:Microfeatures I'd like to see in more languages by feoren on Friday January 06, 2023 @02:49AM (#34272076)
It's a little verbose and can probably be reduced, but "((x % m) + m) % m" always works. Although it's probably not better than a check if x < 0 since branch prediction will get that right almost always.

I do think it's quite odd and frustrating that modulo can return negative numbers and I don't really get the reasoning there, but there's probably a good reason I don't know about.

Re:Microfeatures I'd like to see in more languages by xigoi on Friday January 06, 2023 @05:03AM (#34272960)
The only reason is that C does it that way. And C does it that way because machine code does it that way.