| 1 | //! Constants for the `f32` single-precision floating point type. |
| 2 | //! |
| 3 | //! *[See also the `f32` primitive type][f32].* |
| 4 | //! |
| 5 | //! Mathematically significant numbers are provided in the `consts` sub-module. |
| 6 | //! |
| 7 | //! For the constants defined directly in this module |
| 8 | //! (as distinct from those defined in the `consts` sub-module), |
| 9 | //! new code should instead use the associated constants |
| 10 | //! defined directly on the `f32` type. |
| 11 | |
| 12 | #![stable (feature = "rust1" , since = "1.0.0" )] |
| 13 | |
| 14 | use crate::convert::FloatToInt; |
| 15 | use crate::num::FpCategory; |
| 16 | use crate::panic::const_assert; |
| 17 | use crate::{cfg_select, intrinsics, mem}; |
| 18 | |
| 19 | /// The radix or base of the internal representation of `f32`. |
| 20 | /// Use [`f32::RADIX`] instead. |
| 21 | /// |
| 22 | /// # Examples |
| 23 | /// |
| 24 | /// ```rust |
| 25 | /// // deprecated way |
| 26 | /// # #[allow (deprecated, deprecated_in_future)] |
| 27 | /// let r = std::f32::RADIX; |
| 28 | /// |
| 29 | /// // intended way |
| 30 | /// let r = f32::RADIX; |
| 31 | /// ``` |
| 32 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 33 | #[deprecated (since = "TBD" , note = "replaced by the `RADIX` associated constant on `f32`" )] |
| 34 | #[rustc_diagnostic_item = "f32_legacy_const_radix" ] |
| 35 | pub const RADIX: u32 = f32::RADIX; |
| 36 | |
| 37 | /// Number of significant digits in base 2. |
| 38 | /// Use [`f32::MANTISSA_DIGITS`] instead. |
| 39 | /// |
| 40 | /// # Examples |
| 41 | /// |
| 42 | /// ```rust |
| 43 | /// // deprecated way |
| 44 | /// # #[allow (deprecated, deprecated_in_future)] |
| 45 | /// let d = std::f32::MANTISSA_DIGITS; |
| 46 | /// |
| 47 | /// // intended way |
| 48 | /// let d = f32::MANTISSA_DIGITS; |
| 49 | /// ``` |
| 50 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 51 | #[deprecated ( |
| 52 | since = "TBD" , |
| 53 | note = "replaced by the `MANTISSA_DIGITS` associated constant on `f32`" |
| 54 | )] |
| 55 | #[rustc_diagnostic_item = "f32_legacy_const_mantissa_dig" ] |
| 56 | pub const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS; |
| 57 | |
| 58 | /// Approximate number of significant digits in base 10. |
| 59 | /// Use [`f32::DIGITS`] instead. |
| 60 | /// |
| 61 | /// # Examples |
| 62 | /// |
| 63 | /// ```rust |
| 64 | /// // deprecated way |
| 65 | /// # #[allow (deprecated, deprecated_in_future)] |
| 66 | /// let d = std::f32::DIGITS; |
| 67 | /// |
| 68 | /// // intended way |
| 69 | /// let d = f32::DIGITS; |
| 70 | /// ``` |
| 71 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 72 | #[deprecated (since = "TBD" , note = "replaced by the `DIGITS` associated constant on `f32`" )] |
| 73 | #[rustc_diagnostic_item = "f32_legacy_const_digits" ] |
| 74 | pub const DIGITS: u32 = f32::DIGITS; |
| 75 | |
| 76 | /// [Machine epsilon] value for `f32`. |
| 77 | /// Use [`f32::EPSILON`] instead. |
| 78 | /// |
| 79 | /// This is the difference between `1.0` and the next larger representable number. |
| 80 | /// |
| 81 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon |
| 82 | /// |
| 83 | /// # Examples |
| 84 | /// |
| 85 | /// ```rust |
| 86 | /// // deprecated way |
| 87 | /// # #[allow (deprecated, deprecated_in_future)] |
| 88 | /// let e = std::f32::EPSILON; |
| 89 | /// |
| 90 | /// // intended way |
| 91 | /// let e = f32::EPSILON; |
| 92 | /// ``` |
| 93 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 94 | #[deprecated (since = "TBD" , note = "replaced by the `EPSILON` associated constant on `f32`" )] |
| 95 | #[rustc_diagnostic_item = "f32_legacy_const_epsilon" ] |
| 96 | pub const EPSILON: f32 = f32::EPSILON; |
| 97 | |
| 98 | /// Smallest finite `f32` value. |
| 99 | /// Use [`f32::MIN`] instead. |
| 100 | /// |
| 101 | /// # Examples |
| 102 | /// |
| 103 | /// ```rust |
| 104 | /// // deprecated way |
| 105 | /// # #[allow (deprecated, deprecated_in_future)] |
| 106 | /// let min = std::f32::MIN; |
| 107 | /// |
| 108 | /// // intended way |
| 109 | /// let min = f32::MIN; |
| 110 | /// ``` |
| 111 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 112 | #[deprecated (since = "TBD" , note = "replaced by the `MIN` associated constant on `f32`" )] |
| 113 | #[rustc_diagnostic_item = "f32_legacy_const_min" ] |
| 114 | pub const MIN: f32 = f32::MIN; |
| 115 | |
| 116 | /// Smallest positive normal `f32` value. |
| 117 | /// Use [`f32::MIN_POSITIVE`] instead. |
| 118 | /// |
| 119 | /// # Examples |
| 120 | /// |
| 121 | /// ```rust |
| 122 | /// // deprecated way |
| 123 | /// # #[allow (deprecated, deprecated_in_future)] |
| 124 | /// let min = std::f32::MIN_POSITIVE; |
| 125 | /// |
| 126 | /// // intended way |
| 127 | /// let min = f32::MIN_POSITIVE; |
| 128 | /// ``` |
| 129 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 130 | #[deprecated (since = "TBD" , note = "replaced by the `MIN_POSITIVE` associated constant on `f32`" )] |
| 131 | #[rustc_diagnostic_item = "f32_legacy_const_min_positive" ] |
| 132 | pub const MIN_POSITIVE: f32 = f32::MIN_POSITIVE; |
| 133 | |
| 134 | /// Largest finite `f32` value. |
| 135 | /// Use [`f32::MAX`] instead. |
| 136 | /// |
| 137 | /// # Examples |
| 138 | /// |
| 139 | /// ```rust |
| 140 | /// // deprecated way |
| 141 | /// # #[allow (deprecated, deprecated_in_future)] |
| 142 | /// let max = std::f32::MAX; |
| 143 | /// |
| 144 | /// // intended way |
| 145 | /// let max = f32::MAX; |
| 146 | /// ``` |
| 147 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 148 | #[deprecated (since = "TBD" , note = "replaced by the `MAX` associated constant on `f32`" )] |
| 149 | #[rustc_diagnostic_item = "f32_legacy_const_max" ] |
| 150 | pub const MAX: f32 = f32::MAX; |
| 151 | |
| 152 | /// One greater than the minimum possible normal power of 2 exponent. |
| 153 | /// Use [`f32::MIN_EXP`] instead. |
| 154 | /// |
| 155 | /// # Examples |
| 156 | /// |
| 157 | /// ```rust |
| 158 | /// // deprecated way |
| 159 | /// # #[allow (deprecated, deprecated_in_future)] |
| 160 | /// let min = std::f32::MIN_EXP; |
| 161 | /// |
| 162 | /// // intended way |
| 163 | /// let min = f32::MIN_EXP; |
| 164 | /// ``` |
| 165 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 166 | #[deprecated (since = "TBD" , note = "replaced by the `MIN_EXP` associated constant on `f32`" )] |
| 167 | #[rustc_diagnostic_item = "f32_legacy_const_min_exp" ] |
| 168 | pub const MIN_EXP: i32 = f32::MIN_EXP; |
| 169 | |
| 170 | /// Maximum possible power of 2 exponent. |
| 171 | /// Use [`f32::MAX_EXP`] instead. |
| 172 | /// |
| 173 | /// # Examples |
| 174 | /// |
| 175 | /// ```rust |
| 176 | /// // deprecated way |
| 177 | /// # #[allow (deprecated, deprecated_in_future)] |
| 178 | /// let max = std::f32::MAX_EXP; |
| 179 | /// |
| 180 | /// // intended way |
| 181 | /// let max = f32::MAX_EXP; |
| 182 | /// ``` |
| 183 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 184 | #[deprecated (since = "TBD" , note = "replaced by the `MAX_EXP` associated constant on `f32`" )] |
| 185 | #[rustc_diagnostic_item = "f32_legacy_const_max_exp" ] |
| 186 | pub const MAX_EXP: i32 = f32::MAX_EXP; |
| 187 | |
| 188 | /// Minimum possible normal power of 10 exponent. |
| 189 | /// Use [`f32::MIN_10_EXP`] instead. |
| 190 | /// |
| 191 | /// # Examples |
| 192 | /// |
| 193 | /// ```rust |
| 194 | /// // deprecated way |
| 195 | /// # #[allow (deprecated, deprecated_in_future)] |
| 196 | /// let min = std::f32::MIN_10_EXP; |
| 197 | /// |
| 198 | /// // intended way |
| 199 | /// let min = f32::MIN_10_EXP; |
| 200 | /// ``` |
| 201 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 202 | #[deprecated (since = "TBD" , note = "replaced by the `MIN_10_EXP` associated constant on `f32`" )] |
| 203 | #[rustc_diagnostic_item = "f32_legacy_const_min_10_exp" ] |
| 204 | pub const MIN_10_EXP: i32 = f32::MIN_10_EXP; |
| 205 | |
| 206 | /// Maximum possible power of 10 exponent. |
| 207 | /// Use [`f32::MAX_10_EXP`] instead. |
| 208 | /// |
| 209 | /// # Examples |
| 210 | /// |
| 211 | /// ```rust |
| 212 | /// // deprecated way |
| 213 | /// # #[allow (deprecated, deprecated_in_future)] |
| 214 | /// let max = std::f32::MAX_10_EXP; |
| 215 | /// |
| 216 | /// // intended way |
| 217 | /// let max = f32::MAX_10_EXP; |
| 218 | /// ``` |
| 219 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 220 | #[deprecated (since = "TBD" , note = "replaced by the `MAX_10_EXP` associated constant on `f32`" )] |
| 221 | #[rustc_diagnostic_item = "f32_legacy_const_max_10_exp" ] |
| 222 | pub const MAX_10_EXP: i32 = f32::MAX_10_EXP; |
| 223 | |
| 224 | /// Not a Number (NaN). |
| 225 | /// Use [`f32::NAN`] instead. |
| 226 | /// |
| 227 | /// # Examples |
| 228 | /// |
| 229 | /// ```rust |
| 230 | /// // deprecated way |
| 231 | /// # #[allow (deprecated, deprecated_in_future)] |
| 232 | /// let nan = std::f32::NAN; |
| 233 | /// |
| 234 | /// // intended way |
| 235 | /// let nan = f32::NAN; |
| 236 | /// ``` |
| 237 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 238 | #[deprecated (since = "TBD" , note = "replaced by the `NAN` associated constant on `f32`" )] |
| 239 | #[rustc_diagnostic_item = "f32_legacy_const_nan" ] |
| 240 | pub const NAN: f32 = f32::NAN; |
| 241 | |
| 242 | /// Infinity (∞). |
| 243 | /// Use [`f32::INFINITY`] instead. |
| 244 | /// |
| 245 | /// # Examples |
| 246 | /// |
| 247 | /// ```rust |
| 248 | /// // deprecated way |
| 249 | /// # #[allow (deprecated, deprecated_in_future)] |
| 250 | /// let inf = std::f32::INFINITY; |
| 251 | /// |
| 252 | /// // intended way |
| 253 | /// let inf = f32::INFINITY; |
| 254 | /// ``` |
| 255 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 256 | #[deprecated (since = "TBD" , note = "replaced by the `INFINITY` associated constant on `f32`" )] |
| 257 | #[rustc_diagnostic_item = "f32_legacy_const_infinity" ] |
| 258 | pub const INFINITY: f32 = f32::INFINITY; |
| 259 | |
| 260 | /// Negative infinity (−∞). |
| 261 | /// Use [`f32::NEG_INFINITY`] instead. |
| 262 | /// |
| 263 | /// # Examples |
| 264 | /// |
| 265 | /// ```rust |
| 266 | /// // deprecated way |
| 267 | /// # #[allow (deprecated, deprecated_in_future)] |
| 268 | /// let ninf = std::f32::NEG_INFINITY; |
| 269 | /// |
| 270 | /// // intended way |
| 271 | /// let ninf = f32::NEG_INFINITY; |
| 272 | /// ``` |
| 273 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 274 | #[deprecated (since = "TBD" , note = "replaced by the `NEG_INFINITY` associated constant on `f32`" )] |
| 275 | #[rustc_diagnostic_item = "f32_legacy_const_neg_infinity" ] |
| 276 | pub const NEG_INFINITY: f32 = f32::NEG_INFINITY; |
| 277 | |
| 278 | /// Basic mathematical constants. |
| 279 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 280 | pub mod consts { |
| 281 | // FIXME: replace with mathematical constants from cmath. |
| 282 | |
| 283 | /// Archimedes' constant (π) |
| 284 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 285 | pub const PI: f32 = 3.14159265358979323846264338327950288_f32; |
| 286 | |
| 287 | /// The full circle constant (τ) |
| 288 | /// |
| 289 | /// Equal to 2π. |
| 290 | #[stable (feature = "tau_constant" , since = "1.47.0" )] |
| 291 | pub const TAU: f32 = 6.28318530717958647692528676655900577_f32; |
| 292 | |
| 293 | /// The golden ratio (φ) |
| 294 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
| 295 | pub const PHI: f32 = 1.618033988749894848204586834365638118_f32; |
| 296 | |
| 297 | /// The Euler-Mascheroni constant (γ) |
| 298 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
| 299 | pub const EGAMMA: f32 = 0.577215664901532860606512090082402431_f32; |
| 300 | |
| 301 | /// π/2 |
| 302 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 303 | pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32; |
| 304 | |
| 305 | /// π/3 |
| 306 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 307 | pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32; |
| 308 | |
| 309 | /// π/4 |
| 310 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 311 | pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32; |
| 312 | |
| 313 | /// π/6 |
| 314 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 315 | pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32; |
| 316 | |
| 317 | /// π/8 |
| 318 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 319 | pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32; |
| 320 | |
| 321 | /// 1/π |
| 322 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 323 | pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32; |
| 324 | |
| 325 | /// 1/sqrt(π) |
| 326 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
| 327 | pub const FRAC_1_SQRT_PI: f32 = 0.564189583547756286948079451560772586_f32; |
| 328 | |
| 329 | /// 1/sqrt(2π) |
| 330 | #[doc (alias = "FRAC_1_SQRT_TAU" )] |
| 331 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
| 332 | pub const FRAC_1_SQRT_2PI: f32 = 0.398942280401432677939946059934381868_f32; |
| 333 | |
| 334 | /// 2/π |
| 335 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 336 | pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32; |
| 337 | |
| 338 | /// 2/sqrt(π) |
| 339 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 340 | pub const FRAC_2_SQRT_PI: f32 = 1.12837916709551257389615890312154517_f32; |
| 341 | |
| 342 | /// sqrt(2) |
| 343 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 344 | pub const SQRT_2: f32 = 1.41421356237309504880168872420969808_f32; |
| 345 | |
| 346 | /// 1/sqrt(2) |
| 347 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 348 | pub const FRAC_1_SQRT_2: f32 = 0.707106781186547524400844362104849039_f32; |
| 349 | |
| 350 | /// sqrt(3) |
| 351 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
| 352 | pub const SQRT_3: f32 = 1.732050807568877293527446341505872367_f32; |
| 353 | |
| 354 | /// 1/sqrt(3) |
| 355 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
| 356 | pub const FRAC_1_SQRT_3: f32 = 0.577350269189625764509148780501957456_f32; |
| 357 | |
| 358 | /// Euler's number (e) |
| 359 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 360 | pub const E: f32 = 2.71828182845904523536028747135266250_f32; |
| 361 | |
| 362 | /// log<sub>2</sub>(e) |
| 363 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 364 | pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32; |
| 365 | |
| 366 | /// log<sub>2</sub>(10) |
| 367 | #[stable (feature = "extra_log_consts" , since = "1.43.0" )] |
| 368 | pub const LOG2_10: f32 = 3.32192809488736234787031942948939018_f32; |
| 369 | |
| 370 | /// log<sub>10</sub>(e) |
| 371 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 372 | pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32; |
| 373 | |
| 374 | /// log<sub>10</sub>(2) |
| 375 | #[stable (feature = "extra_log_consts" , since = "1.43.0" )] |
| 376 | pub const LOG10_2: f32 = 0.301029995663981195213738894724493027_f32; |
| 377 | |
| 378 | /// ln(2) |
| 379 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 380 | pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32; |
| 381 | |
| 382 | /// ln(10) |
| 383 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 384 | pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32; |
| 385 | } |
| 386 | |
| 387 | impl f32 { |
| 388 | /// The radix or base of the internal representation of `f32`. |
| 389 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 390 | pub const RADIX: u32 = 2; |
| 391 | |
| 392 | /// Number of significant digits in base 2. |
| 393 | /// |
| 394 | /// Note that the size of the mantissa in the bitwise representation is one |
| 395 | /// smaller than this since the leading 1 is not stored explicitly. |
| 396 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 397 | pub const MANTISSA_DIGITS: u32 = 24; |
| 398 | |
| 399 | /// Approximate number of significant digits in base 10. |
| 400 | /// |
| 401 | /// This is the maximum <i>x</i> such that any decimal number with <i>x</i> |
| 402 | /// significant digits can be converted to `f32` and back without loss. |
| 403 | /// |
| 404 | /// Equal to floor(log<sub>10</sub> 2<sup>[`MANTISSA_DIGITS`] − 1</sup>). |
| 405 | /// |
| 406 | /// [`MANTISSA_DIGITS`]: f32::MANTISSA_DIGITS |
| 407 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 408 | pub const DIGITS: u32 = 6; |
| 409 | |
| 410 | /// [Machine epsilon] value for `f32`. |
| 411 | /// |
| 412 | /// This is the difference between `1.0` and the next larger representable number. |
| 413 | /// |
| 414 | /// Equal to 2<sup>1 − [`MANTISSA_DIGITS`]</sup>. |
| 415 | /// |
| 416 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon |
| 417 | /// [`MANTISSA_DIGITS`]: f32::MANTISSA_DIGITS |
| 418 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 419 | #[rustc_diagnostic_item = "f32_epsilon" ] |
| 420 | pub const EPSILON: f32 = 1.19209290e-07_f32; |
| 421 | |
| 422 | /// Smallest finite `f32` value. |
| 423 | /// |
| 424 | /// Equal to −[`MAX`]. |
| 425 | /// |
| 426 | /// [`MAX`]: f32::MAX |
| 427 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 428 | pub const MIN: f32 = -3.40282347e+38_f32; |
| 429 | /// Smallest positive normal `f32` value. |
| 430 | /// |
| 431 | /// Equal to 2<sup>[`MIN_EXP`] − 1</sup>. |
| 432 | /// |
| 433 | /// [`MIN_EXP`]: f32::MIN_EXP |
| 434 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 435 | pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32; |
| 436 | /// Largest finite `f32` value. |
| 437 | /// |
| 438 | /// Equal to |
| 439 | /// (1 − 2<sup>−[`MANTISSA_DIGITS`]</sup>) 2<sup>[`MAX_EXP`]</sup>. |
| 440 | /// |
| 441 | /// [`MANTISSA_DIGITS`]: f32::MANTISSA_DIGITS |
| 442 | /// [`MAX_EXP`]: f32::MAX_EXP |
| 443 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 444 | pub const MAX: f32 = 3.40282347e+38_f32; |
| 445 | |
| 446 | /// One greater than the minimum possible *normal* power of 2 exponent |
| 447 | /// for a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition). |
| 448 | /// |
| 449 | /// This corresponds to the exact minimum possible *normal* power of 2 exponent |
| 450 | /// for a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition). |
| 451 | /// In other words, all normal numbers representable by this type are |
| 452 | /// greater than or equal to 0.5 × 2<sup><i>MIN_EXP</i></sup>. |
| 453 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 454 | pub const MIN_EXP: i32 = -125; |
| 455 | /// One greater than the maximum possible power of 2 exponent |
| 456 | /// for a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition). |
| 457 | /// |
| 458 | /// This corresponds to the exact maximum possible power of 2 exponent |
| 459 | /// for a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition). |
| 460 | /// In other words, all numbers representable by this type are |
| 461 | /// strictly less than 2<sup><i>MAX_EXP</i></sup>. |
| 462 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 463 | pub const MAX_EXP: i32 = 128; |
| 464 | |
| 465 | /// Minimum <i>x</i> for which 10<sup><i>x</i></sup> is normal. |
| 466 | /// |
| 467 | /// Equal to ceil(log<sub>10</sub> [`MIN_POSITIVE`]). |
| 468 | /// |
| 469 | /// [`MIN_POSITIVE`]: f32::MIN_POSITIVE |
| 470 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 471 | pub const MIN_10_EXP: i32 = -37; |
| 472 | /// Maximum <i>x</i> for which 10<sup><i>x</i></sup> is normal. |
| 473 | /// |
| 474 | /// Equal to floor(log<sub>10</sub> [`MAX`]). |
| 475 | /// |
| 476 | /// [`MAX`]: f32::MAX |
| 477 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 478 | pub const MAX_10_EXP: i32 = 38; |
| 479 | |
| 480 | /// Not a Number (NaN). |
| 481 | /// |
| 482 | /// Note that IEEE 754 doesn't define just a single NaN value; a plethora of bit patterns are |
| 483 | /// considered to be NaN. Furthermore, the standard makes a difference between a "signaling" and |
| 484 | /// a "quiet" NaN, and allows inspecting its "payload" (the unspecified bits in the bit pattern) |
| 485 | /// and its sign. See the [specification of NaN bit patterns](f32#nan-bit-patterns) for more |
| 486 | /// info. |
| 487 | /// |
| 488 | /// This constant is guaranteed to be a quiet NaN (on targets that follow the Rust assumptions |
| 489 | /// that the quiet/signaling bit being set to 1 indicates a quiet NaN). Beyond that, nothing is |
| 490 | /// guaranteed about the specific bit pattern chosen here: both payload and sign are arbitrary. |
| 491 | /// The concrete bit pattern may change across Rust versions and target platforms. |
| 492 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 493 | #[rustc_diagnostic_item = "f32_nan" ] |
| 494 | #[allow (clippy::eq_op)] |
| 495 | pub const NAN: f32 = 0.0_f32 / 0.0_f32; |
| 496 | /// Infinity (∞). |
| 497 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 498 | pub const INFINITY: f32 = 1.0_f32 / 0.0_f32; |
| 499 | /// Negative infinity (−∞). |
| 500 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
| 501 | pub const NEG_INFINITY: f32 = -1.0_f32 / 0.0_f32; |
| 502 | |
| 503 | /// Sign bit |
| 504 | pub(crate) const SIGN_MASK: u32 = 0x8000_0000; |
| 505 | |
| 506 | /// Exponent mask |
| 507 | pub(crate) const EXP_MASK: u32 = 0x7f80_0000; |
| 508 | |
| 509 | /// Mantissa mask |
| 510 | pub(crate) const MAN_MASK: u32 = 0x007f_ffff; |
| 511 | |
| 512 | /// Minimum representable positive value (min subnormal) |
| 513 | const TINY_BITS: u32 = 0x1; |
| 514 | |
| 515 | /// Minimum representable negative value (min negative subnormal) |
| 516 | const NEG_TINY_BITS: u32 = Self::TINY_BITS | Self::SIGN_MASK; |
| 517 | |
| 518 | /// Returns `true` if this value is NaN. |
| 519 | /// |
| 520 | /// ``` |
| 521 | /// let nan = f32::NAN; |
| 522 | /// let f = 7.0_f32; |
| 523 | /// |
| 524 | /// assert!(nan.is_nan()); |
| 525 | /// assert!(!f.is_nan()); |
| 526 | /// ``` |
| 527 | #[must_use ] |
| 528 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 529 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 530 | #[inline ] |
| 531 | #[allow (clippy::eq_op)] // > if you intended to check if the operand is NaN, use `.is_nan()` instead :) |
| 532 | pub const fn is_nan(self) -> bool { |
| 533 | self != self |
| 534 | } |
| 535 | |
| 536 | /// Returns `true` if this value is positive infinity or negative infinity, and |
| 537 | /// `false` otherwise. |
| 538 | /// |
| 539 | /// ``` |
| 540 | /// let f = 7.0f32; |
| 541 | /// let inf = f32::INFINITY; |
| 542 | /// let neg_inf = f32::NEG_INFINITY; |
| 543 | /// let nan = f32::NAN; |
| 544 | /// |
| 545 | /// assert!(!f.is_infinite()); |
| 546 | /// assert!(!nan.is_infinite()); |
| 547 | /// |
| 548 | /// assert!(inf.is_infinite()); |
| 549 | /// assert!(neg_inf.is_infinite()); |
| 550 | /// ``` |
| 551 | #[must_use ] |
| 552 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 553 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 554 | #[inline ] |
| 555 | pub const fn is_infinite(self) -> bool { |
| 556 | // Getting clever with transmutation can result in incorrect answers on some FPUs |
| 557 | // FIXME: alter the Rust <-> Rust calling convention to prevent this problem. |
| 558 | // See https://github.com/rust-lang/rust/issues/72327 |
| 559 | (self == f32::INFINITY) | (self == f32::NEG_INFINITY) |
| 560 | } |
| 561 | |
| 562 | /// Returns `true` if this number is neither infinite nor NaN. |
| 563 | /// |
| 564 | /// ``` |
| 565 | /// let f = 7.0f32; |
| 566 | /// let inf = f32::INFINITY; |
| 567 | /// let neg_inf = f32::NEG_INFINITY; |
| 568 | /// let nan = f32::NAN; |
| 569 | /// |
| 570 | /// assert!(f.is_finite()); |
| 571 | /// |
| 572 | /// assert!(!nan.is_finite()); |
| 573 | /// assert!(!inf.is_finite()); |
| 574 | /// assert!(!neg_inf.is_finite()); |
| 575 | /// ``` |
| 576 | #[must_use ] |
| 577 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 578 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 579 | #[inline ] |
| 580 | pub const fn is_finite(self) -> bool { |
| 581 | // There's no need to handle NaN separately: if self is NaN, |
| 582 | // the comparison is not true, exactly as desired. |
| 583 | self.abs() < Self::INFINITY |
| 584 | } |
| 585 | |
| 586 | /// Returns `true` if the number is [subnormal]. |
| 587 | /// |
| 588 | /// ``` |
| 589 | /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32 |
| 590 | /// let max = f32::MAX; |
| 591 | /// let lower_than_min = 1.0e-40_f32; |
| 592 | /// let zero = 0.0_f32; |
| 593 | /// |
| 594 | /// assert!(!min.is_subnormal()); |
| 595 | /// assert!(!max.is_subnormal()); |
| 596 | /// |
| 597 | /// assert!(!zero.is_subnormal()); |
| 598 | /// assert!(!f32::NAN.is_subnormal()); |
| 599 | /// assert!(!f32::INFINITY.is_subnormal()); |
| 600 | /// // Values between `0` and `min` are Subnormal. |
| 601 | /// assert!(lower_than_min.is_subnormal()); |
| 602 | /// ``` |
| 603 | /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number |
| 604 | #[must_use ] |
| 605 | #[stable (feature = "is_subnormal" , since = "1.53.0" )] |
| 606 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 607 | #[inline ] |
| 608 | pub const fn is_subnormal(self) -> bool { |
| 609 | matches!(self.classify(), FpCategory::Subnormal) |
| 610 | } |
| 611 | |
| 612 | /// Returns `true` if the number is neither zero, infinite, |
| 613 | /// [subnormal], or NaN. |
| 614 | /// |
| 615 | /// ``` |
| 616 | /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32 |
| 617 | /// let max = f32::MAX; |
| 618 | /// let lower_than_min = 1.0e-40_f32; |
| 619 | /// let zero = 0.0_f32; |
| 620 | /// |
| 621 | /// assert!(min.is_normal()); |
| 622 | /// assert!(max.is_normal()); |
| 623 | /// |
| 624 | /// assert!(!zero.is_normal()); |
| 625 | /// assert!(!f32::NAN.is_normal()); |
| 626 | /// assert!(!f32::INFINITY.is_normal()); |
| 627 | /// // Values between `0` and `min` are Subnormal. |
| 628 | /// assert!(!lower_than_min.is_normal()); |
| 629 | /// ``` |
| 630 | /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number |
| 631 | #[must_use ] |
| 632 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 633 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 634 | #[inline ] |
| 635 | pub const fn is_normal(self) -> bool { |
| 636 | matches!(self.classify(), FpCategory::Normal) |
| 637 | } |
| 638 | |
| 639 | /// Returns the floating point category of the number. If only one property |
| 640 | /// is going to be tested, it is generally faster to use the specific |
| 641 | /// predicate instead. |
| 642 | /// |
| 643 | /// ``` |
| 644 | /// use std::num::FpCategory; |
| 645 | /// |
| 646 | /// let num = 12.4_f32; |
| 647 | /// let inf = f32::INFINITY; |
| 648 | /// |
| 649 | /// assert_eq!(num.classify(), FpCategory::Normal); |
| 650 | /// assert_eq!(inf.classify(), FpCategory::Infinite); |
| 651 | /// ``` |
| 652 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 653 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 654 | pub const fn classify(self) -> FpCategory { |
| 655 | // We used to have complicated logic here that avoids the simple bit-based tests to work |
| 656 | // around buggy codegen for x87 targets (see |
| 657 | // https://github.com/rust-lang/rust/issues/114479). However, some LLVM versions later, none |
| 658 | // of our tests is able to find any difference between the complicated and the naive |
| 659 | // version, so now we are back to the naive version. |
| 660 | let b = self.to_bits(); |
| 661 | match (b & Self::MAN_MASK, b & Self::EXP_MASK) { |
| 662 | (0, Self::EXP_MASK) => FpCategory::Infinite, |
| 663 | (_, Self::EXP_MASK) => FpCategory::Nan, |
| 664 | (0, 0) => FpCategory::Zero, |
| 665 | (_, 0) => FpCategory::Subnormal, |
| 666 | _ => FpCategory::Normal, |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /// Returns `true` if `self` has a positive sign, including `+0.0`, NaNs with |
| 671 | /// positive sign bit and positive infinity. |
| 672 | /// |
| 673 | /// Note that IEEE 754 doesn't assign any meaning to the sign bit in case of |
| 674 | /// a NaN, and as Rust doesn't guarantee that the bit pattern of NaNs are |
| 675 | /// conserved over arithmetic operations, the result of `is_sign_positive` on |
| 676 | /// a NaN might produce an unexpected or non-portable result. See the [specification |
| 677 | /// of NaN bit patterns](f32#nan-bit-patterns) for more info. Use `self.signum() == 1.0` |
| 678 | /// if you need fully portable behavior (will return `false` for all NaNs). |
| 679 | /// |
| 680 | /// ``` |
| 681 | /// let f = 7.0_f32; |
| 682 | /// let g = -7.0_f32; |
| 683 | /// |
| 684 | /// assert!(f.is_sign_positive()); |
| 685 | /// assert!(!g.is_sign_positive()); |
| 686 | /// ``` |
| 687 | #[must_use ] |
| 688 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 689 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 690 | #[inline ] |
| 691 | pub const fn is_sign_positive(self) -> bool { |
| 692 | !self.is_sign_negative() |
| 693 | } |
| 694 | |
| 695 | /// Returns `true` if `self` has a negative sign, including `-0.0`, NaNs with |
| 696 | /// negative sign bit and negative infinity. |
| 697 | /// |
| 698 | /// Note that IEEE 754 doesn't assign any meaning to the sign bit in case of |
| 699 | /// a NaN, and as Rust doesn't guarantee that the bit pattern of NaNs are |
| 700 | /// conserved over arithmetic operations, the result of `is_sign_negative` on |
| 701 | /// a NaN might produce an unexpected or non-portable result. See the [specification |
| 702 | /// of NaN bit patterns](f32#nan-bit-patterns) for more info. Use `self.signum() == -1.0` |
| 703 | /// if you need fully portable behavior (will return `false` for all NaNs). |
| 704 | /// |
| 705 | /// ``` |
| 706 | /// let f = 7.0f32; |
| 707 | /// let g = -7.0f32; |
| 708 | /// |
| 709 | /// assert!(!f.is_sign_negative()); |
| 710 | /// assert!(g.is_sign_negative()); |
| 711 | /// ``` |
| 712 | #[must_use ] |
| 713 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 714 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
| 715 | #[inline ] |
| 716 | pub const fn is_sign_negative(self) -> bool { |
| 717 | // IEEE754 says: isSignMinus(x) is true if and only if x has negative sign. isSignMinus |
| 718 | // applies to zeros and NaNs as well. |
| 719 | self.to_bits() & 0x8000_0000 != 0 |
| 720 | } |
| 721 | |
| 722 | /// Returns the least number greater than `self`. |
| 723 | /// |
| 724 | /// Let `TINY` be the smallest representable positive `f32`. Then, |
| 725 | /// - if `self.is_nan()`, this returns `self`; |
| 726 | /// - if `self` is [`NEG_INFINITY`], this returns [`MIN`]; |
| 727 | /// - if `self` is `-TINY`, this returns -0.0; |
| 728 | /// - if `self` is -0.0 or +0.0, this returns `TINY`; |
| 729 | /// - if `self` is [`MAX`] or [`INFINITY`], this returns [`INFINITY`]; |
| 730 | /// - otherwise the unique least value greater than `self` is returned. |
| 731 | /// |
| 732 | /// The identity `x.next_up() == -(-x).next_down()` holds for all non-NaN `x`. When `x` |
| 733 | /// is finite `x == x.next_up().next_down()` also holds. |
| 734 | /// |
| 735 | /// ```rust |
| 736 | /// // f32::EPSILON is the difference between 1.0 and the next number up. |
| 737 | /// assert_eq!(1.0f32.next_up(), 1.0 + f32::EPSILON); |
| 738 | /// // But not for most numbers. |
| 739 | /// assert!(0.1f32.next_up() < 0.1 + f32::EPSILON); |
| 740 | /// assert_eq!(16777216f32.next_up(), 16777218.0); |
| 741 | /// ``` |
| 742 | /// |
| 743 | /// This operation corresponds to IEEE-754 `nextUp`. |
| 744 | /// |
| 745 | /// [`NEG_INFINITY`]: Self::NEG_INFINITY |
| 746 | /// [`INFINITY`]: Self::INFINITY |
| 747 | /// [`MIN`]: Self::MIN |
| 748 | /// [`MAX`]: Self::MAX |
| 749 | #[inline ] |
| 750 | #[doc (alias = "nextUp" )] |
| 751 | #[stable (feature = "float_next_up_down" , since = "1.86.0" )] |
| 752 | #[rustc_const_stable (feature = "float_next_up_down" , since = "1.86.0" )] |
| 753 | pub const fn next_up(self) -> Self { |
| 754 | // Some targets violate Rust's assumption of IEEE semantics, e.g. by flushing |
| 755 | // denormals to zero. This is in general unsound and unsupported, but here |
| 756 | // we do our best to still produce the correct result on such targets. |
| 757 | let bits = self.to_bits(); |
| 758 | if self.is_nan() || bits == Self::INFINITY.to_bits() { |
| 759 | return self; |
| 760 | } |
| 761 | |
| 762 | let abs = bits & !Self::SIGN_MASK; |
| 763 | let next_bits = if abs == 0 { |
| 764 | Self::TINY_BITS |
| 765 | } else if bits == abs { |
| 766 | bits + 1 |
| 767 | } else { |
| 768 | bits - 1 |
| 769 | }; |
| 770 | Self::from_bits(next_bits) |
| 771 | } |
| 772 | |
| 773 | /// Returns the greatest number less than `self`. |
| 774 | /// |
| 775 | /// Let `TINY` be the smallest representable positive `f32`. Then, |
| 776 | /// - if `self.is_nan()`, this returns `self`; |
| 777 | /// - if `self` is [`INFINITY`], this returns [`MAX`]; |
| 778 | /// - if `self` is `TINY`, this returns 0.0; |
| 779 | /// - if `self` is -0.0 or +0.0, this returns `-TINY`; |
| 780 | /// - if `self` is [`MIN`] or [`NEG_INFINITY`], this returns [`NEG_INFINITY`]; |
| 781 | /// - otherwise the unique greatest value less than `self` is returned. |
| 782 | /// |
| 783 | /// The identity `x.next_down() == -(-x).next_up()` holds for all non-NaN `x`. When `x` |
| 784 | /// is finite `x == x.next_down().next_up()` also holds. |
| 785 | /// |
| 786 | /// ```rust |
| 787 | /// let x = 1.0f32; |
| 788 | /// // Clamp value into range [0, 1). |
| 789 | /// let clamped = x.clamp(0.0, 1.0f32.next_down()); |
| 790 | /// assert!(clamped < 1.0); |
| 791 | /// assert_eq!(clamped.next_up(), 1.0); |
| 792 | /// ``` |
| 793 | /// |
| 794 | /// This operation corresponds to IEEE-754 `nextDown`. |
| 795 | /// |
| 796 | /// [`NEG_INFINITY`]: Self::NEG_INFINITY |
| 797 | /// [`INFINITY`]: Self::INFINITY |
| 798 | /// [`MIN`]: Self::MIN |
| 799 | /// [`MAX`]: Self::MAX |
| 800 | #[inline ] |
| 801 | #[doc (alias = "nextDown" )] |
| 802 | #[stable (feature = "float_next_up_down" , since = "1.86.0" )] |
| 803 | #[rustc_const_stable (feature = "float_next_up_down" , since = "1.86.0" )] |
| 804 | pub const fn next_down(self) -> Self { |
| 805 | // Some targets violate Rust's assumption of IEEE semantics, e.g. by flushing |
| 806 | // denormals to zero. This is in general unsound and unsupported, but here |
| 807 | // we do our best to still produce the correct result on such targets. |
| 808 | let bits = self.to_bits(); |
| 809 | if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() { |
| 810 | return self; |
| 811 | } |
| 812 | |
| 813 | let abs = bits & !Self::SIGN_MASK; |
| 814 | let next_bits = if abs == 0 { |
| 815 | Self::NEG_TINY_BITS |
| 816 | } else if bits == abs { |
| 817 | bits - 1 |
| 818 | } else { |
| 819 | bits + 1 |
| 820 | }; |
| 821 | Self::from_bits(next_bits) |
| 822 | } |
| 823 | |
| 824 | /// Takes the reciprocal (inverse) of a number, `1/x`. |
| 825 | /// |
| 826 | /// ``` |
| 827 | /// let x = 2.0_f32; |
| 828 | /// let abs_difference = (x.recip() - (1.0 / x)).abs(); |
| 829 | /// |
| 830 | /// assert!(abs_difference <= f32::EPSILON); |
| 831 | /// ``` |
| 832 | #[must_use = "this returns the result of the operation, without modifying the original" ] |
| 833 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 834 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 835 | #[inline ] |
| 836 | pub const fn recip(self) -> f32 { |
| 837 | 1.0 / self |
| 838 | } |
| 839 | |
| 840 | /// Converts radians to degrees. |
| 841 | /// |
| 842 | /// ``` |
| 843 | /// let angle = std::f32::consts::PI; |
| 844 | /// |
| 845 | /// let abs_difference = (angle.to_degrees() - 180.0).abs(); |
| 846 | /// # #[cfg (any(not(target_arch = "x86" ), target_feature = "sse2" ))] |
| 847 | /// assert!(abs_difference <= f32::EPSILON); |
| 848 | /// ``` |
| 849 | #[must_use = "this returns the result of the operation, \ |
| 850 | without modifying the original" ] |
| 851 | #[stable (feature = "f32_deg_rad_conversions" , since = "1.7.0" )] |
| 852 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 853 | #[inline ] |
| 854 | pub const fn to_degrees(self) -> f32 { |
| 855 | // Use a constant for better precision. |
| 856 | const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32; |
| 857 | self * PIS_IN_180 |
| 858 | } |
| 859 | |
| 860 | /// Converts degrees to radians. |
| 861 | /// |
| 862 | /// ``` |
| 863 | /// let angle = 180.0f32; |
| 864 | /// |
| 865 | /// let abs_difference = (angle.to_radians() - std::f32::consts::PI).abs(); |
| 866 | /// |
| 867 | /// assert!(abs_difference <= f32::EPSILON); |
| 868 | /// ``` |
| 869 | #[must_use = "this returns the result of the operation, \ |
| 870 | without modifying the original" ] |
| 871 | #[stable (feature = "f32_deg_rad_conversions" , since = "1.7.0" )] |
| 872 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 873 | #[inline ] |
| 874 | pub const fn to_radians(self) -> f32 { |
| 875 | const RADS_PER_DEG: f32 = consts::PI / 180.0; |
| 876 | self * RADS_PER_DEG |
| 877 | } |
| 878 | |
| 879 | /// Returns the maximum of the two numbers, ignoring NaN. |
| 880 | /// |
| 881 | /// If one of the arguments is NaN, then the other argument is returned. |
| 882 | /// This follows the IEEE 754-2008 semantics for maxNum, except for handling of signaling NaNs; |
| 883 | /// this function handles all NaNs the same way and avoids maxNum's problems with associativity. |
| 884 | /// This also matches the behavior of libm’s fmax. In particular, if the inputs compare equal |
| 885 | /// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically. |
| 886 | /// |
| 887 | /// ``` |
| 888 | /// let x = 1.0f32; |
| 889 | /// let y = 2.0f32; |
| 890 | /// |
| 891 | /// assert_eq!(x.max(y), y); |
| 892 | /// ``` |
| 893 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
| 894 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 895 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 896 | #[inline ] |
| 897 | pub const fn max(self, other: f32) -> f32 { |
| 898 | intrinsics::maxnumf32(self, other) |
| 899 | } |
| 900 | |
| 901 | /// Returns the minimum of the two numbers, ignoring NaN. |
| 902 | /// |
| 903 | /// If one of the arguments is NaN, then the other argument is returned. |
| 904 | /// This follows the IEEE 754-2008 semantics for minNum, except for handling of signaling NaNs; |
| 905 | /// this function handles all NaNs the same way and avoids minNum's problems with associativity. |
| 906 | /// This also matches the behavior of libm’s fmin. In particular, if the inputs compare equal |
| 907 | /// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically. |
| 908 | /// |
| 909 | /// ``` |
| 910 | /// let x = 1.0f32; |
| 911 | /// let y = 2.0f32; |
| 912 | /// |
| 913 | /// assert_eq!(x.min(y), x); |
| 914 | /// ``` |
| 915 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
| 916 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 917 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 918 | #[inline ] |
| 919 | pub const fn min(self, other: f32) -> f32 { |
| 920 | intrinsics::minnumf32(self, other) |
| 921 | } |
| 922 | |
| 923 | /// Returns the maximum of the two numbers, propagating NaN. |
| 924 | /// |
| 925 | /// This returns NaN when *either* argument is NaN, as opposed to |
| 926 | /// [`f32::max`] which only returns NaN when *both* arguments are NaN. |
| 927 | /// |
| 928 | /// ``` |
| 929 | /// #![feature(float_minimum_maximum)] |
| 930 | /// let x = 1.0f32; |
| 931 | /// let y = 2.0f32; |
| 932 | /// |
| 933 | /// assert_eq!(x.maximum(y), y); |
| 934 | /// assert!(x.maximum(f32::NAN).is_nan()); |
| 935 | /// ``` |
| 936 | /// |
| 937 | /// If one of the arguments is NaN, then NaN is returned. Otherwise this returns the greater |
| 938 | /// of the two numbers. For this operation, -0.0 is considered to be less than +0.0. |
| 939 | /// Note that this follows the semantics specified in IEEE 754-2019. |
| 940 | /// |
| 941 | /// Also note that "propagation" of NaNs here doesn't necessarily mean that the bitpattern of a NaN |
| 942 | /// operand is conserved; see the [specification of NaN bit patterns](f32#nan-bit-patterns) for more info. |
| 943 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
| 944 | #[unstable (feature = "float_minimum_maximum" , issue = "91079" )] |
| 945 | #[inline ] |
| 946 | pub const fn maximum(self, other: f32) -> f32 { |
| 947 | intrinsics::maximumf32(self, other) |
| 948 | } |
| 949 | |
| 950 | /// Returns the minimum of the two numbers, propagating NaN. |
| 951 | /// |
| 952 | /// This returns NaN when *either* argument is NaN, as opposed to |
| 953 | /// [`f32::min`] which only returns NaN when *both* arguments are NaN. |
| 954 | /// |
| 955 | /// ``` |
| 956 | /// #![feature(float_minimum_maximum)] |
| 957 | /// let x = 1.0f32; |
| 958 | /// let y = 2.0f32; |
| 959 | /// |
| 960 | /// assert_eq!(x.minimum(y), x); |
| 961 | /// assert!(x.minimum(f32::NAN).is_nan()); |
| 962 | /// ``` |
| 963 | /// |
| 964 | /// If one of the arguments is NaN, then NaN is returned. Otherwise this returns the lesser |
| 965 | /// of the two numbers. For this operation, -0.0 is considered to be less than +0.0. |
| 966 | /// Note that this follows the semantics specified in IEEE 754-2019. |
| 967 | /// |
| 968 | /// Also note that "propagation" of NaNs here doesn't necessarily mean that the bitpattern of a NaN |
| 969 | /// operand is conserved; see the [specification of NaN bit patterns](f32#nan-bit-patterns) for more info. |
| 970 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
| 971 | #[unstable (feature = "float_minimum_maximum" , issue = "91079" )] |
| 972 | #[inline ] |
| 973 | pub const fn minimum(self, other: f32) -> f32 { |
| 974 | intrinsics::minimumf32(self, other) |
| 975 | } |
| 976 | |
| 977 | /// Calculates the midpoint (average) between `self` and `rhs`. |
| 978 | /// |
| 979 | /// This returns NaN when *either* argument is NaN or if a combination of |
| 980 | /// +inf and -inf is provided as arguments. |
| 981 | /// |
| 982 | /// # Examples |
| 983 | /// |
| 984 | /// ``` |
| 985 | /// assert_eq!(1f32.midpoint(4.0), 2.5); |
| 986 | /// assert_eq!((-5.5f32).midpoint(8.0), 1.25); |
| 987 | /// ``` |
| 988 | #[inline ] |
| 989 | #[doc (alias = "average" )] |
| 990 | #[stable (feature = "num_midpoint" , since = "1.85.0" )] |
| 991 | #[rustc_const_stable (feature = "num_midpoint" , since = "1.85.0" )] |
| 992 | pub const fn midpoint(self, other: f32) -> f32 { |
| 993 | cfg_select! { |
| 994 | // Allow faster implementation that have known good 64-bit float |
| 995 | // implementations. Falling back to the branchy code on targets that don't |
| 996 | // have 64-bit hardware floats or buggy implementations. |
| 997 | // https://github.com/rust-lang/rust/pull/121062#issuecomment-2123408114 |
| 998 | any( |
| 999 | target_arch = "x86_64" , |
| 1000 | target_arch = "aarch64" , |
| 1001 | all(any(target_arch = "riscv32" , target_arch = "riscv64" ), target_feature = "d" ), |
| 1002 | all(target_arch = "loongarch64" , target_feature = "d" ), |
| 1003 | all(target_arch = "arm" , target_feature = "vfp2" ), |
| 1004 | target_arch = "wasm32" , |
| 1005 | target_arch = "wasm64" , |
| 1006 | ) => { |
| 1007 | ((self as f64 + other as f64) / 2.0) as f32 |
| 1008 | } |
| 1009 | _ => { |
| 1010 | const LO: f32 = f32::MIN_POSITIVE * 2.; |
| 1011 | const HI: f32 = f32::MAX / 2.; |
| 1012 | |
| 1013 | let (a, b) = (self, other); |
| 1014 | let abs_a = a.abs(); |
| 1015 | let abs_b = b.abs(); |
| 1016 | |
| 1017 | if abs_a <= HI && abs_b <= HI { |
| 1018 | // Overflow is impossible |
| 1019 | (a + b) / 2. |
| 1020 | } else if abs_a < LO { |
| 1021 | // Not safe to halve `a` (would underflow) |
| 1022 | a + (b / 2.) |
| 1023 | } else if abs_b < LO { |
| 1024 | // Not safe to halve `b` (would underflow) |
| 1025 | (a / 2.) + b |
| 1026 | } else { |
| 1027 | // Safe to halve `a` and `b` |
| 1028 | (a / 2.) + (b / 2.) |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /// Rounds toward zero and converts to any primitive integer type, |
| 1035 | /// assuming that the value is finite and fits in that type. |
| 1036 | /// |
| 1037 | /// ``` |
| 1038 | /// let value = 4.6_f32; |
| 1039 | /// let rounded = unsafe { value.to_int_unchecked::<u16>() }; |
| 1040 | /// assert_eq!(rounded, 4); |
| 1041 | /// |
| 1042 | /// let value = -128.9_f32; |
| 1043 | /// let rounded = unsafe { value.to_int_unchecked::<i8>() }; |
| 1044 | /// assert_eq!(rounded, i8::MIN); |
| 1045 | /// ``` |
| 1046 | /// |
| 1047 | /// # Safety |
| 1048 | /// |
| 1049 | /// The value must: |
| 1050 | /// |
| 1051 | /// * Not be `NaN` |
| 1052 | /// * Not be infinite |
| 1053 | /// * Be representable in the return type `Int`, after truncating off its fractional part |
| 1054 | #[must_use = "this returns the result of the operation, \ |
| 1055 | without modifying the original" ] |
| 1056 | #[stable (feature = "float_approx_unchecked_to" , since = "1.44.0" )] |
| 1057 | #[inline ] |
| 1058 | pub unsafe fn to_int_unchecked<Int>(self) -> Int |
| 1059 | where |
| 1060 | Self: FloatToInt<Int>, |
| 1061 | { |
| 1062 | // SAFETY: the caller must uphold the safety contract for |
| 1063 | // `FloatToInt::to_int_unchecked`. |
| 1064 | unsafe { FloatToInt::<Int>::to_int_unchecked(self) } |
| 1065 | } |
| 1066 | |
| 1067 | /// Raw transmutation to `u32`. |
| 1068 | /// |
| 1069 | /// This is currently identical to `transmute::<f32, u32>(self)` on all platforms. |
| 1070 | /// |
| 1071 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
| 1072 | /// portability of this operation (there are almost no issues). |
| 1073 | /// |
| 1074 | /// Note that this function is distinct from `as` casting, which attempts to |
| 1075 | /// preserve the *numeric* value, and not the bitwise value. |
| 1076 | /// |
| 1077 | /// # Examples |
| 1078 | /// |
| 1079 | /// ``` |
| 1080 | /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting! |
| 1081 | /// assert_eq!((12.5f32).to_bits(), 0x41480000); |
| 1082 | /// |
| 1083 | /// ``` |
| 1084 | #[must_use = "this returns the result of the operation, \ |
| 1085 | without modifying the original" ] |
| 1086 | #[stable (feature = "float_bits_conv" , since = "1.20.0" )] |
| 1087 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1088 | #[inline ] |
| 1089 | #[allow (unnecessary_transmutes)] |
| 1090 | pub const fn to_bits(self) -> u32 { |
| 1091 | // SAFETY: `u32` is a plain old datatype so we can always transmute to it. |
| 1092 | unsafe { mem::transmute(self) } |
| 1093 | } |
| 1094 | |
| 1095 | /// Raw transmutation from `u32`. |
| 1096 | /// |
| 1097 | /// This is currently identical to `transmute::<u32, f32>(v)` on all platforms. |
| 1098 | /// It turns out this is incredibly portable, for two reasons: |
| 1099 | /// |
| 1100 | /// * Floats and Ints have the same endianness on all supported platforms. |
| 1101 | /// * IEEE 754 very precisely specifies the bit layout of floats. |
| 1102 | /// |
| 1103 | /// However there is one caveat: prior to the 2008 version of IEEE 754, how |
| 1104 | /// to interpret the NaN signaling bit wasn't actually specified. Most platforms |
| 1105 | /// (notably x86 and ARM) picked the interpretation that was ultimately |
| 1106 | /// standardized in 2008, but some didn't (notably MIPS). As a result, all |
| 1107 | /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa. |
| 1108 | /// |
| 1109 | /// Rather than trying to preserve signaling-ness cross-platform, this |
| 1110 | /// implementation favors preserving the exact bits. This means that |
| 1111 | /// any payloads encoded in NaNs will be preserved even if the result of |
| 1112 | /// this method is sent over the network from an x86 machine to a MIPS one. |
| 1113 | /// |
| 1114 | /// If the results of this method are only manipulated by the same |
| 1115 | /// architecture that produced them, then there is no portability concern. |
| 1116 | /// |
| 1117 | /// If the input isn't NaN, then there is no portability concern. |
| 1118 | /// |
| 1119 | /// If you don't care about signalingness (very likely), then there is no |
| 1120 | /// portability concern. |
| 1121 | /// |
| 1122 | /// Note that this function is distinct from `as` casting, which attempts to |
| 1123 | /// preserve the *numeric* value, and not the bitwise value. |
| 1124 | /// |
| 1125 | /// # Examples |
| 1126 | /// |
| 1127 | /// ``` |
| 1128 | /// let v = f32::from_bits(0x41480000); |
| 1129 | /// assert_eq!(v, 12.5); |
| 1130 | /// ``` |
| 1131 | #[stable (feature = "float_bits_conv" , since = "1.20.0" )] |
| 1132 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1133 | #[must_use ] |
| 1134 | #[inline ] |
| 1135 | #[allow (unnecessary_transmutes)] |
| 1136 | pub const fn from_bits(v: u32) -> Self { |
| 1137 | // It turns out the safety issues with sNaN were overblown! Hooray! |
| 1138 | // SAFETY: `u32` is a plain old datatype so we can always transmute from it. |
| 1139 | unsafe { mem::transmute(v) } |
| 1140 | } |
| 1141 | |
| 1142 | /// Returns the memory representation of this floating point number as a byte array in |
| 1143 | /// big-endian (network) byte order. |
| 1144 | /// |
| 1145 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
| 1146 | /// portability of this operation (there are almost no issues). |
| 1147 | /// |
| 1148 | /// # Examples |
| 1149 | /// |
| 1150 | /// ``` |
| 1151 | /// let bytes = 12.5f32.to_be_bytes(); |
| 1152 | /// assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]); |
| 1153 | /// ``` |
| 1154 | #[must_use = "this returns the result of the operation, \ |
| 1155 | without modifying the original" ] |
| 1156 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
| 1157 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1158 | #[inline ] |
| 1159 | pub const fn to_be_bytes(self) -> [u8; 4] { |
| 1160 | self.to_bits().to_be_bytes() |
| 1161 | } |
| 1162 | |
| 1163 | /// Returns the memory representation of this floating point number as a byte array in |
| 1164 | /// little-endian byte order. |
| 1165 | /// |
| 1166 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
| 1167 | /// portability of this operation (there are almost no issues). |
| 1168 | /// |
| 1169 | /// # Examples |
| 1170 | /// |
| 1171 | /// ``` |
| 1172 | /// let bytes = 12.5f32.to_le_bytes(); |
| 1173 | /// assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]); |
| 1174 | /// ``` |
| 1175 | #[must_use = "this returns the result of the operation, \ |
| 1176 | without modifying the original" ] |
| 1177 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
| 1178 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1179 | #[inline ] |
| 1180 | pub const fn to_le_bytes(self) -> [u8; 4] { |
| 1181 | self.to_bits().to_le_bytes() |
| 1182 | } |
| 1183 | |
| 1184 | /// Returns the memory representation of this floating point number as a byte array in |
| 1185 | /// native byte order. |
| 1186 | /// |
| 1187 | /// As the target platform's native endianness is used, portable code |
| 1188 | /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead. |
| 1189 | /// |
| 1190 | /// [`to_be_bytes`]: f32::to_be_bytes |
| 1191 | /// [`to_le_bytes`]: f32::to_le_bytes |
| 1192 | /// |
| 1193 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
| 1194 | /// portability of this operation (there are almost no issues). |
| 1195 | /// |
| 1196 | /// # Examples |
| 1197 | /// |
| 1198 | /// ``` |
| 1199 | /// let bytes = 12.5f32.to_ne_bytes(); |
| 1200 | /// assert_eq!( |
| 1201 | /// bytes, |
| 1202 | /// if cfg!(target_endian = "big" ) { |
| 1203 | /// [0x41, 0x48, 0x00, 0x00] |
| 1204 | /// } else { |
| 1205 | /// [0x00, 0x00, 0x48, 0x41] |
| 1206 | /// } |
| 1207 | /// ); |
| 1208 | /// ``` |
| 1209 | #[must_use = "this returns the result of the operation, \ |
| 1210 | without modifying the original" ] |
| 1211 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
| 1212 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1213 | #[inline ] |
| 1214 | pub const fn to_ne_bytes(self) -> [u8; 4] { |
| 1215 | self.to_bits().to_ne_bytes() |
| 1216 | } |
| 1217 | |
| 1218 | /// Creates a floating point value from its representation as a byte array in big endian. |
| 1219 | /// |
| 1220 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
| 1221 | /// portability of this operation (there are almost no issues). |
| 1222 | /// |
| 1223 | /// # Examples |
| 1224 | /// |
| 1225 | /// ``` |
| 1226 | /// let value = f32::from_be_bytes([0x41, 0x48, 0x00, 0x00]); |
| 1227 | /// assert_eq!(value, 12.5); |
| 1228 | /// ``` |
| 1229 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
| 1230 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1231 | #[must_use ] |
| 1232 | #[inline ] |
| 1233 | pub const fn from_be_bytes(bytes: [u8; 4]) -> Self { |
| 1234 | Self::from_bits(u32::from_be_bytes(bytes)) |
| 1235 | } |
| 1236 | |
| 1237 | /// Creates a floating point value from its representation as a byte array in little endian. |
| 1238 | /// |
| 1239 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
| 1240 | /// portability of this operation (there are almost no issues). |
| 1241 | /// |
| 1242 | /// # Examples |
| 1243 | /// |
| 1244 | /// ``` |
| 1245 | /// let value = f32::from_le_bytes([0x00, 0x00, 0x48, 0x41]); |
| 1246 | /// assert_eq!(value, 12.5); |
| 1247 | /// ``` |
| 1248 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
| 1249 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1250 | #[must_use ] |
| 1251 | #[inline ] |
| 1252 | pub const fn from_le_bytes(bytes: [u8; 4]) -> Self { |
| 1253 | Self::from_bits(u32::from_le_bytes(bytes)) |
| 1254 | } |
| 1255 | |
| 1256 | /// Creates a floating point value from its representation as a byte array in native endian. |
| 1257 | /// |
| 1258 | /// As the target platform's native endianness is used, portable code |
| 1259 | /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as |
| 1260 | /// appropriate instead. |
| 1261 | /// |
| 1262 | /// [`from_be_bytes`]: f32::from_be_bytes |
| 1263 | /// [`from_le_bytes`]: f32::from_le_bytes |
| 1264 | /// |
| 1265 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
| 1266 | /// portability of this operation (there are almost no issues). |
| 1267 | /// |
| 1268 | /// # Examples |
| 1269 | /// |
| 1270 | /// ``` |
| 1271 | /// let value = f32::from_ne_bytes(if cfg!(target_endian = "big" ) { |
| 1272 | /// [0x41, 0x48, 0x00, 0x00] |
| 1273 | /// } else { |
| 1274 | /// [0x00, 0x00, 0x48, 0x41] |
| 1275 | /// }); |
| 1276 | /// assert_eq!(value, 12.5); |
| 1277 | /// ``` |
| 1278 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
| 1279 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
| 1280 | #[must_use ] |
| 1281 | #[inline ] |
| 1282 | pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self { |
| 1283 | Self::from_bits(u32::from_ne_bytes(bytes)) |
| 1284 | } |
| 1285 | |
| 1286 | /// Returns the ordering between `self` and `other`. |
| 1287 | /// |
| 1288 | /// Unlike the standard partial comparison between floating point numbers, |
| 1289 | /// this comparison always produces an ordering in accordance to |
| 1290 | /// the `totalOrder` predicate as defined in the IEEE 754 (2008 revision) |
| 1291 | /// floating point standard. The values are ordered in the following sequence: |
| 1292 | /// |
| 1293 | /// - negative quiet NaN |
| 1294 | /// - negative signaling NaN |
| 1295 | /// - negative infinity |
| 1296 | /// - negative numbers |
| 1297 | /// - negative subnormal numbers |
| 1298 | /// - negative zero |
| 1299 | /// - positive zero |
| 1300 | /// - positive subnormal numbers |
| 1301 | /// - positive numbers |
| 1302 | /// - positive infinity |
| 1303 | /// - positive signaling NaN |
| 1304 | /// - positive quiet NaN. |
| 1305 | /// |
| 1306 | /// The ordering established by this function does not always agree with the |
| 1307 | /// [`PartialOrd`] and [`PartialEq`] implementations of `f32`. For example, |
| 1308 | /// they consider negative and positive zero equal, while `total_cmp` |
| 1309 | /// doesn't. |
| 1310 | /// |
| 1311 | /// The interpretation of the signaling NaN bit follows the definition in |
| 1312 | /// the IEEE 754 standard, which may not match the interpretation by some of |
| 1313 | /// the older, non-conformant (e.g. MIPS) hardware implementations. |
| 1314 | /// |
| 1315 | /// # Example |
| 1316 | /// |
| 1317 | /// ``` |
| 1318 | /// struct GoodBoy { |
| 1319 | /// name: String, |
| 1320 | /// weight: f32, |
| 1321 | /// } |
| 1322 | /// |
| 1323 | /// let mut bois = vec![ |
| 1324 | /// GoodBoy { name: "Pucci" .to_owned(), weight: 0.1 }, |
| 1325 | /// GoodBoy { name: "Woofer" .to_owned(), weight: 99.0 }, |
| 1326 | /// GoodBoy { name: "Yapper" .to_owned(), weight: 10.0 }, |
| 1327 | /// GoodBoy { name: "Chonk" .to_owned(), weight: f32::INFINITY }, |
| 1328 | /// GoodBoy { name: "Abs. Unit" .to_owned(), weight: f32::NAN }, |
| 1329 | /// GoodBoy { name: "Floaty" .to_owned(), weight: -5.0 }, |
| 1330 | /// ]; |
| 1331 | /// |
| 1332 | /// bois.sort_by(|a, b| a.weight.total_cmp(&b.weight)); |
| 1333 | /// |
| 1334 | /// // `f32::NAN` could be positive or negative, which will affect the sort order. |
| 1335 | /// if f32::NAN.is_sign_negative() { |
| 1336 | /// assert!(bois.into_iter().map(|b| b.weight) |
| 1337 | /// .zip([f32::NAN, -5.0, 0.1, 10.0, 99.0, f32::INFINITY].iter()) |
| 1338 | /// .all(|(a, b)| a.to_bits() == b.to_bits())) |
| 1339 | /// } else { |
| 1340 | /// assert!(bois.into_iter().map(|b| b.weight) |
| 1341 | /// .zip([-5.0, 0.1, 10.0, 99.0, f32::INFINITY, f32::NAN].iter()) |
| 1342 | /// .all(|(a, b)| a.to_bits() == b.to_bits())) |
| 1343 | /// } |
| 1344 | /// ``` |
| 1345 | #[stable (feature = "total_cmp" , since = "1.62.0" )] |
| 1346 | #[must_use ] |
| 1347 | #[inline ] |
| 1348 | pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering { |
| 1349 | let mut left = self.to_bits() as i32; |
| 1350 | let mut right = other.to_bits() as i32; |
| 1351 | |
| 1352 | // In case of negatives, flip all the bits except the sign |
| 1353 | // to achieve a similar layout as two's complement integers |
| 1354 | // |
| 1355 | // Why does this work? IEEE 754 floats consist of three fields: |
| 1356 | // Sign bit, exponent and mantissa. The set of exponent and mantissa |
| 1357 | // fields as a whole have the property that their bitwise order is |
| 1358 | // equal to the numeric magnitude where the magnitude is defined. |
| 1359 | // The magnitude is not normally defined on NaN values, but |
| 1360 | // IEEE 754 totalOrder defines the NaN values also to follow the |
| 1361 | // bitwise order. This leads to order explained in the doc comment. |
| 1362 | // However, the representation of magnitude is the same for negative |
| 1363 | // and positive numbers – only the sign bit is different. |
| 1364 | // To easily compare the floats as signed integers, we need to |
| 1365 | // flip the exponent and mantissa bits in case of negative numbers. |
| 1366 | // We effectively convert the numbers to "two's complement" form. |
| 1367 | // |
| 1368 | // To do the flipping, we construct a mask and XOR against it. |
| 1369 | // We branchlessly calculate an "all-ones except for the sign bit" |
| 1370 | // mask from negative-signed values: right shifting sign-extends |
| 1371 | // the integer, so we "fill" the mask with sign bits, and then |
| 1372 | // convert to unsigned to push one more zero bit. |
| 1373 | // On positive values, the mask is all zeros, so it's a no-op. |
| 1374 | left ^= (((left >> 31) as u32) >> 1) as i32; |
| 1375 | right ^= (((right >> 31) as u32) >> 1) as i32; |
| 1376 | |
| 1377 | left.cmp(&right) |
| 1378 | } |
| 1379 | |
| 1380 | /// Restrict a value to a certain interval unless it is NaN. |
| 1381 | /// |
| 1382 | /// Returns `max` if `self` is greater than `max`, and `min` if `self` is |
| 1383 | /// less than `min`. Otherwise this returns `self`. |
| 1384 | /// |
| 1385 | /// Note that this function returns NaN if the initial value was NaN as |
| 1386 | /// well. |
| 1387 | /// |
| 1388 | /// # Panics |
| 1389 | /// |
| 1390 | /// Panics if `min > max`, `min` is NaN, or `max` is NaN. |
| 1391 | /// |
| 1392 | /// # Examples |
| 1393 | /// |
| 1394 | /// ``` |
| 1395 | /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0); |
| 1396 | /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0); |
| 1397 | /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0); |
| 1398 | /// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan()); |
| 1399 | /// ``` |
| 1400 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1401 | #[stable (feature = "clamp" , since = "1.50.0" )] |
| 1402 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 1403 | #[inline ] |
| 1404 | pub const fn clamp(mut self, min: f32, max: f32) -> f32 { |
| 1405 | const_assert!( |
| 1406 | min <= max, |
| 1407 | "min > max, or either was NaN" , |
| 1408 | "min > max, or either was NaN. min = {min:?}, max = {max:?}" , |
| 1409 | min: f32, |
| 1410 | max: f32, |
| 1411 | ); |
| 1412 | |
| 1413 | if self < min { |
| 1414 | self = min; |
| 1415 | } |
| 1416 | if self > max { |
| 1417 | self = max; |
| 1418 | } |
| 1419 | self |
| 1420 | } |
| 1421 | |
| 1422 | /// Computes the absolute value of `self`. |
| 1423 | /// |
| 1424 | /// This function always returns the precise result. |
| 1425 | /// |
| 1426 | /// # Examples |
| 1427 | /// |
| 1428 | /// ``` |
| 1429 | /// let x = 3.5_f32; |
| 1430 | /// let y = -3.5_f32; |
| 1431 | /// |
| 1432 | /// assert_eq!(x.abs(), x); |
| 1433 | /// assert_eq!(y.abs(), -y); |
| 1434 | /// |
| 1435 | /// assert!(f32::NAN.abs().is_nan()); |
| 1436 | /// ``` |
| 1437 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1438 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 1439 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 1440 | #[inline ] |
| 1441 | pub const fn abs(self) -> f32 { |
| 1442 | // SAFETY: this is actually a safe intrinsic |
| 1443 | unsafe { intrinsics::fabsf32(self) } |
| 1444 | } |
| 1445 | |
| 1446 | /// Returns a number that represents the sign of `self`. |
| 1447 | /// |
| 1448 | /// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 1449 | /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 1450 | /// - NaN if the number is NaN |
| 1451 | /// |
| 1452 | /// # Examples |
| 1453 | /// |
| 1454 | /// ``` |
| 1455 | /// let f = 3.5_f32; |
| 1456 | /// |
| 1457 | /// assert_eq!(f.signum(), 1.0); |
| 1458 | /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0); |
| 1459 | /// |
| 1460 | /// assert!(f32::NAN.signum().is_nan()); |
| 1461 | /// ``` |
| 1462 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1463 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 1464 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 1465 | #[inline ] |
| 1466 | pub const fn signum(self) -> f32 { |
| 1467 | if self.is_nan() { Self::NAN } else { 1.0_f32.copysign(self) } |
| 1468 | } |
| 1469 | |
| 1470 | /// Returns a number composed of the magnitude of `self` and the sign of |
| 1471 | /// `sign`. |
| 1472 | /// |
| 1473 | /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise equal to `-self`. |
| 1474 | /// If `self` is a NaN, then a NaN with the same payload as `self` and the sign bit of `sign` is |
| 1475 | /// returned. |
| 1476 | /// |
| 1477 | /// If `sign` is a NaN, then this operation will still carry over its sign into the result. Note |
| 1478 | /// that IEEE 754 doesn't assign any meaning to the sign bit in case of a NaN, and as Rust |
| 1479 | /// doesn't guarantee that the bit pattern of NaNs are conserved over arithmetic operations, the |
| 1480 | /// result of `copysign` with `sign` being a NaN might produce an unexpected or non-portable |
| 1481 | /// result. See the [specification of NaN bit patterns](primitive@f32#nan-bit-patterns) for more |
| 1482 | /// info. |
| 1483 | /// |
| 1484 | /// # Examples |
| 1485 | /// |
| 1486 | /// ``` |
| 1487 | /// let f = 3.5_f32; |
| 1488 | /// |
| 1489 | /// assert_eq!(f.copysign(0.42), 3.5_f32); |
| 1490 | /// assert_eq!(f.copysign(-0.42), -3.5_f32); |
| 1491 | /// assert_eq!((-f).copysign(0.42), 3.5_f32); |
| 1492 | /// assert_eq!((-f).copysign(-0.42), -3.5_f32); |
| 1493 | /// |
| 1494 | /// assert!(f32::NAN.copysign(1.0).is_nan()); |
| 1495 | /// ``` |
| 1496 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1497 | #[inline ] |
| 1498 | #[stable (feature = "copysign" , since = "1.35.0" )] |
| 1499 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
| 1500 | pub const fn copysign(self, sign: f32) -> f32 { |
| 1501 | // SAFETY: this is actually a safe intrinsic |
| 1502 | unsafe { intrinsics::copysignf32(self, sign) } |
| 1503 | } |
| 1504 | |
| 1505 | /// Float addition that allows optimizations based on algebraic rules. |
| 1506 | /// |
| 1507 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info. |
| 1508 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1509 | #[unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1510 | #[rustc_const_unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1511 | #[inline ] |
| 1512 | pub const fn algebraic_add(self, rhs: f32) -> f32 { |
| 1513 | intrinsics::fadd_algebraic(self, rhs) |
| 1514 | } |
| 1515 | |
| 1516 | /// Float subtraction that allows optimizations based on algebraic rules. |
| 1517 | /// |
| 1518 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info. |
| 1519 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1520 | #[unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1521 | #[rustc_const_unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1522 | #[inline ] |
| 1523 | pub const fn algebraic_sub(self, rhs: f32) -> f32 { |
| 1524 | intrinsics::fsub_algebraic(self, rhs) |
| 1525 | } |
| 1526 | |
| 1527 | /// Float multiplication that allows optimizations based on algebraic rules. |
| 1528 | /// |
| 1529 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info. |
| 1530 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1531 | #[unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1532 | #[rustc_const_unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1533 | #[inline ] |
| 1534 | pub const fn algebraic_mul(self, rhs: f32) -> f32 { |
| 1535 | intrinsics::fmul_algebraic(self, rhs) |
| 1536 | } |
| 1537 | |
| 1538 | /// Float division that allows optimizations based on algebraic rules. |
| 1539 | /// |
| 1540 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info. |
| 1541 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1542 | #[unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1543 | #[rustc_const_unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1544 | #[inline ] |
| 1545 | pub const fn algebraic_div(self, rhs: f32) -> f32 { |
| 1546 | intrinsics::fdiv_algebraic(self, rhs) |
| 1547 | } |
| 1548 | |
| 1549 | /// Float remainder that allows optimizations based on algebraic rules. |
| 1550 | /// |
| 1551 | /// See [algebraic operators](primitive@f32#algebraic-operators) for more info. |
| 1552 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1553 | #[unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1554 | #[rustc_const_unstable (feature = "float_algebraic" , issue = "136469" )] |
| 1555 | #[inline ] |
| 1556 | pub const fn algebraic_rem(self, rhs: f32) -> f32 { |
| 1557 | intrinsics::frem_algebraic(self, rhs) |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | /// Experimental implementations of floating point functions in `core`. |
| 1562 | /// |
| 1563 | /// _The standalone functions in this module are for testing only. |
| 1564 | /// They will be stabilized as inherent methods._ |
| 1565 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1566 | pub mod math { |
| 1567 | use crate::intrinsics; |
| 1568 | use crate::num::libm; |
| 1569 | |
| 1570 | /// Experimental version of `floor` in `core`. See [`f32::floor`] for details. |
| 1571 | /// |
| 1572 | /// # Examples |
| 1573 | /// |
| 1574 | /// ``` |
| 1575 | /// #![feature(core_float_math)] |
| 1576 | /// |
| 1577 | /// use core::f32; |
| 1578 | /// |
| 1579 | /// let f = 3.7_f32; |
| 1580 | /// let g = 3.0_f32; |
| 1581 | /// let h = -3.7_f32; |
| 1582 | /// |
| 1583 | /// assert_eq!(f32::math::floor(f), 3.0); |
| 1584 | /// assert_eq!(f32::math::floor(g), 3.0); |
| 1585 | /// assert_eq!(f32::math::floor(h), -4.0); |
| 1586 | /// ``` |
| 1587 | /// |
| 1588 | /// _This standalone function is for testing only. |
| 1589 | /// It will be stabilized as an inherent method._ |
| 1590 | /// |
| 1591 | /// [`f32::floor`]: ../../../std/primitive.f32.html#method.floor |
| 1592 | #[inline ] |
| 1593 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1594 | #[rustc_const_unstable (feature = "const_float_round_methods" , issue = "141555" )] |
| 1595 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1596 | pub const fn floor(x: f32) -> f32 { |
| 1597 | // SAFETY: intrinsic with no preconditions |
| 1598 | unsafe { intrinsics::floorf32(x) } |
| 1599 | } |
| 1600 | |
| 1601 | /// Experimental version of `ceil` in `core`. See [`f32::ceil`] for details. |
| 1602 | /// |
| 1603 | /// # Examples |
| 1604 | /// |
| 1605 | /// ``` |
| 1606 | /// #![feature(core_float_math)] |
| 1607 | /// |
| 1608 | /// use core::f32; |
| 1609 | /// |
| 1610 | /// let f = 3.01_f32; |
| 1611 | /// let g = 4.0_f32; |
| 1612 | /// |
| 1613 | /// assert_eq!(f32::math::ceil(f), 4.0); |
| 1614 | /// assert_eq!(f32::math::ceil(g), 4.0); |
| 1615 | /// ``` |
| 1616 | /// |
| 1617 | /// _This standalone function is for testing only. |
| 1618 | /// It will be stabilized as an inherent method._ |
| 1619 | /// |
| 1620 | /// [`f32::ceil`]: ../../../std/primitive.f32.html#method.ceil |
| 1621 | #[inline ] |
| 1622 | #[doc (alias = "ceiling" )] |
| 1623 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1624 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1625 | #[rustc_const_unstable (feature = "const_float_round_methods" , issue = "141555" )] |
| 1626 | pub const fn ceil(x: f32) -> f32 { |
| 1627 | // SAFETY: intrinsic with no preconditions |
| 1628 | unsafe { intrinsics::ceilf32(x) } |
| 1629 | } |
| 1630 | |
| 1631 | /// Experimental version of `round` in `core`. See [`f32::round`] for details. |
| 1632 | /// |
| 1633 | /// # Examples |
| 1634 | /// |
| 1635 | /// ``` |
| 1636 | /// #![feature(core_float_math)] |
| 1637 | /// |
| 1638 | /// use core::f32; |
| 1639 | /// |
| 1640 | /// let f = 3.3_f32; |
| 1641 | /// let g = -3.3_f32; |
| 1642 | /// let h = -3.7_f32; |
| 1643 | /// let i = 3.5_f32; |
| 1644 | /// let j = 4.5_f32; |
| 1645 | /// |
| 1646 | /// assert_eq!(f32::math::round(f), 3.0); |
| 1647 | /// assert_eq!(f32::math::round(g), -3.0); |
| 1648 | /// assert_eq!(f32::math::round(h), -4.0); |
| 1649 | /// assert_eq!(f32::math::round(i), 4.0); |
| 1650 | /// assert_eq!(f32::math::round(j), 5.0); |
| 1651 | /// ``` |
| 1652 | /// |
| 1653 | /// _This standalone function is for testing only. |
| 1654 | /// It will be stabilized as an inherent method._ |
| 1655 | /// |
| 1656 | /// [`f32::round`]: ../../../std/primitive.f32.html#method.round |
| 1657 | #[inline ] |
| 1658 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1659 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1660 | #[rustc_const_unstable (feature = "const_float_round_methods" , issue = "141555" )] |
| 1661 | pub const fn round(x: f32) -> f32 { |
| 1662 | // SAFETY: intrinsic with no preconditions |
| 1663 | unsafe { intrinsics::roundf32(x) } |
| 1664 | } |
| 1665 | |
| 1666 | /// Experimental version of `round_ties_even` in `core`. See [`f32::round_ties_even`] for |
| 1667 | /// details. |
| 1668 | /// |
| 1669 | /// # Examples |
| 1670 | /// |
| 1671 | /// ``` |
| 1672 | /// #![feature(core_float_math)] |
| 1673 | /// |
| 1674 | /// use core::f32; |
| 1675 | /// |
| 1676 | /// let f = 3.3_f32; |
| 1677 | /// let g = -3.3_f32; |
| 1678 | /// let h = 3.5_f32; |
| 1679 | /// let i = 4.5_f32; |
| 1680 | /// |
| 1681 | /// assert_eq!(f32::math::round_ties_even(f), 3.0); |
| 1682 | /// assert_eq!(f32::math::round_ties_even(g), -3.0); |
| 1683 | /// assert_eq!(f32::math::round_ties_even(h), 4.0); |
| 1684 | /// assert_eq!(f32::math::round_ties_even(i), 4.0); |
| 1685 | /// ``` |
| 1686 | /// |
| 1687 | /// _This standalone function is for testing only. |
| 1688 | /// It will be stabilized as an inherent method._ |
| 1689 | /// |
| 1690 | /// [`f32::round_ties_even`]: ../../../std/primitive.f32.html#method.round_ties_even |
| 1691 | #[inline ] |
| 1692 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1693 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1694 | #[rustc_const_unstable (feature = "const_float_round_methods" , issue = "141555" )] |
| 1695 | pub const fn round_ties_even(x: f32) -> f32 { |
| 1696 | intrinsics::round_ties_even_f32(x) |
| 1697 | } |
| 1698 | |
| 1699 | /// Experimental version of `trunc` in `core`. See [`f32::trunc`] for details. |
| 1700 | /// |
| 1701 | /// # Examples |
| 1702 | /// |
| 1703 | /// ``` |
| 1704 | /// #![feature(core_float_math)] |
| 1705 | /// |
| 1706 | /// use core::f32; |
| 1707 | /// |
| 1708 | /// let f = 3.7_f32; |
| 1709 | /// let g = 3.0_f32; |
| 1710 | /// let h = -3.7_f32; |
| 1711 | /// |
| 1712 | /// assert_eq!(f32::math::trunc(f), 3.0); |
| 1713 | /// assert_eq!(f32::math::trunc(g), 3.0); |
| 1714 | /// assert_eq!(f32::math::trunc(h), -3.0); |
| 1715 | /// ``` |
| 1716 | /// |
| 1717 | /// _This standalone function is for testing only. |
| 1718 | /// It will be stabilized as an inherent method._ |
| 1719 | /// |
| 1720 | /// [`f32::trunc`]: ../../../std/primitive.f32.html#method.trunc |
| 1721 | #[inline ] |
| 1722 | #[doc (alias = "truncate" )] |
| 1723 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1724 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1725 | #[rustc_const_unstable (feature = "const_float_round_methods" , issue = "141555" )] |
| 1726 | pub const fn trunc(x: f32) -> f32 { |
| 1727 | // SAFETY: intrinsic with no preconditions |
| 1728 | unsafe { intrinsics::truncf32(x) } |
| 1729 | } |
| 1730 | |
| 1731 | /// Experimental version of `fract` in `core`. See [`f32::fract`] for details. |
| 1732 | /// |
| 1733 | /// # Examples |
| 1734 | /// |
| 1735 | /// ``` |
| 1736 | /// #![feature(core_float_math)] |
| 1737 | /// |
| 1738 | /// use core::f32; |
| 1739 | /// |
| 1740 | /// let x = 3.6_f32; |
| 1741 | /// let y = -3.6_f32; |
| 1742 | /// let abs_difference_x = (f32::math::fract(x) - 0.6).abs(); |
| 1743 | /// let abs_difference_y = (f32::math::fract(y) - (-0.6)).abs(); |
| 1744 | /// |
| 1745 | /// assert!(abs_difference_x <= f32::EPSILON); |
| 1746 | /// assert!(abs_difference_y <= f32::EPSILON); |
| 1747 | /// ``` |
| 1748 | /// |
| 1749 | /// _This standalone function is for testing only. |
| 1750 | /// It will be stabilized as an inherent method._ |
| 1751 | /// |
| 1752 | /// [`f32::fract`]: ../../../std/primitive.f32.html#method.fract |
| 1753 | #[inline ] |
| 1754 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1755 | #[rustc_const_unstable (feature = "const_float_round_methods" , issue = "141555" )] |
| 1756 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1757 | pub const fn fract(x: f32) -> f32 { |
| 1758 | x - trunc(x) |
| 1759 | } |
| 1760 | |
| 1761 | /// Experimental version of `mul_add` in `core`. See [`f32::mul_add`] for details. |
| 1762 | /// |
| 1763 | /// # Examples |
| 1764 | /// |
| 1765 | /// ``` |
| 1766 | /// #![feature(core_float_math)] |
| 1767 | /// |
| 1768 | /// # // FIXME(#140515): mingw has an incorrect fma |
| 1769 | /// # // https://sourceforge.net/p/mingw-w64/bugs/848/ |
| 1770 | /// # #[cfg (all(target_os = "windows" , target_env = "gnu" , not(target_abi = "llvm" )))] { |
| 1771 | /// use core::f32; |
| 1772 | /// |
| 1773 | /// let m = 10.0_f32; |
| 1774 | /// let x = 4.0_f32; |
| 1775 | /// let b = 60.0_f32; |
| 1776 | /// |
| 1777 | /// assert_eq!(f32::math::mul_add(m, x, b), 100.0); |
| 1778 | /// assert_eq!(m * x + b, 100.0); |
| 1779 | /// |
| 1780 | /// let one_plus_eps = 1.0_f32 + f32::EPSILON; |
| 1781 | /// let one_minus_eps = 1.0_f32 - f32::EPSILON; |
| 1782 | /// let minus_one = -1.0_f32; |
| 1783 | /// |
| 1784 | /// // The exact result (1 + eps) * (1 - eps) = 1 - eps * eps. |
| 1785 | /// assert_eq!( |
| 1786 | /// f32::math::mul_add(one_plus_eps, one_minus_eps, minus_one), |
| 1787 | /// -f32::EPSILON * f32::EPSILON |
| 1788 | /// ); |
| 1789 | /// // Different rounding with the non-fused multiply and add. |
| 1790 | /// assert_eq!(one_plus_eps * one_minus_eps + minus_one, 0.0); |
| 1791 | /// # } |
| 1792 | /// ``` |
| 1793 | /// |
| 1794 | /// _This standalone function is for testing only. |
| 1795 | /// It will be stabilized as an inherent method._ |
| 1796 | /// |
| 1797 | /// [`f32::mul_add`]: ../../../std/primitive.f32.html#method.mul_add |
| 1798 | #[inline ] |
| 1799 | #[doc (alias = "fmaf" , alias = "fusedMultiplyAdd" )] |
| 1800 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1801 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1802 | pub fn mul_add(x: f32, y: f32, z: f32) -> f32 { |
| 1803 | // SAFETY: intrinsic with no preconditions |
| 1804 | unsafe { intrinsics::fmaf32(x, y, z) } |
| 1805 | } |
| 1806 | |
| 1807 | /// Experimental version of `div_euclid` in `core`. See [`f32::div_euclid`] for details. |
| 1808 | /// |
| 1809 | /// # Examples |
| 1810 | /// |
| 1811 | /// ``` |
| 1812 | /// #![feature(core_float_math)] |
| 1813 | /// |
| 1814 | /// use core::f32; |
| 1815 | /// |
| 1816 | /// let a: f32 = 7.0; |
| 1817 | /// let b = 4.0; |
| 1818 | /// assert_eq!(f32::math::div_euclid(a, b), 1.0); // 7.0 > 4.0 * 1.0 |
| 1819 | /// assert_eq!(f32::math::div_euclid(-a, b), -2.0); // -7.0 >= 4.0 * -2.0 |
| 1820 | /// assert_eq!(f32::math::div_euclid(a, -b), -1.0); // 7.0 >= -4.0 * -1.0 |
| 1821 | /// assert_eq!(f32::math::div_euclid(-a, -b), 2.0); // -7.0 >= -4.0 * 2.0 |
| 1822 | /// ``` |
| 1823 | /// |
| 1824 | /// _This standalone function is for testing only. |
| 1825 | /// It will be stabilized as an inherent method._ |
| 1826 | /// |
| 1827 | /// [`f32::div_euclid`]: ../../../std/primitive.f32.html#method.div_euclid |
| 1828 | #[inline ] |
| 1829 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1830 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1831 | pub fn div_euclid(x: f32, rhs: f32) -> f32 { |
| 1832 | let q = trunc(x / rhs); |
| 1833 | if x % rhs < 0.0 { |
| 1834 | return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }; |
| 1835 | } |
| 1836 | q |
| 1837 | } |
| 1838 | |
| 1839 | /// Experimental version of `rem_euclid` in `core`. See [`f32::rem_euclid`] for details. |
| 1840 | /// |
| 1841 | /// # Examples |
| 1842 | /// |
| 1843 | /// ``` |
| 1844 | /// #![feature(core_float_math)] |
| 1845 | /// |
| 1846 | /// use core::f32; |
| 1847 | /// |
| 1848 | /// let a: f32 = 7.0; |
| 1849 | /// let b = 4.0; |
| 1850 | /// assert_eq!(f32::math::rem_euclid(a, b), 3.0); |
| 1851 | /// assert_eq!(f32::math::rem_euclid(-a, b), 1.0); |
| 1852 | /// assert_eq!(f32::math::rem_euclid(a, -b), 3.0); |
| 1853 | /// assert_eq!(f32::math::rem_euclid(-a, -b), 1.0); |
| 1854 | /// // limitation due to round-off error |
| 1855 | /// assert!(f32::math::rem_euclid(-f32::EPSILON, 3.0) != 0.0); |
| 1856 | /// ``` |
| 1857 | /// |
| 1858 | /// _This standalone function is for testing only. |
| 1859 | /// It will be stabilized as an inherent method._ |
| 1860 | /// |
| 1861 | /// [`f32::rem_euclid`]: ../../../std/primitive.f32.html#method.rem_euclid |
| 1862 | #[inline ] |
| 1863 | #[doc (alias = "modulo" , alias = "mod" )] |
| 1864 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1865 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1866 | pub fn rem_euclid(x: f32, rhs: f32) -> f32 { |
| 1867 | let r = x % rhs; |
| 1868 | if r < 0.0 { r + rhs.abs() } else { r } |
| 1869 | } |
| 1870 | |
| 1871 | /// Experimental version of `powi` in `core`. See [`f32::powi`] for details. |
| 1872 | /// |
| 1873 | /// # Examples |
| 1874 | /// |
| 1875 | /// ``` |
| 1876 | /// #![feature(core_float_math)] |
| 1877 | /// |
| 1878 | /// use core::f32; |
| 1879 | /// |
| 1880 | /// let x = 2.0_f32; |
| 1881 | /// let abs_difference = (f32::math::powi(x, 2) - (x * x)).abs(); |
| 1882 | /// assert!(abs_difference <= 1e-5); |
| 1883 | /// |
| 1884 | /// assert_eq!(f32::math::powi(f32::NAN, 0), 1.0); |
| 1885 | /// ``` |
| 1886 | /// |
| 1887 | /// _This standalone function is for testing only. |
| 1888 | /// It will be stabilized as an inherent method._ |
| 1889 | /// |
| 1890 | /// [`f32::powi`]: ../../../std/primitive.f32.html#method.powi |
| 1891 | #[inline ] |
| 1892 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1893 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1894 | pub fn powi(x: f32, n: i32) -> f32 { |
| 1895 | // SAFETY: intrinsic with no preconditions |
| 1896 | unsafe { intrinsics::powif32(x, n) } |
| 1897 | } |
| 1898 | |
| 1899 | /// Experimental version of `sqrt` in `core`. See [`f32::sqrt`] for details. |
| 1900 | /// |
| 1901 | /// # Examples |
| 1902 | /// |
| 1903 | /// ``` |
| 1904 | /// #![feature(core_float_math)] |
| 1905 | /// |
| 1906 | /// use core::f32; |
| 1907 | /// |
| 1908 | /// let positive = 4.0_f32; |
| 1909 | /// let negative = -4.0_f32; |
| 1910 | /// let negative_zero = -0.0_f32; |
| 1911 | /// |
| 1912 | /// assert_eq!(f32::math::sqrt(positive), 2.0); |
| 1913 | /// assert!(f32::math::sqrt(negative).is_nan()); |
| 1914 | /// assert_eq!(f32::math::sqrt(negative_zero), negative_zero); |
| 1915 | /// ``` |
| 1916 | /// |
| 1917 | /// _This standalone function is for testing only. |
| 1918 | /// It will be stabilized as an inherent method._ |
| 1919 | /// |
| 1920 | /// [`f32::sqrt`]: ../../../std/primitive.f32.html#method.sqrt |
| 1921 | #[inline ] |
| 1922 | #[doc (alias = "squareRoot" )] |
| 1923 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 1924 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1925 | pub fn sqrt(x: f32) -> f32 { |
| 1926 | // SAFETY: intrinsic with no preconditions |
| 1927 | unsafe { intrinsics::sqrtf32(x) } |
| 1928 | } |
| 1929 | |
| 1930 | /// Experimental version of `abs_sub` in `core`. See [`f32::abs_sub`] for details. |
| 1931 | /// |
| 1932 | /// # Examples |
| 1933 | /// |
| 1934 | /// ``` |
| 1935 | /// #![feature(core_float_math)] |
| 1936 | /// |
| 1937 | /// use core::f32; |
| 1938 | /// |
| 1939 | /// let x = 3.0f32; |
| 1940 | /// let y = -3.0f32; |
| 1941 | /// |
| 1942 | /// let abs_difference_x = (f32::math::abs_sub(x, 1.0) - 2.0).abs(); |
| 1943 | /// let abs_difference_y = (f32::math::abs_sub(y, 1.0) - 0.0).abs(); |
| 1944 | /// |
| 1945 | /// assert!(abs_difference_x <= f32::EPSILON); |
| 1946 | /// assert!(abs_difference_y <= f32::EPSILON); |
| 1947 | /// ``` |
| 1948 | /// |
| 1949 | /// _This standalone function is for testing only. |
| 1950 | /// It will be stabilized as an inherent method._ |
| 1951 | /// |
| 1952 | /// [`f32::abs_sub`]: ../../../std/primitive.f32.html#method.abs_sub |
| 1953 | #[inline ] |
| 1954 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 1955 | #[deprecated ( |
| 1956 | since = "1.10.0" , |
| 1957 | note = "you probably meant `(self - other).abs()`: \ |
| 1958 | this operation is `(self - other).max(0.0)` \ |
| 1959 | except that `abs_sub` also propagates NaNs (also \ |
| 1960 | known as `fdimf` in C). If you truly need the positive \ |
| 1961 | difference, consider using that expression or the C function \ |
| 1962 | `fdimf`, depending on how you wish to handle NaN (please consider \ |
| 1963 | filing an issue describing your use-case too)." |
| 1964 | )] |
| 1965 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 1966 | pub fn abs_sub(x: f32, other: f32) -> f32 { |
| 1967 | libm::fdimf(x, other) |
| 1968 | } |
| 1969 | |
| 1970 | /// Experimental version of `cbrt` in `core`. See [`f32::cbrt`] for details. |
| 1971 | /// |
| 1972 | /// # Unspecified precision |
| 1973 | /// |
| 1974 | /// The precision of this function is non-deterministic. This means it varies by platform, Rust version, and |
| 1975 | /// can even differ within the same execution from one invocation to the next. |
| 1976 | /// This function currently corresponds to the `cbrtf` from libc on Unix |
| 1977 | /// and Windows. Note that this might change in the future. |
| 1978 | /// |
| 1979 | /// # Examples |
| 1980 | /// |
| 1981 | /// ``` |
| 1982 | /// #![feature(core_float_math)] |
| 1983 | /// |
| 1984 | /// use core::f32; |
| 1985 | /// |
| 1986 | /// let x = 8.0f32; |
| 1987 | /// |
| 1988 | /// // x^(1/3) - 2 == 0 |
| 1989 | /// let abs_difference = (f32::math::cbrt(x) - 2.0).abs(); |
| 1990 | /// |
| 1991 | /// assert!(abs_difference <= f32::EPSILON); |
| 1992 | /// ``` |
| 1993 | /// |
| 1994 | /// _This standalone function is for testing only. |
| 1995 | /// It will be stabilized as an inherent method._ |
| 1996 | /// |
| 1997 | /// [`f32::cbrt`]: ../../../std/primitive.f32.html#method.cbrt |
| 1998 | #[inline ] |
| 1999 | #[must_use = "method returns a new number and does not mutate the original value" ] |
| 2000 | #[unstable (feature = "core_float_math" , issue = "137578" )] |
| 2001 | pub fn cbrt(x: f32) -> f32 { |
| 2002 | libm::cbrtf(x) |
| 2003 | } |
| 2004 | } |
| 2005 | |