Programming thread

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.
Did you know that Java has a fucked up modulus implementation?

Negative numbers don't return a quotient.

Java:
public class Main {
    public static void main(String[] args) {
        System.out.println(-2 % 10);
    }
}
... and now in Python3
Python:
print(-2 % 10)
James Gosling is a retard. How he got a job doing anything in software is proof that since 1970 the IQ of software engineers has decreased precipitously as time continued. This horrible failure to implement the simplest of operators ruined 3rd pagination implementation I've had to write this year.
Java:
void last() {
    Integer l = this.index - 1;
    this.index = l % this.limit;
}
had to be turned into
Java:
void last() {
    Integer l = this.index - 1;
    if (l > -1) {
        this.index = l % this.limit;
    } else {
        this.index = this.limit + l;
    }
}

Java was created by niggers.
 
That's a common modulo implementation. C and most languages behave the same way.
Mathematically correct modulus should return 8 for -2 % 10. Just because it common doesn't mean it's correct.
---
I maintain that floored is the correct definition for modulus.
 
Last edited:
But it is mathematically correct under the rules of modular arithmetic.
Yes the floored definition of modulus is correct. Using the truncated definition that would return -2 instead of 8 would be incorrect mathematically. The only reason this matters is because if you want pages to wrap during pagination, and you are storing the current page this.index, then it would be easy to use modulus to force any indexes that exceed the maximum index possible to wrap around back to the first page's index.

It's no surprise that programming languages that don't used the mathematically correct modulus are such lolcows of a language.
 
Last edited:
Using the truncated definition that would return -2 instead of 8 would be incorrect mathematically.
Given an integer n > 1, called a modulus, two integers a and b are said to be congruent modulo n, if n is a divisor of their difference (i.e., if there is an integer k such that ab = kn).
You have -2 % 10 = -2 which means -2 === -2 (mod 10) which is true of course (k=0). QED.
 
Mathematically correct modulus should return 8 for -2 % 10. Just because it common doesn't mean it's correct.
---
I maintain that flooded is the correct definition for modulus.
-2 divided by 10 isn't -1 with a remainder of 8. It's 0 with a remainder of -2.

If you want a guaranteed positive remainder, you can always do (i % j + |j|) % j.
 
You have -2 % 10 = -2 which means -2 === -2 (mod 10) which is true of course (k=0). QED.
You could just as correctly have -2 % 10 return 148, but there is also that little matter of practicality. Most people using the % operator are not actually using it to work with a set of equivalence classes that partition the integers.
 
Isn't that a pretty common use? Like separating odd and even numbers.
But they typically do that by mapping the odd numbers to 1 and the evens to 0, which from a mathematical point of view is just choosing one arbitrary representative each from the "odds" and "evens" equivalence classes.

What most people want from "%", strictly speaking, is to choose the integers 0..n-1 as the representatives of the equivalence classes of Z/n, and for any integer, return the representative of whichever equivalence class it belongs to. There's no particular word for that but "mod" will do as a shorthand.
 
  • Like
Reactions: Knight of the Rope
I maintain that flooded is the correct definition for modulus.
Yes the flooded definition of modulus is correct.
You mean floored, right? I thought you made a typo at first but it's odd that you'd make the same typo across two different posts.

Otherwise I agree though. Not about the nonsense where you think truncated modulus is "mathematically incorrect" (which is just pure cope since @byuu showed you how -2 % 10 == -2 is mathematically-sound according to the definition), but about the floored modulus being more useful in practice since it partitions the entirety of the integers into exactly n sets and loops the way you usually want it to when it flips to negative.

I.e. most people want this:
Code:
     a = -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
a % 10 =   6   7   8   9   0  1  2  3  4  5  6  7  8  9 0 1 2 3 4 5 6 7 8 9  0  1  2  3  4

Not this:
Code:
     a = -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
a % 10    -4  -3  -2  -1   0 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9  0  1  2  3  4
 
Anyone use Nix? I've been interested for a while but have no idea where to start. I don't really like Docker so I figure given that I like Haskell a lot, I might like Nix more? Anyone have any opinions?
 
Anyone use Nix? I've been interested for a while but have no idea where to start. I don't really like Docker so I figure given that I like Haskell a lot, I might like Nix more? Anyone have any opinions?
Literally never heard of it until just now.

I guess my first question would be: what's your planned use-case for it? Are you doing containerized software deployments or trying to reproduce dev environments? If so I can't see Docker ever sucking hard enough at either of those things that you'd abandon it to go with the lesser-known quantity. (On the other hand if you're just curious and it's for play, then go nuts?)
 
Anyone use Nix? I've been interested for a while but have no idea where to start. I don't really like Docker so I figure given that I like Haskell a lot, I might like Nix more? Anyone have any opinions?
I'm curious too. Have you seen this?


I really hate docker myself but I have a lot of investment in it so this looks like it could be a neat way to explore Nix some without completely migrating everything.
 
  • Informative
Reactions: Anstiv
Arguing about this is like arguing that 22/7 should equal 🥧 (3.14....).

It is traditional in many programming languages to do truncate. That does not make it correct. I have no dought that Java's choice in this was the same as to have a static void main, even though it really doesn't have anything to do with Java, and is only a slight nod towards C and C++. The entrypoint for a Java program could have just as easily been something like the Runnable interface or an abstract public Main, which your program's entrypoint must be a child of.

Forget about all the language stuff, algebra and programming languages, for a minute. If you have a finite number line where each unit is one step and it's defined starting at 0 and ending at 10, and you walk two steps backwards, then you would be at 8. If you walking forward on this number line, since it is the only number line in existence, when you step off of 10 you are teleported back to 0, or accept that position 0 on the line have been glued onto position 10, so that the perimeter of the number line forms a circle. Starting from the natural position of 0, you walk 2 steps backwards, or -2 steps. You would then be at position 8. If you were to walk 15 forward steps, then you would end up at position 5. The only reason we use division to calculate this is because division is recursive cutting. If a doctors want's to subtract a appendix from a patient, they cut it out in surgery. If I cut away two from ten, then it's eight, if I have to do this multiple times arriving back at zero in between cuts, it works the same. You can calculate modulus using factorisation, not division, and you get just as accurate results. This is because on a finite number line you'd have to find yourself at some position on the line because it is the only space you have to measure in. So, putting aside all the Mathematics and Programming syntax and symbols, it's illogical to return a negative number for a finite number line that does not have any negative positions. Likewise it would be equally as silly to say return 8 from a number line ranging between 0 and -10. With 0 and -10, 8 does not exist on that number line and since we can only have position that exist on that number line, 8 should not be returned given the set of constraints provided.

It is a mathematical legalist argument to state that -2 % 10 != 8 and then provide profs that state otherwise. C got it wrong. While it is traditional to copy C's mistakes, it is still wrong. Providing formulas that calculate illogical results for arithmetic modulus does not magically make C right, even it does make you feel betting by providing a seemingly reasonable rationalization for your position. It's very interesting to note that Java has 2 different types of modulus in it's standard library: the % operator and Math.floorMod. Why you would need to implement the same operation twice? Could it be because one is to satisfy a population of programmers accustomed to wrong answers while also know these answers are wrong and therefore necessitate the implementation of the correct operation to accompany the errant one? Java as many other ambitious programming languages were launched with the promises of lessening errors in programs. How can this be any more then whole-cloth when Java goes out of it's way to provide the wrong result to common operations. What is % in Java if it's not arithmetic modulus? It must be some other operation perpetrating a mascaraed.

Interestingly enough, Ada has a feature which would be perfect for this situation: Ranges. You can define a type that must contain a number bounded by a desecrate continuum. Though, Ada also has an arithmetically correct modulus operator, so using Ranges would not be the only option.

This would not be a problem if the disabled were not hired to write software.
 
<jesus christ snip>
Fuck me, why do the cranks that insist that mathematics itself is incorrect always write long-winded diatribes that don't even say anything?

Just take the L, man. Most of us agreed with you that the floored modulus is more useful, it doesn't make the math wrong.

It is a mathematical legalist argument to state that -2 % 10 != 8 and then provide profs that state otherwise.
No, it's a mathematical argument. Legalist arguments actually have wiggle room, whereas mathematical arguments are infinitely more autistically pedantic. It is a mathematical fact that -2 % 10 == -2. It is also a mathematical fact that -2 % 10 == 8. So then you need further stipulations to narrow it down to one answer (like, say, the stipulation that you want the remainder to be positive, which seems to be what you're arguing about in that wall of text). That doesn't make -2 any less of a valid answer given the definition of modulo arithmetic.

Sure, C's choice was perhaps an odd one, and the fact that it influenced C++ and Java's choices through some idiotic notion of 'compatibility' is unfortunate. We agree with you on that point. We disagree with you when you start extending it out to crankery.
 
Back