I conducted several tests in Javascript, I will show them (inserted from the console):
-1 % 4
-1
-4 % 4
-0
-8 % 4
-0
-6 % 4
-2
-6 % -4
-2
6 % -4
2
-1% 4 is really -1, however I really don't understand why Javascript does not give 3, which is equally correct, but more canonical.
What is -0? I know -4/4 = -1, which is an integer, but I'm not sure what -0 is.
I am puzzled by the Javascript modulo calculation.
My tests were motivated by a slightly unpleasant surprise that I was working on a gallery, and I have a pointer to the active picture. There are two buttons for the next and previous buttons. The next image of the last image is the first image, and the previous image of the first image is the last image. I am changing the index as:
currentImageIndex = (currentImageIndex + 1) % images.length;
. , :
currentImageIndex = (currentImageIndex - 1) % images.length;
, , , , , currentImageIndex. console.log-ed , -1! , :
currentImageIndex = (currentImageIndex + images.length - 1) % images.length
, . , -, , Javascript, , -0 -4% 4, .