# Calculation of remainder, floating point issues

**URL:** https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338
**Category:** General Usage
**Created:** [August 29, 2023, 6:20pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338 "2023-08-29T18:20:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)
#### Post date: [August 29, 2023, 6:20pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/1 "2023-08-29T18:20:56Z")

</div>

The following remainder calculations are a bit surprising to me, as I’d expect to get a remainder of 0 (or something like ~1e-18) in each case. I guess there is some floating point issue:

```julia
julia> rem(500,0.625)
0.0

julia> rem(50,0.0625)
0.0

julia> rem(5,0.00625)
0.006249999999999723

julia> rem(0.5,0.000625)
0.0006249999999999896

julia> rem(0.05,0.0000625)
1.734723475976807e-18

```

In particular, for the 3rd and 4th results, the generated remainder is essentially the same as the second argument, e.g.:

```julia
julia> rem(0.5, 0.000625) ≈ 0.000625
true

```

If I’m testing for a 0 remainder, then the implication is that I should test for a remainder that is either 0 _or_ the divisor, but I’m curious if this expected or if there is an issue here.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 29, 2023, 7:02pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/2 "2023-08-29T19:02:57Z")

</div>

One would expect the remainder to either be very close to `0.0` or very close to `0.0...625`, depending on whether it’s slightly bigger or smaller than a multiple. Or rather, whether the floating point roundoffs pull in one direction or the other.

---

<div class="post-metadata">

### Author: ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)
#### Post date: [August 29, 2023, 7:06pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/3 "2023-08-29T19:06:08Z")

</div>

This is an issue with floating point numbers in general. in contrast to integers floats are (for most values) not exact, but rounded to the nearest value that can be represented.

This is exactly what happens in your example - the divisor (and dividend) is rounded to the nearest float and depending on whether this rounding happens up or down, the last remainder is either slightly larger than the divisor (you get a result of ~2e-18) or slightly smaller (you get a result of ~divisor - 2e-18).

---

<div class="post-metadata">

### Author: ![natlampen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natlampen/32/36640_2.png) [@natlampen](https://discourse.julialang.org/u/natlampen)
#### Post date: [August 29, 2023, 7:09pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/4 "2023-08-29T19:09:27Z")

</div>

The documentation hints that rem(x,y,r) is the same as x - y\*round(x/y,r). For x = 5 and y = 0.00625 this actually returns 0. So maybe a bug is hiding somewhere?

---

<div class="post-metadata">

### Author: ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)
#### Post date: [August 29, 2023, 7:19pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/5 "2023-08-29T19:19:27Z")

</div>

The comments above about floating point roundoffs are what I was alluding to as “floating point issues”. If the remainder value is e.g. 2e-18, that’s clearly roundoff.

However, @natlampen hits the nail on the head - when the “remainder” value is essentially equal to the divisor, it seems like this is an obvious result to correct. Hence I also think about a “bug hiding somewhere”.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 29, 2023, 7:44pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/6 "2023-08-29T19:44:59Z")

</div>

> [@\_bernhard](#):
>
> This is exactly what happens in your example - the divisor (and dividend) is rounded to the nearest float

In particular, `0.00625` is not exactly representable in binary floating point, so it is rounded to a slightly larger value:

```julia
julia> big(0.00625)
0.00625000000000000034694469519536141888238489627838134765625

```

(In contrast, `5.0` _is_ exactly represented.) So, you are actually computing the remainder of 5 \div 0.00625000000000000034694469519536141888238489627838134765625, which is _not_ zero. The correct quotient is therefore 799, _not_ 800 (=5/0.00625), and the correct remainder computed in arbitrary precision arithmetic to about 300 decimal digits (`setprecision(BigFloat, 1024)`) is:

```julia
julia> 5 - 799 * big(0.00625)
0.00624999999999972279118853890622631297446787357330322265625

```

If we round this to the nearest `Float64` value, we get:

```julia
julia> Float64(5 - 799 * big(0.00625))
0.006249999999999723

julia> rem(5, 0.00625)
0.006249999999999723

```

So, in fact, `rem` is giving _exactly_ the correct answer to the question you asked (i.e., the exact answer rounded to `Float64`).

It’s just that you are not asking the question you thought you were asking, because `0.00625` in floating point is not the number you think it is.

---

<div class="post-metadata">

### Author: ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)
#### Post date: [August 29, 2023, 8:06pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/7 "2023-08-29T20:06:25Z")

</div>

Thanks @stevengj this is a very clear example. Again, the general issues with floating point representation are clear - in fact I tried t=displaying values following more or less the logic you did. The mistake I made was to try to use `@printf` and I stopped a few digits short; next time I will use `big` which I didn’t know of before and thoroughly displays the issues. If I’d used `big` I wouldn’t have posted 🤷

It would be useful to include a 1-line example like this in the docs just to help make clear what the implications of “exact” might be

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 29, 2023, 8:13pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/8 "2023-08-29T20:13:16Z")

</div>

> [@lwhitefox](#):
>
> It would be useful to include a 1-line example like this in the docs just to help make clear what the implications of “exact” might be

Sure, but realize also that this is not specific to Julia. The same floating-point standard is followed by essentially all modern languages, and there is lots of learning material online about this (including in Julia).

(Whether `big` prints all of the digits, i.e. an exact decimal representation, depends on the current precision setting, since `BigFloat` is variable precision. I don’t remember off the top of my head if there is a function that always prints the exact decimal representation of a floating-point value, regardless of the precision and regardless of how many decimal digits are required?)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 30, 2023, 12:31pm UTC](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/9 "2023-08-30T12:31:22Z")

</div>

3 posts were split to a new topic: [Printing exact decimal representations](https://discourse.julialang.org/t/printing-exact-decimal-representations/103365)
