Floating-Point Types in Rust

Share this video with your friends

Send Tweet

In this lesson we take a look at Floating-Point values and the f32 and f64 types. We'll also look at the different ways of defining and creating values of such types.

J. Matthew
J. Matthew
~ 5 years ago

If both types would fit, so that is either f32 or f64, Rust will use f64 by default.

Why default to f64 (which I assume takes more memory)? I would have expected the opposite.

Pascal Precht
Pascal Precht(instructor)
~ 5 years ago

Hey,

once again a very good question that I also find very hard to answer. As you know, I'm certainly not in the position to explain certain design decisions of the language, however I've done some research and ran into this Reddit post: https://www.reddit.com/r/rust/comments/50480b/floating_point_numbers_in_rust/

The author of the post asks pretty much the same question and people seem to mostly answer that Rust followed what C/C++ is doing (more or less, C has double which is the default type for numbers as per comments, and takes 64 bits as well).

One comment says that it's not just necessarily because C did it this way but more because of the same reasons why C did it this way. Unfortunately, they don't seem too elaborate on the "why" here...

Wish I could provide a better answer.