InexactError: Int64


I am encountering this error- InexactError: Int64.

InexactError: Int64(2.5)

When I search for this I got this article [unrelated blogspam link redacted] and as per this, explaining an answer in which they are talking about the value being converted can be exactly represented as an integer, or handle the conversion appropriately. And on the other hand, this resource ERROR: LoadError: InexactError: Int64(Int64, 7.5).

However I fixed the value issue but how to fix the floating-point number, because it’s must.

x = 2.5
y = Int64(x)  # This will cause an InexactError

# Proper handling
y = round(Int64, x)

For the above code, I converted the valve in integer. But my priority is to use floating-point number.

How can I fix them?


When x cannot be exactly represented as an integer, Int64(x) will raise an InexactError.

Int64(2.0) # works, equals to integer 2
Int64(2.1) # raises InexactError because 2.1 is not an integer

I don’t understand the last part of your post. If your priority is to use a floating-point number, then why would you call Int64 at all? Maybe your goal is to round the number to the nearest integer but keep the floating-point type? In that case, use just round without the first argument:

x = 2.1
y = round(x) # equals to float 2.0
1 Like

The most common situation for an error like this is that someone accidentally constructed an array of integers and then tried to store a floating-point number, e.g.

julia> x = [1,2,3,4]
4-element Vector{Int64}:

julia> x[2] = 3.5
ERROR: InexactError: Int64(3.5)

The solution is to make the array a container of floating-point numbers to start with, e.g. via x = Float64[1,2,3,4] or x = [1.0,2,3,4].