ERROR: LoadError: InexactError: Int64(Int64, 7.5)

I am practising Julia on; there’s a ques name Area of a Triangle in that I am getting an error

ERROR: LoadError: InexactError: Int64(Int64, 7.5)
 [1] Type at ./float.jl:700 [inlined]
 [2] convert(::Type{Int64}, ::Float64) at ./number.jl:7
 [3] top-level scope at none:0
 [4] include at ./boot.jl:317 [inlined]
 [5] include_relative(::Module, ::String) at ./loading.jl:1044
 [6] include(::Module, ::String) at ./sysimg.jl:29
 [7] exec_options(::Base.JLOptions) at ./client.jl:266
 [8] _start() at ./client.jl:425
in expression starting at /CandidateCode/CandidateCode.jl:9

It takes base and height as the input to get the area of the triangle in Int64 format, so after doing the multiplication (0.5 * b * h), I converted it to Int64 format. It gives an error for one test case where b=5 and height =3.


b,h = parse(Int64, readline()), parse(Int64, readline())
println(convert(Int64, 0.5*b*h))

Output for other test cases:

The error tells you that Julia can’t convert 7.5 into an integer (because it isn’t a whole number - makes sense).

In my opinion you should perform these calculations with Float64 as type.

But for the input given base =16 and height = 10, if I didn’t convert it to Int64, it will provide 80.0, and that’s not the expected output. So for that, I used convert()

Format the Float64 to match the spec instead of converting to an Int. Perhaps you can find examples here: How to format float numbers in human-friendly format?