Hacker News new | past | comments | ask | show | jobs | submit login

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?



The 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.


It's more than a small convenience. It's a big feature that we can use when designing traits and APIs.


Yes of course (I work on Rust). I mean that the difference between f32::NAN and std::f32::NAN is a small convenience.


It's just for an example - see https://doc.rust-lang.org/std/f32/.


This 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/nu...


That'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.


https://doc.rust-lang.org/std/f32/

I believe you can do std::f32::NAN




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: