Integer division #19126
pd-giz-dave
started this conversation in
Ideas
Integer division
#19126
Replies: 2 comments 3 replies
-
The compiler did exactly as you asked. If you want it to do something different, ask it to do that. width := 100
height := 50
ratio := f32(height) / width // result is a float of 0.5
expect := 0.5 // an f32
if ratio == expect { println('ok')} else { println('fail')} // succeeds
println(ratio) |
Beta Was this translation helpful? Give feedback.
0 replies
-
My point is that I made an error but it was 'hidden' by the float to integer corecion in the ==. IMO auto coerecion between integer and float is dangerous because it can lead to unexpected behaviour as in my very simple example. If the compiler failed in my eg then it would trigger a correction to your example. Without such a hint the error would go undetected until run-time if at all. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm just getting started using V and I like it, but there is an issue that keeps tripping me up and that is division of integers yields an integer, reasonable but dangerous in the following context:
width := 100
height := 50
ratio := height / width // result is an int of 0
expect := 0.5 // an f32
if ratio == expect { println('ok')} else { println('fail')} // fails
println(ratio)
I would expect f32 == int to fail at compile time as a type mismatch
How about a new operator '' meaning coerce operands to float and generate a float answer?
Beta Was this translation helpful? Give feedback.
All reactions