4 ms·
Not a Rust programmer, so the answer to this question may be in TFM -- Rust has floats, but it doesn't make available constants for inf and NaN? Are you not gu
by sevensor 9y ago
Not a Rust programmer, so the answer to this question may be in TFM -- Rust has floats, but it doesn't make available constants for inf and NaN? Are you not guaranteed IEEE754 floats?
const NAN: f32 = 0.0f32 / 0.0f32;
const INFINITY: f32 = 1.0f32 / 0.0f32;
Or is that just for the sake of example?
- Mouse47 9y agohttps://doc.rust-lang.org/std/f32/ https://doc.rust-lang.org/std/f32/ I believe you can do std::f32::NAN
- eddyb 9y agoIt's just for an example - see https://doc.rust-lang.org/std/f32/ https://doc.rust-lang.org/std/f32/.
- tatterdemalion 9y agoThe consts are currently defined in modules with the same name as the type, `std::f32::NAN;`. This feature allows them to be associated with the type itself - `f32::NAN`. A small convenience.
- kzrdude 9y agoIt's more than a small convenience. It's a big feature that we can use when designing traits and APIs.
- tatterdemalion 9y agoYes of course (I work on Rust). I mean that the difference between f32::NAN and std::f32::NAN is a small convenience.
- whyever 9y agoThis is how making the constants available could look like. Currently they are available via a module, and they are implemented like above: https://github.com/rust-lang/rust/blob/master/src/libcore/num/f32.rs https://github.com/rust-lang/rust/blob/master/src/libcore/nu...
- sevensor 9y agoThat's something I'd like to understand better -- MAX is defined as a literal number, but NAN and INFINITY are computed by the compiler. I looked at the header files on my system: GNU nan.h uses a GCC builtin and falls back on a magic bit-pattern (a NaN as defined by the IEEE754). Is 0.0/0.0 more portable? Clearer? Are magic bit-patterns just not in the spirit of the language? Sorry to be so particular about this -- floats are tricky and I'd like to know exactly what's going on with them.