Arithmetic errors

So I perform the following code and the result comes out erroneous in both the single line interface and R studio:

-8%%3
[1] 1
It should be negative -2 but it comes out positive 1. I don't understand, I have deleted and re-installed R multiple times but the error still persists. If I type the following code, the error still persists but the sign becomes negative
8%%-3
[1] -1
Please help!!!!


Referred here from support.rstudio.com

Operator precedence: use

-(8%%3)
#> [1] -2

Created on 2020-07-14 by the reprex package (v0.3.0)

What I don't understand is, why would this (-8%%3) not give a negative value?

The %% operator is for modulus or more commonly known as "remainder" when I was in elementary school. The "remainder" can only be positive

8%%3
#> [1] 2
(-8)%%3
#> [1] 1
-(8%%3)
#> [1] -2
-8%%3
#> [1] 1

Created on 2020-07-14 by the reprex package (v0.3.0)

There's quite a discussion on this operator with negative numbers here though it doesn't include R. R does what Ruby and Python do. https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/#:~:text=The%20modulo%20operator%20returns%20the,negative%20numbers%20into%20the%20mix.&text=Doing%20an%20integer%20division%20and,by%20n%20without%20a%20remainder.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.