Max_value for i32. What about f32?

We have MAX in both modules: std::i32 and std::f32 (among others). But it's different for primitives: i32 and f32.

i32 is provided with max_value method, while f32 is not. What is the reason behind that? Apart of that inf is "max value".

I mean: is there any important trick that developer should be aware of?

std::i32::MAX;
i32::max_value();

std::f32::MAX;
f32::max_value();  
//error: function or associated item not found in `f32`

It's available as a constant but not as a function. See std::f32::MAX

I think that was the question? Why is it not also available as a function but it is for integers?

Exactly.

I'm not sure why, but I assume it has something to do with the maximum value being much less useful for floats.

I'm guessing it's simply because the need for f32::max_value() isn't really great enough to warrant allocating resources to it. I doubt there's a technical reason why the method wouldn't exist.

If the need is not great enough why do we need it anyway? :slight_smile: On the other hand does putting it twice (as MAX and as max_value) make it more important?

Possible use case could be some bitwise operations of floats. But it's covered by std::f32::MAX anyway.

Maybe it's not that important. I'm just curious if there is something hidden behind.

I have a sneaking suspicion that num_trats::Bounded probably used to be part of std back in the pre-1.0 days, and that at the time it was removed/replaced with inherent methods, it was probably only implemented by integers and not floats.

(or rather, I know it used to be part of std, and I have the sneaking suspicion that it was not implemented by floats!)

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.