I’d like to announce the RoundingIntegers, a tiny package that adds new Integer
types. Very simply, RoundingInteger
s always round when you assign to them. For example:
julia> Int(7.2) # fails with "regular" integers
ERROR: InexactError()
in Int64(::Float64) at ./sysimg.jl:53
julia> RInt(7.2) # but not with a rounding integer
7
I suspect their main usage is in containers, e.g., Vector{RInt}
would allow you to assign floating-point values that then automatically get rounded. My motivation for adding this was that I had written specialized logic for this kind of operation:
safe_setindex!{T<:Integer}(a::AbstractArray{T}, x::Real, inds...) = a[inds...] = round(T, x)
safe_setindex!(a::AbstractArray, x::Real, inds...) = a[inds...] = x
one too many times. It just seems cleaner to encode it in the type (it lets the user decided whether s/he wants an error or no error for this kind of operation).