5 ms·
Is f32 the only suffix for float literals? That seems really noisy compared to just f in C. I'm mostly concerned about vector and matrix declarations, but even
by slavik81 9y ago
Is f32 the only suffix for float literals? That seems really noisy compared to just f in C. I'm mostly concerned about vector and matrix declarations, but even in the examples given, 1.0f32 / 0.0f32 looks like a mess of numbers compared to 1.f / 0.f;
- kibwen 9y ago> Is f32 the only suffix for float literals? Nope. `f32` is for single-precision floats, and `f64` is for double-precision floats. There have been some people lobbying for `f16` and `f128` as well. Also, you don't need to use those suffixes. I imagine the OP is using them for maximum explicitness, but you can just write `1.0` and it will be inferred to a floating-point type as necessary (contrast `1`, which will be inferred to an integral type).
- steveklabnik 9y ago> I imagine the OP is using them for maximum explicitness I used them because that's the style that was used inside of std::f32; const INFINITY: f32 = 1.0 / 0.0; totally works.
- tomjakubowski 9y agoGLSL programmers may rejoice in learning that `1.` is accepted syntax too.
- GenKali 9y agoHaving used Rust since pre-release I'm slightly embarrassed that I'm only learning about this today.
- ComputerGuru 9y agoThat's ok. I presume you weren't a C/C++ dev before, that's old school shorthand for C coders that don't want to type out suffixes or cast numeric values.
- GolDDranks 9y agoYes, it is, for 32-bit floats. However, most of the time you don't need to specify that, because it will be inferred from the context. (And if it isn't you can introduce hints like `let growth_rate: f32 = 50.0/140.0`) Also, Rust supports having _ anywhere in number literals. Some may argue that it makes things even noisier, but it helps to separate the suffix from the value itself: `100.0_f32`.
- slavik81 9y agoVery nice. As you say, it doesn't just cast at the end, but actually uses the type through the whole calculation! To illustrate the difference: Rust: fn main() { let a: f32 = 1e7 + 0.5 + 0.5; let b: f32 = 1e7f32 + 0.5f32 + 0.5f32; println!("{}", a==c); // true } C: #include <stdio.h> int main() { float a = 1e7 + 0.5 + 0.5; float b = 1e7f + 0.5f + 0.5f; printf("%s\n", a==b ? "true" : "false"); // false } That's awesome. I'm totally fine with an annoying suffix like f32 if I never actually need to use it. While f might be a nicer suffice than f32, no suffix is even better.
- steveklabnik 9y agoYou often don't need it at all; fn foo(x: f32) { println!("{}", x); } fn main() { let x = 5.0; foo(x); } It will look at the signature for foo, and infer that x must be f32.
- Arkanosis 9y agoWhoa! Isn't this kind of inference dangerous? It seems that whichever call comes first is used to infer the type, so a single added line of code can change the type of a variable… fn foo32(x: f32) { println!("{}", x); } fn foo64(x: f64) { println!("{}", x); } fn bar64() { let x = 5.0; // f64 foo64(x); foo32(x); } fn bar32() { let x = 5.0; // f32, not f64 foo32(x); foo64(x); }
- dbaupp 9y ago
- bluejekyll 9y ago_f32 is https://doc.rust-lang.org/std/primitive.f32.html https://doc.rust-lang.org/std/primitive.f32.html _f64 is https://doc.rust-lang.org/std/primitive.f64.html https://doc.rust-lang.org/std/primitive.f64.html