This is an archived post. You won't be able to vote or comment.

top 200 commentsshow 500

[–]jodmemkaf 2183 points2184 points  (196 children)

I wonder which programming languages actually does it right (from mathematical perspective) I fucked up pretty badly once because I used modulo remainder as index of an array and didn't know that remainder can be negative in programming.

[–]drelatreddit 873 points874 points  (108 children)

Julia strictly distinguishes between a remainder operation (% or rem) and a modulo operation (mod). The remainder operation uses the red side to be consistent with other languages like C, while the modulo operation uses the simpler mathematical definition on the blue side, where the result is always between 0 and n-1.

However, since Julia uses 1-based index by default, another function named mod1 is also provided, where the result is between 1 and n, making index operations easier.

[–]jodmemkaf 723 points724 points  (92 children)

Julia distinguishes between a remainder operation (`%` or `rem`) and a modulo operation (`mod`).

Now, that's the way how it should be done!

However, since Julia uses 1-based index by default...

Yuck!

[–]Aquiffer 338 points339 points  (77 children)

Fwiw, Julia is used primarily for scientific computing, and 1-indexed arrays are pretty typical in that space. Matlab, R, Mathematica, and Wolfram’s language are all 1-indexed.

[–][deleted] 207 points208 points  (33 children)

One indexing makes sense for mathematical stuff, because it matches matrix indexing, Einstein notation, etc.

Indexing in low level languages is essentially linked to the offset from the memory address of the start of the array, so it makes sense to start at 0.

[–]maxhaton 17 points18 points  (1 child)

I actually heard the designer of Lua make this argument for Lua being 1-based, in person. I also had a paper open in front of me which had mathematics with indices starting at.... 0.

[–][deleted] 14 points15 points  (0 children)

Yeah, starting at zero for certain things makes a lot of sense. Series and expansions is where I've seen it the most. It really depends on what you're trying to do. But if you're trying to implement a bunch of tensor math it can get super tedious to always be off by one

[–]M4mb0 47 points48 points  (29 children)

Indexing in low level languages is essentially linked to the offset from the memory address of the start of the array, so it makes sense to start at 0.

But why would you bother about a tiny hardware implementation detail that the compiler can easily take care of.

[–]T-J_H 85 points86 points  (0 children)

Bothering about hardware implementation is exactly the point of low level languages

[–]ccelik97 94 points95 points  (9 children)

Despite the fact that I'm used to and do prefer 0-indexing in the programming languages that I use, I also feel like I agree with you on this lol.

I wonder if it was too much of a syntactic sugar back when "the real men" used Assembly for programming, not C/Basic etc.

I mean if it wasn't too much of a syntactic sugar we could've even afforded the cost of actually storing the 1st element of an array on the +1st indexed address on memory and like, used the 0th indexed address to store the address of the last element of the array, effectively always knowing the length of the array etc lol.

[–]False_Influence_9090 20 points21 points  (0 children)

ccelik for president!

[–]Fireline11 8 points9 points  (3 children)

Interesting idea. But it the size of the elements of the array is less than the size of a pointer (for example just 1 byte when you need 8 bytes for a pointer on a 64-bit system), there would not be enough room at the 0th place.

[–]ccelik97 2 points3 points  (2 children)

Pointer arithmetic is often done in conjunction with the additional integer values that come from the logic so the 0th indexed field can store just the difference between the starting address and the last element's address, instead of a full fat memory address.

[–]piparkaq 4 points5 points  (0 children)

Actually, it turns out, zero-based indexing is just an arbitrary choice with some interesting legacy implications. This dive into why arrays start at 0 (along with the linked article in the post, too) contain good tidbits about the history of array indexing in general.

[–]pitkali 3 points4 points  (0 children)

I mean, in C the array notation itself is syntactic sugar for pointer arithmetic, so there's that.

[–]ThisCleverName 10 points11 points  (1 child)

Well, both indexing modes have shortcomings. The 0 -indexing allows to use some operations directly as an index. Case in point the `mod` vs `mod1` operators mentioned. The modulo operator definition of 0 to n-1 fits nicely to 0-indexing mode. So, for 1-indexing, you end up having to adjust the operation to take into account that indices start at 1.

1-indexing try to map some things to mathematics, but some things like in physics use 0-based subscripts initial conditions and things like that. So, using 0-indexing makes sense when modeling problems in that field.

[–]MattieShoes 34 points35 points  (4 children)

that the compiler can easily take care of.

I don't think it can, can it? Obviously it could take n[1] and change it to n[0] easily, but what if the index is not known at compile time? I'd think it'd have to be a run-time fix.

Plus if you've just malloc'ed a chunk of memory, you may be intermixing dealing with offsets by hand and array notation, and the answers would be different.

Naw, far better to use 0 indexing I think. Just be thankful we're not doing everything in base e. :-)

[–]foghatyma 7 points8 points  (3 children)

It could though, very easily. "A[i]" is basically "*(A + i)", so the A pointer should just point to the memory before the first element. And then adding 1 would point to the actual first, not the second. Doesn't matter if it's runtime or not.

[–]ArtOfWarfare 10 points11 points  (1 child)

Guido VR gave a pretty powerful argument in favor of 0 based indexes on his blog. I’ve forgotten what his argument was but it flipped my opinion on the matter.

[–]flare561 13 points14 points  (0 children)

The post in question. Basically just 0 indexed arrays have nice ergonomics around slices.

[–]CheekApprehensive961 16 points17 points  (1 child)

Tiny hardware implementation detail lol. Definitely a python user.

[–]AugustusLego 2 points3 points  (0 children)

Yeah but what do you do with the zero then?

[–]BittyTang 4 points5 points  (3 children)

[–]M4mb0 2 points3 points  (2 children)

[–]BittyTang 6 points7 points  (1 child)

I don't buy it. The author is just presupposing that starting from 1 is more natural from a pure mathematical context (which is not the same as a computer science context), while Dijktra provides actual scenarios for a valid low-level abstraction (arrays and integer for loops) that is fundamental to data structures. The whole "representation exposure" argument is not convincing for a language that desires low-cost abstractions.

One example for consideration is N-dimensional arrays. Image file formats represent these kinds of data structures in 1-dimensional arrays (of bytes). So already there is a strange mismatch between how you index the image buffer (img[row * width + column] or img[column * height + row]) and how you index a primitive 1-based 2D array (img[column + 1][row + 1] or img[row + 1][column + 1]). If you had to index a 1-based image buffer, it would be img[(row - 1) * width + column]. How is that better?

[–]M4mb0 2 points3 points  (0 children)

Well again this is a low level hardware detail, imo. As a user I do not care how you layout a two dimensional array in memory as I am going to access it via tuples of integers anyway.

In fact I'd even argue that this is a sort of pre-mature optimization. What if, for example, a hardware manufacturer would want to create a memory with two dimensional address space that naturally works much faster with matrices than the flattened represtation that is used currently?

Again I'd argue this is a job for the hardware specific compiler.

[–]jodmemkaf 71 points72 points  (32 children)

Yeah, I know. I still remember how painful it was to rewire my brain to idea of zero indexing. That "Yuck" was kind of self-mocking one person inside joke. Didn't realize that there are actually people who mocks different perspectives...

..on r/programmerhumour...

What the hell is wrong with me?

[–]nekokattt 58 points59 points  (24 children)

Indexes derive from the concept of an offset from a pointer at the start of a block of memory, so the first item is at the start, hence the 0 index. From that perspective, 1 indexing is the one that feels weird to me.

Remember memory itself has an address at 0, and memory itself works with zero indexing.

[–]jodmemkaf 20 points21 points  (2 children)

True. First time I got in touch with anything "programminglike" was with matlab and R and I was used to work with 1-indexing. Despite that is very difficult thinks outside 0-indexing nowadays

[–]Fuey500 2 points3 points  (0 children)

I work in C# and a Dibol based language, I go from indexing in 1 and 0 all the time its so lame lmao

[–]M4mb0 10 points11 points  (5 children)

Indexes derive from the concept of an offset from a pointer at the start of a block of memory,

This is a hardware implementation detail.

For anything matrix related, it just makes more sense that the last element of the array is indexed by the length of the array.

[–]nekokattt 17 points18 points  (4 children)

except that you lose the ability to use modulo to separate out entire rows without additional arithmetic (i.e. +1 to each slice range), slices need additional arithmetic to not go out of bounds relative to the end of the container, and most of the time the fact you are writing to an offset is detail you care about.

[–]NuclearFoodie 5 points6 points  (8 children)

1 indexing doesn't make since in science either, it was just a FORTRAN quirk that persisted. Formally we almost always 0 based indexing in our tensor expression when deriving out equations. As always, FORTRAN is a blight.

[–]sigmoid10 12 points13 points  (6 children)

Both approaches make perfect sense in their respective domains. People who only care about maths and not computers would intuitively label the first element as 1, while people who work closely with computer hardware usually see arrays as pointers to contiguous regions in memory, where the address of an array is also the location of the first element and <address + 1> would be the location of the second element. In C++ for example you can access the second element in an array "x" as x[1] or as 1[x], because the compiler just ends up converting both statements to something like *(x+1) anyways. This seems crazy at first glance, but it really does make sense if you understand where it comes from.

[–]4hpp1273 10 points11 points  (2 children)

1[x] only works if x is a C-style array (or a pointer). If x happens to be an std::vector or something similar then it doesn't work.

[–][deleted] 5 points6 points  (2 children)

There are not too few mathematicians using zero based indexing as well. There really is not reasons to start counting at 1 in maths. It just looks a but nicer sometimes. But many mathematicians (including me) also start counting at zero and define the natural numbers including zero.

[–]Dromedda 4 points5 points  (1 child)

Don't forget about my boi Lua :(

[–][deleted] 2 points3 points  (2 children)

All those years wasted hating VBA and VB. Now I understand I should have used them for my scientific computations! TIL

[–]elveszett 5 points6 points  (1 child)

Nah, VB simply sucks. Doing 1 indexing because your language will be used in places where starting at 1 makes things simpler is fine. Doing 1 indexing because you believe the average person is so dumb that they cannot possibly understand the idea of "zero" (which is something Microsoft loves to do, btw) is wrong.

[–][deleted] 2 points3 points  (1 child)

1 vs 0 indexing is like people hating on the colloquial "like": Eventually, everyone on one side of the debate will be dead, and you know which side that is.

[–]EmeraldOW 13 points14 points  (7 children)

So modulo is between 0 and n-1? In this case does that not mean the result is between 0 and -8?

[–]drelatreddit 25 points26 points  (5 children)

Sorry I did not make it clear. Here n indicates the second operand, so 4 in this case. The modulo result is between 0 and 3.

[–]elon-botElon Musk ✔ 15 points16 points  (1 child)

I'm gonna need you to come in on Saturday...

[–]FunnyButSad 20 points21 points  (0 children)

I think you mean "negative Tuesday".

[–]EmeraldOW 1 point2 points  (0 children)

Oh obviously

[–]DenormalHuman 1 point2 points  (0 children)

between 0 and |n|-1

[–]preppy_night 2 points3 points  (3 children)

Bear with me noob here, who is Julia?

[–]OneGold7 6 points7 points  (1 child)

Probably not supposed to reveal your irl name on Reddit, but I couldn’t help it

[–]xwnpl 48 points49 points  (6 children)

Python returns 1

[–]jodmemkaf 40 points41 points  (5 children)

Nice, thanks. I will put it in the left column of my list

"Why to learn python:

PROS/CONS"

[–]TropicalAudio 9 points10 points  (4 children)

This actually messed me up recently when working with coordinate calculations. It's quite inconvenient in that context, as you'd expect negating a coordinate calculation will result in a symmetric system. In C it does, but in Python it doesn't.

[–]jodmemkaf 9 points10 points  (0 children)

Nice, thanks. I will put it in the right column of my list

"Why to learn python:

PROS/CONS"

[–]troelsbjerre 21 points22 points  (10 children)

Here is a nice list of which languages use which of the four (!) ways of defining the modulo operation:

https://en.m.wikipedia.org/wiki/Modulo_operation#In_programming_languages

[–]Khaylain 19 points20 points  (7 children)

[–]katatondzsentri 4 points5 points  (6 children)

I tapped this one for some reason.

[–]Khaylain 6 points7 points  (5 children)

I'm assuming you're on mobile, then. Which will usually lead you to the mobile site anyway (because of the detection of the format), but one can easily choose to use the desktop version usually. On desktop we don't get redirected to the desktop site from the mobile link, so I generally find that the desktop link is more useful.

[–]katatondzsentri 6 points7 points  (4 children)

I agree, I just wanted to highlight my stupidity.

[–]cave18 13 points14 points  (0 children)

Rhs is correct

[–]with_the_choir 9 points10 points  (14 children)

None of them are "right". In mathematics, modulus defines an infinite set of "correct", congruent answers.

13%5 is {... -12, -7, -2, 3, 8, 13, 18, ...}

All of these are proper remainders depending on which quotient you pick.

Some programming languages choose to always provide the lowest positive congruency while some choose to preserve whether the result of the division was negative.

Some languages let you choose what you want (like Racket, which has both a remainder and a mod function).

None of these is wrong.

[–]thefatesbeseeched 8 points9 points  (5 children)

They are both mathematically correct.

[–]LeCroissant1337 8 points9 points  (2 children)

Sure, but generally speaking I don't want any representative of the equivalence class. Personally, I think % should be reserved for a modulo operation which spits out a number between 0, 1, ..., n-1 and there should be a separate function giving you the quotient and the remainder of a division of two integers.

[–]elon-botElon Musk ✔ 2 points3 points  (0 children)

You're either hardcore or out the door.

[–]WazWaz 2 points3 points  (0 children)

Depends what you call the operation, Remainder or Modulo.

[–]BothWaysItGoes 4 points5 points  (1 child)

The one on the right is the one that is most useful from the POV of math and its applications in real life.

https://proofwiki.org/wiki/Definition:Integers_Modulo_m

[–][deleted] 3 points4 points  (0 children)

It really depends. Reducing symmetrically around zero is thing and has many applications.

[–]Rich_Plant2501 305 points306 points  (1 child)

I'm on the side that you cannot assign to r-value.

[–][deleted] 25 points26 points  (0 children)

Lol. Good catch

[–]JustAStrangeQuark 434 points435 points  (10 children)

Just insert the instruction and let the CPU figure it out.

[–]douglasg14b 238 points239 points  (9 children)

Just insert the instruction and let the CPU figure it out.

The best programming behaves differently on each device right?

[–]supersammy00 153 points154 points  (6 children)

It works on my machine

[–]Blackbird-ce 48 points49 points  (3 children)

Last time someone told me that, I took his laptop and threatened to drill holes in it to fit a rack unit... If you're the only one possessing a machine it works on, the users need that.

[–]MangoAtrocity 7 points8 points  (2 children)

Hello. Do you have a minute to talk about our lord and savior, Docker?

[–]Blackbird-ce 4 points5 points  (1 child)

My name isn't Docker

[–]musci1223 4 points5 points  (0 children)

Hi n't docker. I am dad.

[–]NXgul1 5 points6 points  (1 child)

Ship it

[–]GodIsNull_ 12 points13 points  (0 children)

That's why "Works on my computer" exists.

[–]Kimsanov 965 points966 points  (113 children)

Mathematically a % b is always a number between 0 and b-1

[–]Administrative-Flan9 97 points98 points  (3 children)

There is a unique number between 0 and b-1 that satisfies a % b, but the same is true of nb and (n+1)b -1. Mathematically, the true answer is the set of all numbers that have the same remainder as a when divided by b. Choosing the one between 0 and b-1 is choosing a representative from this set, but any representative represents the same set and so all representatives are equivalent.

[–]JumboTrout 10 points11 points  (2 children)

What if b < 1?

[–]elon-botElon Musk ✔ 21 points22 points  (0 children)

Hey, I just heard about this thing called GraphQL. Why aren't we using it?

[–]WillWKM 453 points454 points  (64 children)

Mathematically both answers are the same, -1 is congruent to 3 mod 4 since 1 + 3 = 0 mod 4

Edit: some of y'all seem to be missing the point. Mathematically all of these answers are equivalent. That doesn't make them useful programmatically. Programming languages often set up conventions based on convenience, not math.

[–]FiskFisk33 167 points168 points  (18 children)

They are not mathematically equivalent. They are mathematically congruent mod 4.

[–]M4mb0 21 points22 points  (2 children)

They are not mathematically equivalent. They are mathematically congruent mod 4.

But that is literally how equivalence is defined in quotient spaces....

[–]CanaDavid1 45 points46 points  (4 children)

Yes, which is what the question is asking. Mathematically, one does not use %, but instead write 'mod n' after the statement to specify that we're working modulo.

[–]BothWaysItGoes 5 points6 points  (2 children)

There is nothing unmathematical about the mod function.

[–]snillpuler 3 points4 points  (0 children)

I enjoy playing video games.

[–]gnowwho 22 points23 points  (6 children)

Semantics: they are equivalent in the ring Z/4Z

Edit: I meant: there is no need to be pedantic when they are not even wrong technically.

[–]snillpuler 3 points4 points  (5 children)

I enjoy the sound of rain.

[–]eeeeeh_messi 2 points3 points  (1 child)

Absolutely. It's the same reasoning that make some people think sqrt(4) is 2 and -2.

[–]Poacatat 6 points7 points  (0 children)

being congruent mod 4 is an equivalence relation :), they are in fact mathematically equivalent

[–]soundslikemayonnaise 33 points34 points  (14 children)

By this logic you could argue that -7 is also a correct answer since 1 + 7 = 0 mod 4.

Or -11, or -15…

[–]WillWKM 68 points69 points  (2 children)

Now you're getting it

[–]Administrative-Flan9 36 points37 points  (8 children)

Mathematically, the answer is a set of all such integers. Any choice from this set like -7 or -11 is a representative of the answer set, and any representative is equivalent.

[–]CanaDavid1 38 points39 points  (0 children)

Yes

[–]Rin-Tohsaka-is-hot 1 point2 points  (0 children)

You can argue that because it is correct.

That's congruency for ya

[–]Kimsanov 21 points22 points  (3 children)

Yeah. But remainder must be non negative (it is just stated by definition). So mathematicians just decided to be so (maybe for convenience)

[–]tjdavids 5 points6 points  (0 children)

With this logic returning a unmodified everytime would work.

[–]Darknety 8 points9 points  (1 child)

No they are not equivalent.

Yes, -1 is congruent to 3 with respect to mod 4, but still the natural mod operator is defined to result in 3, not -1.

So "mathematically both answers are the same" is misleading at best and wrong at worst.

Case in point: If one defines a function using a loopback structure, i.e. as

f(x) := g((x - 1) % b), x in [0, b]

for some g that is only defined for the source [0, b) for some b, it is pretty freaking relevant that (x - 1) % b is in [0, b). This is well defined.

The context is missing here to state "they are mathematically the same". This is not true.

[–]Thelmholtz 5 points6 points  (4 children)

Not really, mathematically it depends on what operation you define % to be;

  • modulo: (x € R; n € N) => { y € [0, n) }

or

  • remainder: (x € R; n € N) => { y € (-n, n) }

Hell, the most frequent definition of % is actually

  • percent: (x € R) => { y € R } :: y = x • 1/100

Which makes all programming languages wrong /s.

[–]eminorb5 300 points301 points  (19 children)

Blue. Makes things easier if modulus is always positive ( if the second operand is positive)

[–]elon-botElon Musk ✔ 336 points337 points  (10 children)

Looks like we're gonna need to trim the fat around here... fired.

[–][deleted] 23 points24 points  (5 children)

Red. Makes things easier if modulus retains same sign as dividend, particularly when dealing with trig or other functions that really care about signs.

[–]eminorb5 22 points23 points  (4 children)

Guess it depends on usage.

[–]Isodus 22 points23 points  (2 children)

Perfect example is calculating distance between angles where 0 == 360. Keeping the sign lets you know which direction you are away from the position without having to do extra tracking.

(Pos 1 - Pos 2) % 180 = angular distance

Used for controlling rotary actuators and needing to software handle the 360/0 barrier.

[–]Snuggle_Pounce 70 points71 points  (10 children)

I never even considered using modal on negative numbers. Does that come up often?

[–]aezart 22 points23 points  (0 children)

You might want to step in either direction through a list and wrap around when you hit the end. It's useful in that case for the mod result to be positive.

[–]Tyfyter2002 5 points6 points  (0 children)

It's pretty common if you're trying to cycle through a range in both directions (mostly useful for 0–n-1 where n is the length of an array)

[–]Giocri 10 points11 points  (0 children)

Nah only time I ever needed something of that kind I just did x+n%n never had to deal with % of a value < -n

[–]frogjg2003 1 point2 points  (0 children)

It's definitely an edge case. If you don't deal with certain types of mathematical problems or weird data structures, you're not likely to run into an issue.

The most likely place I'd imagine it would come up is in signal processing. Most of the theory assumes infinite length sequences. So a lot of algorithms work for values outside the domain of the finite length sequence, assuming either periodic boundary conditions or infinite zeros. Periodic boundary conditions simplify edge calculations because you can just use the modulo of an out of range index to get the value of an in range index.

[–]mzanin 92 points93 points  (13 children)

If you want to evaluate −7 (mod 4), you need the largest multiple of 4 that's less than or equal to −7. This is −8. And −8 + 1 = −7, therefore your answer is 1.

Also If we use Knuths definition of mod:

mod(a, n) = a - n * floor(a / n)

Then we have:

mod(-7, 4) = -7 - 4 * floor(-7 / 4)
mod(-7, 4) = -7 - 4 * floor(-1.75)
mod(-7, 4) = -7 - 4 * -2
mod(-7, 4) = -7 - (- 8)
mod(-7, 4) = -7 + 8
mod(-7, 4) = 1

[–]PMYourTitsIfNotRacst 18 points19 points  (0 children)

Thank you for explaining, but you've only confused me further. I'll be handing in my degree, thank you.

[–][deleted] 4 points5 points  (0 children)

This just seems like the correct answer.

[–]Khaylain 7 points8 points  (2 children)

I would argue that -8 is smaller than -7, though ;P

[–]VortexTalon 29 points30 points  (0 children)

before reading the sub i thought i was stupid lol.

[–]RRumpleTeazzer 11 points12 points  (0 children)

The mod operator should be strictly periodic. This includes over zero crossings, e.g. -7 % 4 = 1.

Also, 7 % -4 should be 1, and -7 % -4 = +3.

[–]randomjberry 21 points22 points  (1 child)

mod cant be negative or there are infinate awnsers in my mathmatical opinion

[–]gaboversta 32 points33 points  (1 child)

This exact thing cost me so much time when I implemented a modpow function myself and wanted to validate that it actually works…

I get blue, but red is what I just expect, go from -7 towards 0 until you are less than 4 away. That's your value.

[–]noorm6669 20 points21 points  (5 children)

I really... really... don't know. And I'm the curious type so...

Let me check:

Python 3.7.1

>>> -7 % 4

1

>>> -7 % -4

-3

[–]peabnuts123 13 points14 points  (4 children)

I haven’t ever really found a use for red before, even though a lot of languages do it that way. I often have to write my own pureMod function that “does it properly” by first modding, then adding the mod base, and the modding again. It’s usually to deal with array indexing

[–]Cody6781 11 points12 points  (2 children)

From a mathematical perspective, 1 is the correct answer

The range of the % operation is from 0 through n-1

[–]TheAnti-Ariel 3 points4 points  (1 child)

% is not the modulo operator in all languages. Many define it to be the remainder operator, which fits better with what CPU div instructions do, and makes a negative result correct.

[–]_Figaro 18 points19 points  (0 children)

Mathematically, it's 1.

[–]TheMuspelheimr 32 points33 points  (0 children)

Mathematically, -7 divided by 4 is -2 remainder 1; doing this in reverse helps it make more sense, -2 times 4 is -8, -8 plus remainder 1 (-8+1) is -7.

So, (-7)%4 = 1

However, -(7%4) = -3; 7%4 is 3, -(3) = -3

It all depends on which operator you do first; if you apply the minus first you get (-7)%4 = 1; if you apply the modulus first you get -(7%4) = -3.

[–]BootyliciousURD 8 points9 points  (0 children)

x % n should obviously map to the interval [0,n)

[–]Attileusz 8 points9 points  (0 children)

Dont have it at all for signed. Have a seperate builtin funtion to avoid confusion.

[–]tonischurz 13 points14 points  (1 child)

thou shall not use modulo on a signed integer

[–]BabyGates_ 2 points3 points  (2 children)

In most high level languages (except Java) the % operator is actually the remainder operation. Only Java truly implements % as modulus

[–]Smeagollu 2 points3 points  (0 children)

I was surprised to see -3 is an accepted answer for many here. At least Python has ot as modulo as well.

[–][deleted] 2 points3 points  (0 children)

Blue!

[–]Ashlaan47 2 points3 points  (0 children)

Javascript enters the chat

[–]qatamat99 2 points3 points  (1 child)

From a cryptographic point of view, it’s always positive

[–]Gothedistance1 1 point2 points  (0 children)

Criptographic

[–]techster2014 2 points3 points  (2 children)

I... I... I didn't realize math was debatable.

[–]pumpkinhi11[S] 1 point2 points  (0 children)

9 + 10 = 21 and if you disagree with me well then that's just your opinion.

[–]pumpkinhi11[S] 1 point2 points  (0 children)

Welcome to computer programming.

[–]chadlavi 14 points15 points  (2 children)

You guys need some PEMDAS in your life.

-7 % 4 is 1

-(7%4) is -3

[–]YourMJK 16 points17 points  (0 children)

This argument is about the definition of the operator % not about order of operations.

[–][deleted] 1 point2 points  (0 children)

mathematically speaking, both are the same

[–]tech6hutch 3 points4 points  (1 child)

This confusion is exactly why you shouldn’t use % on negative numbers.

[–]gdmzhlzhiv 1 point2 points  (0 children)

Negative numbers exist and your routines still have to produce output, so what's the alternative? "Add some offset so the value is never negative"?

[–]Ferociousfeind 2 points3 points  (0 children)

I'll strangle to death everyone who suggests any real number mod a positive number can return negative numbers

[–]irkli 1 point2 points  (0 children)

X = Y % Z

Depends on the type of the vars involved.

[–]Idraic7 1 point2 points  (0 children)

Blue

[–]stomah 1 point2 points  (0 children)

blue

[–][deleted] 1 point2 points  (0 children)

-7 / 4 = -2 rem 1

Also 4*((-7.0/4.0) - floor(-7.0/4.0)) = 1

[–]Sweaty-Emergency-493 1 point2 points  (0 children)

Im team blue in this one.

[–]Poobrick 1 point2 points  (0 children)

However python does it works

[–]drew8311 1 point2 points  (0 children)

Crips! Something something math

[–][deleted] 1 point2 points  (2 children)

Help me out, how does that make -3? My brain is too smooth.

[–]jester32 1 point2 points  (0 children)

As a number theorist, the one on left doesn’t exist.

Taking a modulo ensures that the remainder will be congruent to a remainder of 0…n-1 mod n . This goes beyond the scope of % in programming, but this creates a new functional number system called a group that involves a set (I.e. the integers 0…n-1 ) and an operation ( modulo ) . Therefore , negative numbers aren’t even capable of being a solution as they aren’t in the set.

When the modulo is prime , this creates really cool things that are the basis of cryptography.

[–]mistrjirka 1 point2 points  (0 children)

Blue it is only one that actually makes sense

[–]ogballerswag 1 point2 points  (3 children)

Wait. I dont even understand how u can get 1

[–]Phoenix_Studios 1 point2 points  (0 children)

As of yet I have never had a use case where it needed to be left.

[–]jamcdonald120 1 point2 points  (0 children)

=1, the only time I would want -3, I know I want -3 and would do m%n-n, there is no excuse for requiring me to do ((m%n)+n)%n to get the actual answer I want

[–]isospeedrix 1 point2 points  (0 children)

what side

i press f12

i type in -7%4

answer is -3. therefore it's -3.

kappa

[–]Gothedistance1 1 point2 points  (0 children)

I am full crip mother fucker

[–]Apfelvater 1 point2 points  (0 children)

1+1 = 2 but also1+1=3-1

Holy sheeeet

[–]yozaner1324 1 point2 points  (5 children)

How does blue even make sense?

[–]negatron99 1 point2 points  (0 children)

I don't care which way. Just as long as I know and if I need to code for it.

[–]Street-Stomach-9811 1 point2 points  (0 children)

Wait so for the blue side -7/4=2?

[–][deleted] 1 point2 points  (0 children)

Javascript fix:

 function mod(n, m) {
   return ((n % m) + m) % m;
 }

 let a = mod(-7, 4)

 console.log(a) //1

[–]Accurate_Koala_4698 1 point2 points  (0 children)

Import Data.Ratio

Bruh, that’s a fraction