# Closest integer to a float number

**URL:** <https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243>\
**Category:** New to Julia\
**Created:** [April 13, 2021, 11:47pm UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243 "2021-04-13T23:47:00Z")\
**Posts on this page:** 8\
**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:** [April 13, 2021, 11:47pm UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/1 "2021-04-13T23:47:00Z")

</div>

Hello,

I would like to apply a certain operation when my time variable `t` becomes a multiple of `t0`. Is there a robust way to implement this?

I think this boils down to check whether t/t0 is an integer in a robust fashion.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 14, 2021, 12:10am UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/2 "2021-04-14T00:10:39Z")

</div>

`t % t0 == 0` works until floating point precision bites you. Then you’ll need to check whether `t % t0` is close enough to zero. What’s close enough depends on your application.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 14, 2021, 12:11am UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/3 "2021-04-14T00:11:54Z")

</div>

There’s `isinteger` if you want to know if it’s exactly an integer:

```julia
help?> isinteger
search: isinteger

  isinteger(x) -> Bool

  Test whether x is numerically equal to some integer.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> isinteger(4.0)
  true

```

Maybe something like this would be helpful for testing if it’s close to an integer up to some tolerance:

```julia
julia> near_integer(x; tol=1e-3) = abs(x - round(x)) <= tol
near_integer (generic function with 1 method)

julia> near_integer(1.001)
true

julia> near_integer(1.1)
false

```

---

<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:** [April 14, 2021, 12:12am UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/4 "2021-04-14T00:12:21Z")

</div>

Note that you have to check both close to `0`, and close to `t0`

---

<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:** [April 14, 2021, 12:42am UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/5 "2021-04-14T00:42:15Z")

</div>

Thank you all for your answers.

Can’t we just check if the ratio `t/t0` is close enough to an integer

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 14, 2021, 12:43am UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/6 "2021-04-14T00:43:54Z")

</div>

I guess it depends on whether you want to have a relative or absolute tolerance. The ratio won’t work for numbers close to 0 though

---

<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:** [April 14, 2021, 2:19am UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/7 "2021-04-14T02:19:29Z")

</div>

> [@giordano](#):
>
> I guess it depends on whether you want to have a relative or absolute tolerance.

If you are checking whether something is close to an integer, the natural scale for comparison is 1 (the difference between consecutive integers), in which case relative and absolute tolerance become the same thing. i.e. it sounds like you just want to check `abs(t/t0 - round(t/t0)) < δ` for some tolerance `δ`.

Of course, you haven’t said anything about the underlying problem that is driving this. It could be that there is a completely different approach based on reformulating your problem in a more robust form.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 14, 2021, 4:45am UTC](https://discourse.julialang.org/t/closest-integer-to-a-float-number/59243/8 "2021-04-14T04:45:49Z")

</div>

You might want to check for the first time `t` is greater than `t_prev+t0` which feels more robust than computing the ratio, at least if there can be any form of jitter involved.
