core/num/
f64.rs

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