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