Please check following example
def foo(x : Int32) : Int32
x
end
def bar(x : Float32) : Float32
x
end
bar(foo 1) # Fails, casting an Int32 to a Float32 might lose precision
Why i consider, casting a Float32 to Int32 will lose precision?
Please check following example
def foo(x : Int32) : Int32
x
end
def bar(x : Float32) : Float32
x
end
bar(foo 1) # Fails, casting an Int32 to a Float32 might lose precision
Why i consider, casting a Float32 to Int32 will lose precision?
First of all, the cast is Int32 to Float32, not the other way around. Second, not all Int32 can be represented exactly by a Float32. Which is not hard to see, considering they have the same amount of bits but Float32 covers a bigger range of numbers. That means there needs to be holes somewhere where two integers map to the same float.
Thanks for explain.
And, current compiler no this error message for now, it just output:
Error: no overload matches 'bar' with type Int32
Overloads are:
- bar(x : Float32)
I copy some code from my note with comment, my fault.