What's the difference between '1f32' and '1 as f32', such as performance or whatever?

A small problem. I see two ways to write on some documents.

x_move -= 1f32;
// or `x_move -= 1 as f32`?

1f32 is a floating point literal. You could also write 1. and let it be inferred as f32 or f64 from the surrounding context.

1 as f32 is an integer literal which you are casting to f32. The exact integer type is not specified, but it will fall back to i32. This will still optimize to the same thing eventually, but it's a little clunky to have such a cast when you could write the floating point directly.

3 Likes

1 as f32 will create an i32 first and the convert it. Given compiler optimizations exist, the difference is probably nonexistent, unless your numbers become too large to fit an i32, in which case the overflowing_literals lint would help you though. Nonetheless, 1_f32 (or equivalently 1f32, though I personally like the underscore) is a lot more idiomatic in my opinion.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.