# Possible to have a type of discrete values/floats?

**URL:** <https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 7, 2020, 11:05am UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158 "2020-04-07T11:05:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [April 7, 2020, 11:05am UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/1 "2020-04-07T11:05:18Z")

</div>

Hi all,

Maybe it’s a silly question, but I have some code where I naturally pass around `Float64`s into my functions. For some part of the operations however, these are always discrete (so in this part alone they could as well be `Int`s). I want to write a function now that has a method on these kind of “discrete floats”. Is there a good way to do that? Some example to visualise my question below:

```julia
discretefloats = [4.0, 3.0, 10.0, 8.0, 2.0]

function myfunction(vals::Vector{SomeTypeForThis})
    # do stuff on discrete floats
end

myfunction(discretefloats)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 7, 2020, 11:38am UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/2 "2020-04-07T11:38:14Z")

</div>

A wrapper type like

```julia
struct DiscreteFloat <: AbstractFloat
    value::Int
end

```

with all the relevant methods defined (depending on what you need)?

---

<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 7, 2020, 2:34pm UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/3 "2020-04-07T14:34:10Z")

</div>

> [@mlanghinrichs](#):
>
> For some part of the operations however, these are always discrete (so in this part alone they could as well be `Int` s).

`Float64` operations on integer values up to \pm 2^{53} are exact, so why do you need a different method for this?

---

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [April 7, 2020, 7:49pm UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/4 "2020-04-07T19:49:20Z")

</div>

thanks both of you for your reply. So I was more thinking in terms of “safe” use of this function I want to write. `Float64`s are per se no problem, but the underlying function does only make sense for discrete floats or integers (basically it’s a resampling method of a discrete count distribution). So I thought if I can somehow constrain the type of the input variable, myself (and potentially other users in some distant galaxy) could not misuse the function.

But maybe that is a wrong way of thinking. Would `Float64`s and a good documentation be just fine?  
Or throwing some error if the float is not discrete (at the cost of a small check to do this)?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 7, 2020, 8:06pm UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/5 "2020-04-07T20:06:44Z")

</div>

If it only makes sense or integers then you can just limit your input to that type.

Now the only reason I can think of, from your description, that you might not want this is for the convinence of the user. If that is the case, then you can’t possibly have any check based on the type. All what you need to have in that case is a check on the floating point values, say, by just convert them to integers which will throw an error if the convertion can’t be done exactly.

---

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [April 7, 2020, 9:12pm UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/6 "2020-04-07T21:12:25Z")

</div>

yes that sounds totally reasonable. I’m sometimes a bit unsure how to solve these kind of “stylistic” issues, so thanks for all your help and explanations.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 8, 2020, 6:03pm UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/7 "2020-04-08T18:03:26Z")

</div>

there is `isinteger(x::Float64)`

---

<div class="post-metadata">

**Author:** ![mlanghinrichs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlanghinrichs/32/50371_2.png) [@mlanghinrichs](https://discourse.julialang.org/u/mlanghinrichs)\
**Post date:** [April 8, 2020, 6:13pm UTC](https://discourse.julialang.org/t/possible-to-have-a-type-of-discrete-values-floats/37158/8 "2020-04-08T18:13:15Z")

</div>

thanks for the hint, that’s nice! also because it’s completely clear then what I’m checking for.
