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

Hi all,

Maybe it’s a silly question, but I have some code where I naturally pass around Float64s into my functions. For some part of the operations however, these are always discrete (so in this part alone they could as well be Ints). 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:

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

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

myfunction(discretefloats)


A wrapper type like

struct DiscreteFloat <: AbstractFloat
value::Int
end


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

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

thanks both of you for your reply. So I was more thinking in terms of “safe” use of this function I want to write. Float64s 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 Float64s 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)?

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.

4 Likes

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.

there is isinteger(x::Float64)

2 Likes

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