# Strange behavior of mod with float

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-mod-with-float/93536>\
**Category:** General Usage\
**Tags:** question, numbers, float\
**Created:** [January 25, 2023, 9:39pm UTC](https://discourse.julialang.org/t/strange-behavior-of-mod-with-float/93536 "2023-01-25T21:39:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [January 25, 2023, 9:39pm UTC](https://discourse.julialang.org/t/strange-behavior-of-mod-with-float/93536/1 "2023-01-25T21:39:38Z")

</div>

Hello,

What is the best way to determine if a float number `b` is an integer multiple of another float number `a`, (i.e. `b = k a` with `k` an integer)? I wanted to use the following test  
`abs(mod(b, a))<1e-12`, but I am confused with the behavior of the `mod` routine:

```julia
@show mod(2*0.1, 0.1), mod(3*0.1, 0.1), mod(5*0.1, 0.1), mod(8*0.1, 0.1), mod(9*0.1, 0.1)

```

Output:

```julia
(0.0, 2.7755575615628914e-17, 0.09999999999999998, 0.0, 0.09999999999999998)

```

Aren’t we supposed to get `0` for all these results (up to machine precision)? I would appreciate your help for a more robust test.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 25, 2023, 10:32pm UTC](https://discourse.julialang.org/t/strange-behavior-of-mod-with-float/93536/2 "2023-01-25T22:32:49Z")

</div>

> [@mleprovost](#):
>
> determine if a float number `b` is an integer multiple of another float

Linking [similar thread](https://discourse.julialang.org/t/how-to-test-if-one-float-is-an-integer-multiple-of-another/70745/3).

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [January 25, 2023, 10:35pm UTC](https://discourse.julialang.org/t/strange-behavior-of-mod-with-float/93536/3 "2023-01-25T22:35:26Z")

</div>

Thank you for your answer. I definitely missed this thread.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 25, 2023, 10:52pm UTC](https://discourse.julialang.org/t/strange-behavior-of-mod-with-float/93536/4 "2023-01-25T22:52:12Z")

</div>

> [@mleprovost](#):
>
> abs(mod(b, a))\<1e-12

For what it’s worth you can get that idea to work if you shift the `mod` away from zero:

```julia
abs(mod(b + 0.5 * a, a) - 0.5 * a) < 1e-12

```
