Skip to main content

core/num/
f128.rs

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