]> git.lizzy.rs Git - rust.git/blob - library/core/src/num/f64.rs
Rollup merge of #79293 - Havvy:test-eval-order-compound-assign, r=Mark-Simulacrum
[rust.git] / library / core / src / num / f64.rs
1 //! This module provides constants which are specific to the implementation
2 //! of the `f64` floating point data type.
3 //!
4 //! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
5 //!
6 //! Mathematically significant numbers are provided in the `consts` sub-module.
7 //!
8 //! Although using these constants won’t cause compilation warnings,
9 //! new code should use the associated constants directly on the primitive type.
10
11 #![stable(feature = "rust1", since = "1.0.0")]
12
13 use crate::convert::FloatToInt;
14 #[cfg(not(test))]
15 use crate::intrinsics;
16 use crate::mem;
17 use crate::num::FpCategory;
18
19 /// The radix or base of the internal representation of `f64`.
20 /// Use [`f64::RADIX`](../../std/primitive.f64.html#associatedconstant.RADIX) instead.
21 ///
22 /// # Examples
23 ///
24 /// ```rust
25 /// // deprecated way
26 /// let r = std::f64::RADIX;
27 ///
28 /// // intended way
29 /// let r = f64::RADIX;
30 /// ```
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub const RADIX: u32 = f64::RADIX;
33
34 /// Number of significant digits in base 2.
35 /// Use [`f64::MANTISSA_DIGITS`](../../std/primitive.f64.html#associatedconstant.MANTISSA_DIGITS) instead.
36 ///
37 /// # Examples
38 ///
39 /// ```rust
40 /// // deprecated way
41 /// let d = std::f64::MANTISSA_DIGITS;
42 ///
43 /// // intended way
44 /// let d = f64::MANTISSA_DIGITS;
45 /// ```
46 #[stable(feature = "rust1", since = "1.0.0")]
47 pub const MANTISSA_DIGITS: u32 = f64::MANTISSA_DIGITS;
48
49 /// Approximate number of significant digits in base 10.
50 /// Use [`f64::DIGITS`](../../std/primitive.f64.html#associatedconstant.DIGITS) instead.
51 ///
52 /// # Examples
53 ///
54 /// ```rust
55 /// // deprecated way
56 /// let d = std::f64::DIGITS;
57 ///
58 /// // intended way
59 /// let d = f64::DIGITS;
60 /// ```
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub const DIGITS: u32 = f64::DIGITS;
63
64 /// [Machine epsilon] value for `f64`.
65 /// Use [`f64::EPSILON`](../../std/primitive.f64.html#associatedconstant.EPSILON) instead.
66 ///
67 /// This is the difference between `1.0` and the next larger representable number.
68 ///
69 /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
70 ///
71 /// # Examples
72 ///
73 /// ```rust
74 /// // deprecated way
75 /// let e = std::f64::EPSILON;
76 ///
77 /// // intended way
78 /// let e = f64::EPSILON;
79 /// ```
80 #[stable(feature = "rust1", since = "1.0.0")]
81 pub const EPSILON: f64 = f64::EPSILON;
82
83 /// Smallest finite `f64` value.
84 /// Use [`f64::MIN`](../../std/primitive.f64.html#associatedconstant.MIN) instead.
85 ///
86 /// # Examples
87 ///
88 /// ```rust
89 /// // deprecated way
90 /// let min = std::f64::MIN;
91 ///
92 /// // intended way
93 /// let min = f64::MIN;
94 /// ```
95 #[stable(feature = "rust1", since = "1.0.0")]
96 pub const MIN: f64 = f64::MIN;
97
98 /// Smallest positive normal `f64` value.
99 /// Use [`f64::MIN_POSITIVE`](../../std/primitive.f64.html#associatedconstant.MIN_POSITIVE) instead.
100 ///
101 /// # Examples
102 ///
103 /// ```rust
104 /// // deprecated way
105 /// let min = std::f64::MIN_POSITIVE;
106 ///
107 /// // intended way
108 /// let min = f64::MIN_POSITIVE;
109 /// ```
110 #[stable(feature = "rust1", since = "1.0.0")]
111 pub const MIN_POSITIVE: f64 = f64::MIN_POSITIVE;
112
113 /// Largest finite `f64` value.
114 /// Use [`f64::MAX`](../../std/primitive.f64.html#associatedconstant.MAX) instead.
115 ///
116 /// # Examples
117 ///
118 /// ```rust
119 /// // deprecated way
120 /// let max = std::f64::MAX;
121 ///
122 /// // intended way
123 /// let max = f64::MAX;
124 /// ```
125 #[stable(feature = "rust1", since = "1.0.0")]
126 pub const MAX: f64 = f64::MAX;
127
128 /// One greater than the minimum possible normal power of 2 exponent.
129 /// Use [`f64::MIN_EXP`](../../std/primitive.f64.html#associatedconstant.MIN_EXP) instead.
130 ///
131 /// # Examples
132 ///
133 /// ```rust
134 /// // deprecated way
135 /// let min = std::f64::MIN_EXP;
136 ///
137 /// // intended way
138 /// let min = f64::MIN_EXP;
139 /// ```
140 #[stable(feature = "rust1", since = "1.0.0")]
141 pub const MIN_EXP: i32 = f64::MIN_EXP;
142
143 /// Maximum possible power of 2 exponent.
144 /// Use [`f64::MAX_EXP`](../../std/primitive.f64.html#associatedconstant.MAX_EXP) instead.
145 ///
146 /// # Examples
147 ///
148 /// ```rust
149 /// // deprecated way
150 /// let max = std::f64::MAX_EXP;
151 ///
152 /// // intended way
153 /// let max = f64::MAX_EXP;
154 /// ```
155 #[stable(feature = "rust1", since = "1.0.0")]
156 pub const MAX_EXP: i32 = f64::MAX_EXP;
157
158 /// Minimum possible normal power of 10 exponent.
159 /// Use [`f64::MIN_10_EXP`](../../std/primitive.f64.html#associatedconstant.MIN_10_EXP) instead.
160 ///
161 /// # Examples
162 ///
163 /// ```rust
164 /// // deprecated way
165 /// let min = std::f64::MIN_10_EXP;
166 ///
167 /// // intended way
168 /// let min = f64::MIN_10_EXP;
169 /// ```
170 #[stable(feature = "rust1", since = "1.0.0")]
171 pub const MIN_10_EXP: i32 = f64::MIN_10_EXP;
172
173 /// Maximum possible power of 10 exponent.
174 /// Use [`f64::MAX_10_EXP`](../../std/primitive.f64.html#associatedconstant.MAX_10_EXP) instead.
175 ///
176 /// # Examples
177 ///
178 /// ```rust
179 /// // deprecated way
180 /// let max = std::f64::MAX_10_EXP;
181 ///
182 /// // intended way
183 /// let max = f64::MAX_10_EXP;
184 /// ```
185 #[stable(feature = "rust1", since = "1.0.0")]
186 pub const MAX_10_EXP: i32 = f64::MAX_10_EXP;
187
188 /// Not a Number (NaN).
189 /// Use [`f64::NAN`](../../std/primitive.f64.html#associatedconstant.NAN) instead.
190 ///
191 /// # Examples
192 ///
193 /// ```rust
194 /// // deprecated way
195 /// let nan = std::f64::NAN;
196 ///
197 /// // intended way
198 /// let nan = f64::NAN;
199 /// ```
200 #[stable(feature = "rust1", since = "1.0.0")]
201 pub const NAN: f64 = f64::NAN;
202
203 /// Infinity (∞).
204 /// Use [`f64::INFINITY`](../../std/primitive.f64.html#associatedconstant.INFINITY) instead.
205 ///
206 /// # Examples
207 ///
208 /// ```rust
209 /// // deprecated way
210 /// let inf = std::f64::INFINITY;
211 ///
212 /// // intended way
213 /// let inf = f64::INFINITY;
214 /// ```
215 #[stable(feature = "rust1", since = "1.0.0")]
216 pub const INFINITY: f64 = f64::INFINITY;
217
218 /// Negative infinity (−∞).
219 /// Use [`f64::NEG_INFINITY`](../../std/primitive.f64.html#associatedconstant.NEG_INFINITY) instead.
220 ///
221 /// # Examples
222 ///
223 /// ```rust
224 /// // deprecated way
225 /// let ninf = std::f64::NEG_INFINITY;
226 ///
227 /// // intended way
228 /// let ninf = f64::NEG_INFINITY;
229 /// ```
230 #[stable(feature = "rust1", since = "1.0.0")]
231 pub const NEG_INFINITY: f64 = f64::NEG_INFINITY;
232
233 /// Basic mathematical constants.
234 #[stable(feature = "rust1", since = "1.0.0")]
235 pub mod consts {
236     // FIXME: replace with mathematical constants from cmath.
237
238     /// Archimedes' constant (π)
239     #[stable(feature = "rust1", since = "1.0.0")]
240     pub const PI: f64 = 3.14159265358979323846264338327950288_f64;
241
242     /// The full circle constant (τ)
243     ///
244     /// Equal to 2π.
245     #[stable(feature = "tau_constant", since = "1.47.0")]
246     pub const TAU: f64 = 6.28318530717958647692528676655900577_f64;
247
248     /// π/2
249     #[stable(feature = "rust1", since = "1.0.0")]
250     pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
251
252     /// π/3
253     #[stable(feature = "rust1", since = "1.0.0")]
254     pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
255
256     /// π/4
257     #[stable(feature = "rust1", since = "1.0.0")]
258     pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
259
260     /// π/6
261     #[stable(feature = "rust1", since = "1.0.0")]
262     pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
263
264     /// π/8
265     #[stable(feature = "rust1", since = "1.0.0")]
266     pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
267
268     /// 1/π
269     #[stable(feature = "rust1", since = "1.0.0")]
270     pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
271
272     /// 2/π
273     #[stable(feature = "rust1", since = "1.0.0")]
274     pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
275
276     /// 2/sqrt(π)
277     #[stable(feature = "rust1", since = "1.0.0")]
278     pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64;
279
280     /// sqrt(2)
281     #[stable(feature = "rust1", since = "1.0.0")]
282     pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64;
283
284     /// 1/sqrt(2)
285     #[stable(feature = "rust1", since = "1.0.0")]
286     pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64;
287
288     /// Euler's number (e)
289     #[stable(feature = "rust1", since = "1.0.0")]
290     pub const E: f64 = 2.71828182845904523536028747135266250_f64;
291
292     /// log<sub>2</sub>(10)
293     #[stable(feature = "extra_log_consts", since = "1.43.0")]
294     pub const LOG2_10: f64 = 3.32192809488736234787031942948939018_f64;
295
296     /// log<sub>2</sub>(e)
297     #[stable(feature = "rust1", since = "1.0.0")]
298     pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
299
300     /// log<sub>10</sub>(2)
301     #[stable(feature = "extra_log_consts", since = "1.43.0")]
302     pub const LOG10_2: f64 = 0.301029995663981195213738894724493027_f64;
303
304     /// log<sub>10</sub>(e)
305     #[stable(feature = "rust1", since = "1.0.0")]
306     pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
307
308     /// ln(2)
309     #[stable(feature = "rust1", since = "1.0.0")]
310     pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64;
311
312     /// ln(10)
313     #[stable(feature = "rust1", since = "1.0.0")]
314     pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64;
315 }
316
317 #[lang = "f64"]
318 #[cfg(not(test))]
319 impl f64 {
320     /// The radix or base of the internal representation of `f64`.
321     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
322     pub const RADIX: u32 = 2;
323
324     /// Number of significant digits in base 2.
325     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
326     pub const MANTISSA_DIGITS: u32 = 53;
327     /// Approximate number of significant digits in base 10.
328     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
329     pub const DIGITS: u32 = 15;
330
331     /// [Machine epsilon] value for `f64`.
332     ///
333     /// This is the difference between `1.0` and the next larger representable number.
334     ///
335     /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
336     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
337     pub const EPSILON: f64 = 2.2204460492503131e-16_f64;
338
339     /// Smallest finite `f64` value.
340     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
341     pub const MIN: f64 = -1.7976931348623157e+308_f64;
342     /// Smallest positive normal `f64` value.
343     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
344     pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64;
345     /// Largest finite `f64` value.
346     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
347     pub const MAX: f64 = 1.7976931348623157e+308_f64;
348
349     /// One greater than the minimum possible normal power of 2 exponent.
350     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
351     pub const MIN_EXP: i32 = -1021;
352     /// Maximum possible power of 2 exponent.
353     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
354     pub const MAX_EXP: i32 = 1024;
355
356     /// Minimum possible normal power of 10 exponent.
357     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
358     pub const MIN_10_EXP: i32 = -307;
359     /// Maximum possible power of 10 exponent.
360     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
361     pub const MAX_10_EXP: i32 = 308;
362
363     /// Not a Number (NaN).
364     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
365     pub const NAN: f64 = 0.0_f64 / 0.0_f64;
366     /// Infinity (∞).
367     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
368     pub const INFINITY: f64 = 1.0_f64 / 0.0_f64;
369     /// Negative infinity (−∞).
370     #[stable(feature = "assoc_int_consts", since = "1.43.0")]
371     pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64;
372
373     /// Returns `true` if this value is `NaN`.
374     ///
375     /// ```
376     /// let nan = f64::NAN;
377     /// let f = 7.0_f64;
378     ///
379     /// assert!(nan.is_nan());
380     /// assert!(!f.is_nan());
381     /// ```
382     #[stable(feature = "rust1", since = "1.0.0")]
383     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
384     #[inline]
385     pub const fn is_nan(self) -> bool {
386         self != self
387     }
388
389     // FIXME(#50145): `abs` is publicly unavailable in libcore due to
390     // concerns about portability, so this implementation is for
391     // private use internally.
392     #[inline]
393     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
394     const fn abs_private(self) -> f64 {
395         f64::from_bits(self.to_bits() & 0x7fff_ffff_ffff_ffff)
396     }
397
398     /// Returns `true` if this value is positive infinity or negative infinity, and
399     /// `false` otherwise.
400     ///
401     /// ```
402     /// let f = 7.0f64;
403     /// let inf = f64::INFINITY;
404     /// let neg_inf = f64::NEG_INFINITY;
405     /// let nan = f64::NAN;
406     ///
407     /// assert!(!f.is_infinite());
408     /// assert!(!nan.is_infinite());
409     ///
410     /// assert!(inf.is_infinite());
411     /// assert!(neg_inf.is_infinite());
412     /// ```
413     #[stable(feature = "rust1", since = "1.0.0")]
414     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
415     #[inline]
416     pub const fn is_infinite(self) -> bool {
417         self.abs_private() == Self::INFINITY
418     }
419
420     /// Returns `true` if this number is neither infinite nor `NaN`.
421     ///
422     /// ```
423     /// let f = 7.0f64;
424     /// let inf: f64 = f64::INFINITY;
425     /// let neg_inf: f64 = f64::NEG_INFINITY;
426     /// let nan: f64 = f64::NAN;
427     ///
428     /// assert!(f.is_finite());
429     ///
430     /// assert!(!nan.is_finite());
431     /// assert!(!inf.is_finite());
432     /// assert!(!neg_inf.is_finite());
433     /// ```
434     #[stable(feature = "rust1", since = "1.0.0")]
435     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
436     #[inline]
437     pub const fn is_finite(self) -> bool {
438         // There's no need to handle NaN separately: if self is NaN,
439         // the comparison is not true, exactly as desired.
440         self.abs_private() < Self::INFINITY
441     }
442
443     /// Returns `true` if the number is [subnormal].
444     ///
445     /// ```
446     /// #![feature(is_subnormal)]
447     /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308_f64
448     /// let max = f64::MAX;
449     /// let lower_than_min = 1.0e-308_f64;
450     /// let zero = 0.0_f64;
451     ///
452     /// assert!(!min.is_subnormal());
453     /// assert!(!max.is_subnormal());
454     ///
455     /// assert!(!zero.is_subnormal());
456     /// assert!(!f64::NAN.is_subnormal());
457     /// assert!(!f64::INFINITY.is_subnormal());
458     /// // Values between `0` and `min` are Subnormal.
459     /// assert!(lower_than_min.is_subnormal());
460     /// ```
461     /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
462     #[unstable(feature = "is_subnormal", issue = "79288")]
463     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
464     #[inline]
465     pub const fn is_subnormal(self) -> bool {
466         matches!(self.classify(), FpCategory::Subnormal)
467     }
468
469     /// Returns `true` if the number is neither zero, infinite,
470     /// [subnormal], or `NaN`.
471     ///
472     /// ```
473     /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
474     /// let max = f64::MAX;
475     /// let lower_than_min = 1.0e-308_f64;
476     /// let zero = 0.0f64;
477     ///
478     /// assert!(min.is_normal());
479     /// assert!(max.is_normal());
480     ///
481     /// assert!(!zero.is_normal());
482     /// assert!(!f64::NAN.is_normal());
483     /// assert!(!f64::INFINITY.is_normal());
484     /// // Values between `0` and `min` are Subnormal.
485     /// assert!(!lower_than_min.is_normal());
486     /// ```
487     /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
488     #[stable(feature = "rust1", since = "1.0.0")]
489     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
490     #[inline]
491     pub const fn is_normal(self) -> bool {
492         matches!(self.classify(), FpCategory::Normal)
493     }
494
495     /// Returns the floating point category of the number. If only one property
496     /// is going to be tested, it is generally faster to use the specific
497     /// predicate instead.
498     ///
499     /// ```
500     /// use std::num::FpCategory;
501     ///
502     /// let num = 12.4_f64;
503     /// let inf = f64::INFINITY;
504     ///
505     /// assert_eq!(num.classify(), FpCategory::Normal);
506     /// assert_eq!(inf.classify(), FpCategory::Infinite);
507     /// ```
508     #[stable(feature = "rust1", since = "1.0.0")]
509     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
510     pub const fn classify(self) -> FpCategory {
511         const EXP_MASK: u64 = 0x7ff0000000000000;
512         const MAN_MASK: u64 = 0x000fffffffffffff;
513
514         let bits = self.to_bits();
515         match (bits & MAN_MASK, bits & EXP_MASK) {
516             (0, 0) => FpCategory::Zero,
517             (_, 0) => FpCategory::Subnormal,
518             (0, EXP_MASK) => FpCategory::Infinite,
519             (_, EXP_MASK) => FpCategory::Nan,
520             _ => FpCategory::Normal,
521         }
522     }
523
524     /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with
525     /// positive sign bit and positive infinity.
526     ///
527     /// ```
528     /// let f = 7.0_f64;
529     /// let g = -7.0_f64;
530     ///
531     /// assert!(f.is_sign_positive());
532     /// assert!(!g.is_sign_positive());
533     /// ```
534     #[stable(feature = "rust1", since = "1.0.0")]
535     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
536     #[inline]
537     pub const fn is_sign_positive(self) -> bool {
538         !self.is_sign_negative()
539     }
540
541     #[stable(feature = "rust1", since = "1.0.0")]
542     #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
543     #[inline]
544     #[doc(hidden)]
545     pub fn is_positive(self) -> bool {
546         self.is_sign_positive()
547     }
548
549     /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with
550     /// negative sign bit and negative infinity.
551     ///
552     /// ```
553     /// let f = 7.0_f64;
554     /// let g = -7.0_f64;
555     ///
556     /// assert!(!f.is_sign_negative());
557     /// assert!(g.is_sign_negative());
558     /// ```
559     #[stable(feature = "rust1", since = "1.0.0")]
560     #[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
561     #[inline]
562     pub const fn is_sign_negative(self) -> bool {
563         self.to_bits() & 0x8000_0000_0000_0000 != 0
564     }
565
566     #[stable(feature = "rust1", since = "1.0.0")]
567     #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
568     #[inline]
569     #[doc(hidden)]
570     pub fn is_negative(self) -> bool {
571         self.is_sign_negative()
572     }
573
574     /// Takes the reciprocal (inverse) of a number, `1/x`.
575     ///
576     /// ```
577     /// let x = 2.0_f64;
578     /// let abs_difference = (x.recip() - (1.0 / x)).abs();
579     ///
580     /// assert!(abs_difference < 1e-10);
581     /// ```
582     #[stable(feature = "rust1", since = "1.0.0")]
583     #[inline]
584     pub fn recip(self) -> f64 {
585         1.0 / self
586     }
587
588     /// Converts radians to degrees.
589     ///
590     /// ```
591     /// let angle = std::f64::consts::PI;
592     ///
593     /// let abs_difference = (angle.to_degrees() - 180.0).abs();
594     ///
595     /// assert!(abs_difference < 1e-10);
596     /// ```
597     #[stable(feature = "rust1", since = "1.0.0")]
598     #[inline]
599     pub fn to_degrees(self) -> f64 {
600         // The division here is correctly rounded with respect to the true
601         // value of 180/π. (This differs from f32, where a constant must be
602         // used to ensure a correctly rounded result.)
603         self * (180.0f64 / consts::PI)
604     }
605
606     /// Converts degrees to radians.
607     ///
608     /// ```
609     /// let angle = 180.0_f64;
610     ///
611     /// let abs_difference = (angle.to_radians() - std::f64::consts::PI).abs();
612     ///
613     /// assert!(abs_difference < 1e-10);
614     /// ```
615     #[stable(feature = "rust1", since = "1.0.0")]
616     #[inline]
617     pub fn to_radians(self) -> f64 {
618         let value: f64 = consts::PI;
619         self * (value / 180.0)
620     }
621
622     /// Returns the maximum of the two numbers.
623     ///
624     /// ```
625     /// let x = 1.0_f64;
626     /// let y = 2.0_f64;
627     ///
628     /// assert_eq!(x.max(y), y);
629     /// ```
630     ///
631     /// If one of the arguments is NaN, then the other argument is returned.
632     #[stable(feature = "rust1", since = "1.0.0")]
633     #[inline]
634     pub fn max(self, other: f64) -> f64 {
635         intrinsics::maxnumf64(self, other)
636     }
637
638     /// Returns the minimum of the two numbers.
639     ///
640     /// ```
641     /// let x = 1.0_f64;
642     /// let y = 2.0_f64;
643     ///
644     /// assert_eq!(x.min(y), x);
645     /// ```
646     ///
647     /// If one of the arguments is NaN, then the other argument is returned.
648     #[stable(feature = "rust1", since = "1.0.0")]
649     #[inline]
650     pub fn min(self, other: f64) -> f64 {
651         intrinsics::minnumf64(self, other)
652     }
653
654     /// Rounds toward zero and converts to any primitive integer type,
655     /// assuming that the value is finite and fits in that type.
656     ///
657     /// ```
658     /// let value = 4.6_f64;
659     /// let rounded = unsafe { value.to_int_unchecked::<u16>() };
660     /// assert_eq!(rounded, 4);
661     ///
662     /// let value = -128.9_f64;
663     /// let rounded = unsafe { value.to_int_unchecked::<i8>() };
664     /// assert_eq!(rounded, i8::MIN);
665     /// ```
666     ///
667     /// # Safety
668     ///
669     /// The value must:
670     ///
671     /// * Not be `NaN`
672     /// * Not be infinite
673     /// * Be representable in the return type `Int`, after truncating off its fractional part
674     #[stable(feature = "float_approx_unchecked_to", since = "1.44.0")]
675     #[inline]
676     pub unsafe fn to_int_unchecked<Int>(self) -> Int
677     where
678         Self: FloatToInt<Int>,
679     {
680         // SAFETY: the caller must uphold the safety contract for
681         // `FloatToInt::to_int_unchecked`.
682         unsafe { FloatToInt::<Int>::to_int_unchecked(self) }
683     }
684
685     /// Raw transmutation to `u64`.
686     ///
687     /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
688     ///
689     /// See `from_bits` for some discussion of the portability of this operation
690     /// (there are almost no issues).
691     ///
692     /// Note that this function is distinct from `as` casting, which attempts to
693     /// preserve the *numeric* value, and not the bitwise value.
694     ///
695     /// # Examples
696     ///
697     /// ```
698     /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
699     /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
700     ///
701     /// ```
702     #[stable(feature = "float_bits_conv", since = "1.20.0")]
703     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
704     #[inline]
705     pub const fn to_bits(self) -> u64 {
706         // SAFETY: `u64` is a plain old datatype so we can always transmute to it
707         unsafe { mem::transmute(self) }
708     }
709
710     /// Raw transmutation from `u64`.
711     ///
712     /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
713     /// It turns out this is incredibly portable, for two reasons:
714     ///
715     /// * Floats and Ints have the same endianness on all supported platforms.
716     /// * IEEE-754 very precisely specifies the bit layout of floats.
717     ///
718     /// However there is one caveat: prior to the 2008 version of IEEE-754, how
719     /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
720     /// (notably x86 and ARM) picked the interpretation that was ultimately
721     /// standardized in 2008, but some didn't (notably MIPS). As a result, all
722     /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
723     ///
724     /// Rather than trying to preserve signaling-ness cross-platform, this
725     /// implementation favors preserving the exact bits. This means that
726     /// any payloads encoded in NaNs will be preserved even if the result of
727     /// this method is sent over the network from an x86 machine to a MIPS one.
728     ///
729     /// If the results of this method are only manipulated by the same
730     /// architecture that produced them, then there is no portability concern.
731     ///
732     /// If the input isn't NaN, then there is no portability concern.
733     ///
734     /// If you don't care about signaling-ness (very likely), then there is no
735     /// portability concern.
736     ///
737     /// Note that this function is distinct from `as` casting, which attempts to
738     /// preserve the *numeric* value, and not the bitwise value.
739     ///
740     /// # Examples
741     ///
742     /// ```
743     /// let v = f64::from_bits(0x4029000000000000);
744     /// assert_eq!(v, 12.5);
745     /// ```
746     #[stable(feature = "float_bits_conv", since = "1.20.0")]
747     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
748     #[inline]
749     pub const fn from_bits(v: u64) -> Self {
750         // SAFETY: `u64` is a plain old datatype so we can always transmute from it
751         // It turns out the safety issues with sNaN were overblown! Hooray!
752         unsafe { mem::transmute(v) }
753     }
754
755     /// Return the memory representation of this floating point number as a byte array in
756     /// big-endian (network) byte order.
757     ///
758     /// # Examples
759     ///
760     /// ```
761     /// let bytes = 12.5f64.to_be_bytes();
762     /// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
763     /// ```
764     #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
765     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
766     #[inline]
767     pub const fn to_be_bytes(self) -> [u8; 8] {
768         self.to_bits().to_be_bytes()
769     }
770
771     /// Return the memory representation of this floating point number as a byte array in
772     /// little-endian byte order.
773     ///
774     /// # Examples
775     ///
776     /// ```
777     /// let bytes = 12.5f64.to_le_bytes();
778     /// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
779     /// ```
780     #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
781     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
782     #[inline]
783     pub const fn to_le_bytes(self) -> [u8; 8] {
784         self.to_bits().to_le_bytes()
785     }
786
787     /// Return the memory representation of this floating point number as a byte array in
788     /// native byte order.
789     ///
790     /// As the target platform's native endianness is used, portable code
791     /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
792     ///
793     /// [`to_be_bytes`]: #method.to_be_bytes
794     /// [`to_le_bytes`]: #method.to_le_bytes
795     ///
796     /// # Examples
797     ///
798     /// ```
799     /// let bytes = 12.5f64.to_ne_bytes();
800     /// assert_eq!(
801     ///     bytes,
802     ///     if cfg!(target_endian = "big") {
803     ///         [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
804     ///     } else {
805     ///         [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
806     ///     }
807     /// );
808     /// ```
809     #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
810     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
811     #[inline]
812     pub const fn to_ne_bytes(self) -> [u8; 8] {
813         self.to_bits().to_ne_bytes()
814     }
815
816     /// Return the memory representation of this floating point number as a byte array in
817     /// native byte order.
818     ///
819     /// [`to_ne_bytes`] should be preferred over this whenever possible.
820     ///
821     /// [`to_ne_bytes`]: #method.to_ne_bytes
822     ///
823     /// # Examples
824     ///
825     /// ```
826     /// #![feature(num_as_ne_bytes)]
827     /// let num = 12.5f64;
828     /// let bytes = num.as_ne_bytes();
829     /// assert_eq!(
830     ///     bytes,
831     ///     if cfg!(target_endian = "big") {
832     ///         &[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
833     ///     } else {
834     ///         &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
835     ///     }
836     /// );
837     /// ```
838     #[unstable(feature = "num_as_ne_bytes", issue = "76976")]
839     #[inline]
840     pub fn as_ne_bytes(&self) -> &[u8; 8] {
841         // SAFETY: `f64` is a plain old datatype so we can always transmute to it
842         unsafe { &*(self as *const Self as *const _) }
843     }
844
845     /// Create a floating point value from its representation as a byte array in big endian.
846     ///
847     /// # Examples
848     ///
849     /// ```
850     /// let value = f64::from_be_bytes([0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
851     /// assert_eq!(value, 12.5);
852     /// ```
853     #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
854     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
855     #[inline]
856     pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
857         Self::from_bits(u64::from_be_bytes(bytes))
858     }
859
860     /// Create a floating point value from its representation as a byte array in little endian.
861     ///
862     /// # Examples
863     ///
864     /// ```
865     /// let value = f64::from_le_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
866     /// assert_eq!(value, 12.5);
867     /// ```
868     #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
869     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
870     #[inline]
871     pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
872         Self::from_bits(u64::from_le_bytes(bytes))
873     }
874
875     /// Create a floating point value from its representation as a byte array in native endian.
876     ///
877     /// As the target platform's native endianness is used, portable code
878     /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
879     /// appropriate instead.
880     ///
881     /// [`from_be_bytes`]: #method.from_be_bytes
882     /// [`from_le_bytes`]: #method.from_le_bytes
883     ///
884     /// # Examples
885     ///
886     /// ```
887     /// let value = f64::from_ne_bytes(if cfg!(target_endian = "big") {
888     ///     [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
889     /// } else {
890     ///     [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
891     /// });
892     /// assert_eq!(value, 12.5);
893     /// ```
894     #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
895     #[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
896     #[inline]
897     pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
898         Self::from_bits(u64::from_ne_bytes(bytes))
899     }
900
901     /// Returns an ordering between self and other values.
902     /// Unlike the standard partial comparison between floating point numbers,
903     /// this comparison always produces an ordering in accordance to
904     /// the totalOrder predicate as defined in IEEE 754 (2008 revision)
905     /// floating point standard. The values are ordered in following order:
906     /// - Negative quiet NaN
907     /// - Negative signaling NaN
908     /// - Negative infinity
909     /// - Negative numbers
910     /// - Negative subnormal numbers
911     /// - Negative zero
912     /// - Positive zero
913     /// - Positive subnormal numbers
914     /// - Positive numbers
915     /// - Positive infinity
916     /// - Positive signaling NaN
917     /// - Positive quiet NaN
918     ///
919     /// Note that this function does not always agree with the [`PartialOrd`]
920     /// and [`PartialEq`] implementations of `f64`. In particular, they regard
921     /// negative and positive zero as equal, while `total_cmp` doesn't.
922     ///
923     /// # Example
924     /// ```
925     /// #![feature(total_cmp)]
926     /// struct GoodBoy {
927     ///     name: String,
928     ///     weight: f64,
929     /// }
930     ///
931     /// let mut bois = vec![
932     ///     GoodBoy { name: "Pucci".to_owned(), weight: 0.1 },
933     ///     GoodBoy { name: "Woofer".to_owned(), weight: 99.0 },
934     ///     GoodBoy { name: "Yapper".to_owned(), weight: 10.0 },
935     ///     GoodBoy { name: "Chonk".to_owned(), weight: f64::INFINITY },
936     ///     GoodBoy { name: "Abs. Unit".to_owned(), weight: f64::NAN },
937     ///     GoodBoy { name: "Floaty".to_owned(), weight: -5.0 },
938     /// ];
939     ///
940     /// bois.sort_by(|a, b| a.weight.total_cmp(&b.weight));
941     /// # assert!(bois.into_iter().map(|b| b.weight)
942     /// #     .zip([-5.0, 0.1, 10.0, 99.0, f64::INFINITY, f64::NAN].iter())
943     /// #     .all(|(a, b)| a.to_bits() == b.to_bits()))
944     /// ```
945     #[unstable(feature = "total_cmp", issue = "72599")]
946     #[inline]
947     pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering {
948         let mut left = self.to_bits() as i64;
949         let mut right = other.to_bits() as i64;
950
951         // In case of negatives, flip all the bits except the sign
952         // to achieve a similar layout as two's complement integers
953         //
954         // Why does this work? IEEE 754 floats consist of three fields:
955         // Sign bit, exponent and mantissa. The set of exponent and mantissa
956         // fields as a whole have the property that their bitwise order is
957         // equal to the numeric magnitude where the magnitude is defined.
958         // The magnitude is not normally defined on NaN values, but
959         // IEEE 754 totalOrder defines the NaN values also to follow the
960         // bitwise order. This leads to order explained in the doc comment.
961         // However, the representation of magnitude is the same for negative
962         // and positive numbers – only the sign bit is different.
963         // To easily compare the floats as signed integers, we need to
964         // flip the exponent and mantissa bits in case of negative numbers.
965         // We effectively convert the numbers to "two's complement" form.
966         //
967         // To do the flipping, we construct a mask and XOR against it.
968         // We branchlessly calculate an "all-ones except for the sign bit"
969         // mask from negative-signed values: right shifting sign-extends
970         // the integer, so we "fill" the mask with sign bits, and then
971         // convert to unsigned to push one more zero bit.
972         // On positive values, the mask is all zeros, so it's a no-op.
973         left ^= (((left >> 63) as u64) >> 1) as i64;
974         right ^= (((right >> 63) as u64) >> 1) as i64;
975
976         left.cmp(&right)
977     }
978 }