Why is this an InexactError()?

This is an attempt to translate an example from K&R C. I am unable to parse the InexactError() the code throws up.

struct Point
    x::Int
    y::Int

    function Point()
        new(0, 0)
    end

    function Point(i::Union{Int, Float64}, j::Union{Int, Float64})
        new(i, j)
    end

end

struct Rect
    p1::Point
    p2::Point
end

screen = Rect(Point(), Point(16.0, 9.0))

middle = Point((screen.p1.x + screen.p2.x)/2, (screen.p1.y + screen.p2.y)/2)

middle throws up an InexactError():

InexactError()
convert(::Type{Int64}, ::Float64) at float.jl:679
Point(::Float64, ::Float64) at KRC.jl:52

How do I correct this?

Either change x and y to be Float64 or explicitly round using round((screen.p1.x + screen.p2.x)/2) or round(Int, (screen.p1.x + screen.p2.x)/2).

julia> Int(1.2)
ERROR: InexactError()
Stacktrace:
 [1] convert(::Type{Int64}, ::Float64) at .\float.jl:679
 [2] Int64(::Float64) at .\sysimg.jl:24

julia> Int(round(1.2))
1 

julia> round(1.2)
1.0

julia> round(Int, 1.2)
1

julia> Int(1.0)
1
2 Likes

Got it, my struct definition was wrong. :neutral_face: