let exact_range: f64 = 42.6;
let range1: usize = exact_range.floor() as usize;
Is there a method somewhere in the standard library that does floors a f64 float and returns a usize directly? I see the as usize not as inherently unsafe, but bug-prone, since I could also call exact_range as usize directly (not sure how that would convert float to int tbh). And since the floor method always returns an "integer" (float ending in .0), I thought there would be a floor method that directly returns a usize or any other integer with enough bits (so not a u8 or something).
There is no primitive integer type with enough bits to represent all possible intger-valued floats. An f64 has a maximal finite value of more than 10308, which would require an approximately 900-bit integer to accomodate.
Searching through the documentation it doesn’t seem like there is.
It also seems difficult to implement, because the integer range of f64 is much greater than usize.
Additionally, floats can have weird values like ±∞ or NaN which this method needs to deal with.