# Difference between rem2pi and mod2pi?

**URL:** <https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527>\
**Category:** General Usage\
**Created:** [September 21, 2021, 2:14pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527 "2021-09-21T14:14:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 21, 2021, 2:14pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/1 "2021-09-21T14:14:07Z")

</div>

I’m trying to understand the difference between `rem2pi(x, RoundNearest)` and `mod2pi` (or more precisely `mod2pi - π`) from [Base, Math functions](https://docs.julialang.org/en/v1/base/math/). Is that correct that roughly speaking both function return the same thing, that is the principal value of an angle between -π and +π?

I understand from the doc that `mod(x, y)` is the same as `rem(x, y, RoundDown)`, but things gets complicated when it says that `mod2pi(x)` is different from `mod(x, 2π)`…

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 21, 2021, 2:31pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/2 "2021-09-21T14:31:35Z")

</div>

When it says `mod2pi(x)` is different from `mod(x, 2π)`, it just means that `mod(x, 2π)` uses a floating point representation of `2pi`, while `mod2pi` uses the mathematical number. This makes a difference for large inputs, since the rounding error in `2pi` will get multiplied by `floor(x/2pi)`.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 21, 2021, 2:47pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/3 "2021-09-21T14:47:42Z")

</div>

Thanks. So is it correct that `mod2pi(x)` is the same as `rem2pi(x, RoundDown)`?

And then, since the doc of `rem2pi(x, RoundDown)` says it return a number in [0, 2π], how is `mod2pi - π` different from `rem2pi(x, RoundNearest)` which return a number in [-π, π]? Are those the same except for the extraneous substraction (which of course brings some error)? Or are there some input values which return different output values?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 21, 2021, 2:54pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/4 "2021-09-21T14:54:38Z")

</div>

```julia
julia> rem2pi(-1.0, RoundNearest)
-1.0

julia> mod2pi(-1.0)
5.283185307179586

```

negative numbers strike again.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 21, 2021, 3:14pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/5 "2021-09-21T15:14:56Z")

</div>

Good catch! Also, I now realize that my reasoning about substracting π to `mod2pi(x)` is silly.

So it seems that for my usecase, I should stick with `rem2pi(x, RoundNearest)`.

I’m not sure what you mean by _“negative numbers strike again.”_, but my first implementation, before discovering `rem2pi` and friends was `(x+π) % 2π - π` which at first seems to work but indeed breaks when `(x+π)` is negative. This lead me to the surprising discovery that `rem(x, y)` output range depends on the sign of `x` by default (i.e. when not specifying the rounding mode argument which I didn’t know it existed!). Is it this kind of “surprise for new users” that you were thinking of?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 21, 2021, 3:23pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/7 "2021-09-21T15:23:03Z")

</div>

What’s wrong with it? `-1.0+2pi=5.283185307179586` so it’s in the correct range…

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 21, 2021, 3:25pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/9 "2021-09-21T15:25:30Z")

</div>

@pierre-haessig This is the same as the difference between the `rem` and `mod` functions (as specified by ieee). `rem` returns a result with the same sign as the divisor. `mod` is periodic. These definitions correspond when the divisor and dividend have the same sign.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 21, 2021, 3:50pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/10 "2021-09-21T15:50:19Z")

</div>

Thanks for the context. In fact, I think the suprise for me was mostly about coming from Python where `-1 % 3` returns 2 (I just check to be sure!) while Julia returns -1.

Digging further in Python, I just found out about the relatively new (Python 3.7+) function [`math.remainder`](https://docs.python.org/3/library/math.html#math.remainder) which claims to be the “IEEE 754-style remainder”. Since the doc says it returns `r` such that |r| ≤ y/2, this maps to Julia’s `rem` used with `RoundNearest` if I’m not mistaken.

About realizing one of my proposition was silly: it’s just that I thought `mod2pi(x) - π` would give the principal angle in [-π, π]. But in fact, it should be `mod2pi(x+π) - π`. Otherwise, the output range is correct, but the value is wrong by 180°…

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [September 21, 2021, 3:52pm UTC](https://discourse.julialang.org/t/difference-between-rem2pi-and-mod2pi/68527/11 "2021-09-21T15:52:38Z")

</div>

By the way, I can access the doc of `rem(x, y, r::RoundingMode=RoundToZero)` through the REPL, but oddly enough I don’t see it in the doc [Base.rem](https://docs.julialang.org/en/v1/base/math/#Base.rem) which is only about `rem(x, y)`. Why is that?
