Primitive Typef128
f128 #116909)Expand description
A 128-bit floating-point type (specifically, the “binary128” type defined in IEEE 754-2008).
This type is very similar tof32 andf64, but has increased precision by using twiceas many bits asf64. Please seethe documentation forf32 orWikipedia onquad-precision values for more information.
Note that no platforms have hardware support forf128 without enabling target specific features,as for all instruction set architecturesf128 is considered an optional feature. Only Power ISA(“PowerPC”) and RISC-V (via the Q extension) specify it, and only certain microarchitecturesactually implement it. For x86-64 and AArch64, ISA support is not even specified, so it will alwaysbe a software implementation significantly slower thanf64.
Note:f128 support is incomplete. Many platforms will not be able to link math functions. Onx86 in particular, these functions do link but their results are always incorrect.
Implementations§
Source§implf128
implf128
Sourcepub fnpowf(self, n:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnpowf(self, n:f128) ->f128
f128 #116909)Raises a number to a floating point power.
Note that this function is special in that it can return non-NaN results for NaN inputs. Forexample,f128::powf(f128::NAN, 0.0) returns1.0. However, if an input is asignalingNaN, then the result is non-deterministically either a NaN or the result that thecorresponding quiet NaN would produce.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
§Examples
Sourcepub fnexp(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnexp(self) ->f128
f128 #116909)Sourcepub fnexp2(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnexp2(self) ->f128
f128 #116909)Sourcepub fnln(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnln(self) ->f128
f128 #116909)Returns the natural logarithm of the number.
This returns NaN when the number is negative, and negative infinity when number is zero.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
§Examples
#![feature(f128)]letone =1.0f128;// e^1lete = one.exp();// ln(e) - 1 == 0letabs_difference = (e.ln() -1.0).abs();assert!(abs_difference <= f128::EPSILON);Non-positive values:
Sourcepub fnlog(self, base:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnlog(self, base:f128) ->f128
f128 #116909)Returns the logarithm of the number with respect to an arbitrary base.
This returns NaN when the number is negative, and negative infinity when number is zero.
The result might not be correctly rounded owing to implementation details;self.log2() can produce more accurate results for base 2, andself.log10() can produce more accurate results for base 10.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
§Examples
#![feature(f128)]letfive =5.0f128;// log5(5) - 1 == 0letabs_difference = (five.log(5.0) -1.0).abs();assert!(abs_difference <= f128::EPSILON);Non-positive values:
Sourcepub fnlog2(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnlog2(self) ->f128
f128 #116909)Returns the base 2 logarithm of the number.
This returns NaN when the number is negative, and negative infinity when number is zero.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
§Examples
#![feature(f128)]lettwo =2.0f128;// log2(2) - 1 == 0letabs_difference = (two.log2() -1.0).abs();assert!(abs_difference <= f128::EPSILON);Non-positive values:
Sourcepub fnlog10(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnlog10(self) ->f128
f128 #116909)Returns the base 10 logarithm of the number.
This returns NaN when the number is negative, and negative infinity when number is zero.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
§Examples
#![feature(f128)]letten =10.0f128;// log10(10) - 1 == 0letabs_difference = (ten.log10() -1.0).abs();assert!(abs_difference <= f128::EPSILON);Non-positive values:
Sourcepub fncbrt(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fncbrt(self) ->f128
f128 #116909)Returns the cube root of a number.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thecbrtf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnhypot(self, other:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnhypot(self, other:f128) ->f128
f128 #116909)Compute the distance between the origin and a point (x,y) on theEuclidean plane. Equivalently, compute the length of the hypotenuse of aright-angle triangle with other sides having lengthx.abs() andy.abs().
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thehypotf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnsin(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnsin(self) ->f128
f128 #116909)Sourcepub fncos(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fncos(self) ->f128
f128 #116909)Sourcepub fntan(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fntan(self) ->f128
f128 #116909)Computes the tangent of a number (in radians).
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thetanf128 from libc on Unix andWindows. Note that this might change in the future.
§Examples
Sourcepub fnasin(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnasin(self) ->f128
f128 #116909)Computes the arcsine of a number. Return value is in radians inthe range [-pi/2, pi/2] or NaN if the number is outside the range[-1, 1].
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to theasinf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnacos(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnacos(self) ->f128
f128 #116909)Computes the arccosine of a number. Return value is in radians inthe range [0, pi] or NaN if the number is outside the range[-1, 1].
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to theacosf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnatan(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnatan(self) ->f128
f128 #116909)Computes the arctangent of a number. Return value is in radians in therange [-pi/2, pi/2];
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to theatanf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnatan2(self, other:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnatan2(self, other:f128) ->f128
f128 #116909)Computes the four quadrant arctangent ofself (y) andother (x) in radians.
x | y | Piecewise Definition | Range |
|---|---|---|---|
>= +0 | >= +0 | arctan(y/x) | [+0, +pi/2] |
>= +0 | <= -0 | arctan(y/x) | [-pi/2, -0] |
<= -0 | >= +0 | arctan(y/x) + pi | [+pi/2, +pi] |
<= -0 | <= -0 | arctan(y/x) - pi | [-pi, -pi/2] |
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to theatan2f128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
#![feature(f128)]// Positive angles measured counter-clockwise// from positive x axis// -pi/4 radians (45 deg clockwise)letx1 =3.0f128;lety1 = -3.0f128;// 3pi/4 radians (135 deg counter-clockwise)letx2 = -3.0f128;lety2 =3.0f128;letabs_difference_1 = (y1.atan2(x1) - (-std::f128::consts::FRAC_PI_4)).abs();letabs_difference_2 = (y2.atan2(x2) - (3.0* std::f128::consts::FRAC_PI_4)).abs();assert!(abs_difference_1 <= f128::EPSILON);assert!(abs_difference_2 <= f128::EPSILON);Sourcepub fnsin_cos(self) -> (f128,f128)
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnsin_cos(self) -> (f128,f128)
f128 #116909)Simultaneously computes the sine and cosine of the number,x. Returns(sin(x), cos(x)).
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to the(f128::sin(x), f128::cos(x)). Note that this might change in the future.
§Examples
Sourcepub fnexp_m1(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnexp_m1(self) ->f128
f128 #116909)Returnse^(self) - 1 in a way that is accurate even if thenumber is close to zero.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to theexpm1f128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnln_1p(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnln_1p(self) ->f128
f128 #116909)Returnsln(1+n) (natural logarithm) more accurately than ifthe operations were performed separately.
This returns NaN whenn < -1.0, and negative infinity whenn == -1.0.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thelog1pf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
#![feature(f128)]letx =1e-8_f128;// for very small x, ln(1 + x) is approximately x - x^2 / 2letapprox = x - x * x /2.0;letabs_difference = (x.ln_1p() - approx).abs();assert!(abs_difference <1e-10);Out-of-range values:
Sourcepub fnsinh(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnsinh(self) ->f128
f128 #116909)Hyperbolic sine function.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thesinhf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fncosh(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fncosh(self) ->f128
f128 #116909)Hyperbolic cosine function.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thecoshf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fntanh(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fntanh(self) ->f128
f128 #116909)Hyperbolic tangent function.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thetanhf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnasinh(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnasinh(self) ->f128
f128 #116909)Sourcepub fnacosh(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnacosh(self) ->f128
f128 #116909)Sourcepub fnatanh(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnatanh(self) ->f128
f128 #116909)Sourcepub fngamma(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fngamma(self) ->f128
f128 #116909)Gamma function.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thetgammaf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnln_gamma(self) -> (f128,i32)
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnln_gamma(self) -> (f128,i32)
f128 #116909)Natural logarithm of the absolute value of the gamma function
The integer part of the tuple indicates the sign of the gamma function.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to thelgammaf128_r from libc on Unixand Windows. Note that this might change in the future.
§Examples
Sourcepub fnerf(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnerf(self) ->f128
f128 #116909)Error function.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to theerff128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
#![feature(f128)]#![feature(float_erf)]/// The error function relates what percent of a normal distribution lies/// within `x` standard deviations (scaled by `1/sqrt(2)`).fnwithin_standard_deviations(x: f128) -> f128 { (x * std::f128::consts::FRAC_1_SQRT_2).erf() *100.0}// 68% of a normal distribution is within one standard deviationassert!((within_standard_deviations(1.0) -68.269).abs() <0.01);// 95% of a normal distribution is within two standard deviationsassert!((within_standard_deviations(2.0) -95.450).abs() <0.01);// 99.7% of a normal distribution is within three standard deviationsassert!((within_standard_deviations(3.0) -99.730).abs() <0.01);Sourcepub fnerfc(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnerfc(self) ->f128
f128 #116909)Complementary error function.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
This function currently corresponds to theerfcf128 from libc on Unixand Windows. Note that this might change in the future.
§Examples
Source§implf128
implf128
Sourcepub constRADIX:u32 = 2
🔬This is a nightly-only experimental API. (f128 #116909)
pub constRADIX:u32 = 2
f128 #116909)The radix or base of the internal representation off128.
Sourcepub constMANTISSA_DIGITS:u32 = 113
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMANTISSA_DIGITS:u32 = 113
f128 #116909)Number of significant digits in base 2.
Note that the size of the mantissa in the bitwise representation is onesmaller than this since the leading 1 is not stored explicitly.
Sourcepub constDIGITS:u32 = 33
🔬This is a nightly-only experimental API. (f128 #116909)
pub constDIGITS:u32 = 33
f128 #116909)Approximate number of significant digits in base 10.
This is the maximumx such that any decimal number withxsignificant digits can be converted tof128 and back without loss.
Equal to floor(log10 2MANTISSA_DIGITS − 1).
Sourcepub constEPSILON:f128 = 1.92592994438723585305597794258492732e-34_f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub constEPSILON:f128 = 1.92592994438723585305597794258492732e-34_f128
f128 #116909)Machine epsilon value forf128.
This is the difference between1.0 and the next larger representable number.
Equal to 21 − MANTISSA_DIGITS.
Sourcepub constMIN:f128 = -1.18973149535723176508575932662800702e+4932_f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMIN:f128 = -1.18973149535723176508575932662800702e+4932_f128
f128 #116909)Smallest finitef128 value.
Equal to −MAX.
Sourcepub constMIN_POSITIVE:f128 = 3.36210314311209350626267781732175260e-4932_f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMIN_POSITIVE:f128 = 3.36210314311209350626267781732175260e-4932_f128
f128 #116909)Smallest positive normalf128 value.
Equal to 2MIN_EXP − 1.
Sourcepub constMAX:f128 = 1.18973149535723176508575932662800702e+4932_f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMAX:f128 = 1.18973149535723176508575932662800702e+4932_f128
f128 #116909)Largest finitef128 value.
Equal to(1 − 2−MANTISSA_DIGITS) 2MAX_EXP.
Sourcepub constMIN_EXP:i32 = -16_381
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMIN_EXP:i32 = -16_381
f128 #116909)One greater than the minimum possiblenormal power of 2 exponentfor a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition).
This corresponds to the exact minimum possiblenormal power of 2 exponentfor a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition).In other words, all normal numbers representable by this type aregreater than or equal to 0.5 × 2MIN_EXP.
Sourcepub constMAX_EXP:i32 = 16_384
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMAX_EXP:i32 = 16_384
f128 #116909)One greater than the maximum possible power of 2 exponentfor a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition).
This corresponds to the exact maximum possible power of 2 exponentfor a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition).In other words, all numbers representable by this type arestrictly less than 2MAX_EXP.
Sourcepub constMIN_10_EXP:i32 = -4_931
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMIN_10_EXP:i32 = -4_931
f128 #116909)Minimumx for which 10x is normal.
Equal to ceil(log10 MIN_POSITIVE).
Sourcepub constMAX_10_EXP:i32 = 4_932
🔬This is a nightly-only experimental API. (f128 #116909)
pub constMAX_10_EXP:i32 = 4_932
f128 #116909)Maximumx for which 10x is normal.
Equal to floor(log10 MAX).
Sourcepub constNAN:f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub constNAN:f128
f128 #116909)Not a Number (NaN).
Note that IEEE 754 doesn’t define just a single NaN value; a plethora of bit patterns areconsidered to be NaN. Furthermore, the standard makes a difference between a “signaling” anda “quiet” NaN, and allows inspecting its “payload” (the unspecified bits in the bit pattern)and its sign. See thespecification of NaN bit patterns for moreinfo.
This constant is guaranteed to be a quiet NaN (on targets that follow the Rust assumptionsthat the quiet/signaling bit being set to 1 indicates a quiet NaN). Beyond that, nothing isguaranteed about the specific bit pattern chosen here: both payload and sign are arbitrary.The concrete bit pattern may change across Rust versions and target platforms.
Sourcepub constNEG_INFINITY:f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub constNEG_INFINITY:f128
f128 #116909)Negative infinity (−∞).
Sourcepub const fnis_infinite(self) ->bool
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnis_infinite(self) ->bool
f128 #116909)Returnstrue if this value is positive infinity or negative infinity, andfalse otherwise.
Sourcepub const fnis_subnormal(self) ->bool
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnis_subnormal(self) ->bool
f128 #116909)Returnstrue if the number issubnormal.
#![feature(f128)]letmin = f128::MIN_POSITIVE;// 3.362103143e-4932f128letmax = f128::MAX;letlower_than_min =1.0e-4960_f128;letzero =0.0_f128;assert!(!min.is_subnormal());assert!(!max.is_subnormal());assert!(!zero.is_subnormal());assert!(!f128::NAN.is_subnormal());assert!(!f128::INFINITY.is_subnormal());// Values between `0` and `min` are Subnormal.assert!(lower_than_min.is_subnormal());Sourcepub const fnis_normal(self) ->bool
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnis_normal(self) ->bool
f128 #116909)Returnstrue if the number is neither zero, infinite,subnormal, or NaN.
#![feature(f128)]letmin = f128::MIN_POSITIVE;// 3.362103143e-4932f128letmax = f128::MAX;letlower_than_min =1.0e-4960_f128;letzero =0.0_f128;assert!(min.is_normal());assert!(max.is_normal());assert!(!zero.is_normal());assert!(!f128::NAN.is_normal());assert!(!f128::INFINITY.is_normal());// Values between `0` and `min` are Subnormal.assert!(!lower_than_min.is_normal());Sourcepub const fnclassify(self) ->FpCategory
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnclassify(self) ->FpCategory
f128 #116909)Returns the floating point category of the number. If only one propertyis going to be tested, it is generally faster to use the specificpredicate instead.
Sourcepub const fnis_sign_positive(self) ->bool
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnis_sign_positive(self) ->bool
f128 #116909)Returnstrue ifself has a positive sign, including+0.0, NaNs withpositive sign bit and positive infinity.
Note that IEEE 754 doesn’t assign any meaning to the sign bit in case ofa NaN, and as Rust doesn’t guarantee that the bit pattern of NaNs areconserved over arithmetic operations, the result ofis_sign_positive ona NaN might produce an unexpected or non-portable result. See thespecificationof NaN bit patterns for more info. Useself.signum() == 1.0if you need fully portable behavior (will returnfalse for all NaNs).
Sourcepub const fnis_sign_negative(self) ->bool
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnis_sign_negative(self) ->bool
f128 #116909)Returnstrue ifself has a negative sign, including-0.0, NaNs withnegative sign bit and negative infinity.
Note that IEEE 754 doesn’t assign any meaning to the sign bit in case ofa NaN, and as Rust doesn’t guarantee that the bit pattern of NaNs areconserved over arithmetic operations, the result ofis_sign_negative ona NaN might produce an unexpected or non-portable result. See thespecificationof NaN bit patterns for more info. Useself.signum() == -1.0if you need fully portable behavior (will returnfalse for all NaNs).
Sourcepub const fnnext_up(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnnext_up(self) ->f128
f128 #116909)Returns the least number greater thanself.
LetTINY be the smallest representable positivef128. Then,
- if
self.is_nan(), this returnsself; - if
selfisNEG_INFINITY, this returnsMIN; - if
selfis-TINY, this returns -0.0; - if
selfis -0.0 or +0.0, this returnsTINY; - if
selfisMAXorINFINITY, this returnsINFINITY; - otherwise the unique least value greater than
selfis returned.
The identityx.next_up() == -(-x).next_down() holds for all non-NaNx. Whenxis finitex == x.next_up().next_down() also holds.
#![feature(f128)]// f128::EPSILON is the difference between 1.0 and the next number up.assert_eq!(1.0f128.next_up(),1.0+ f128::EPSILON);// But not for most numbers.assert!(0.1f128.next_up() <0.1+ f128::EPSILON);assert_eq!(4611686018427387904f128.next_up(),4611686018427387904.000000000000001);This operation corresponds to IEEE-754nextUp.
Sourcepub const fnnext_down(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnnext_down(self) ->f128
f128 #116909)Returns the greatest number less thanself.
LetTINY be the smallest representable positivef128. Then,
- if
self.is_nan(), this returnsself; - if
selfisINFINITY, this returnsMAX; - if
selfisTINY, this returns 0.0; - if
selfis -0.0 or +0.0, this returns-TINY; - if
selfisMINorNEG_INFINITY, this returnsNEG_INFINITY; - otherwise the unique greatest value less than
selfis returned.
The identityx.next_down() == -(-x).next_up() holds for all non-NaNx. Whenxis finitex == x.next_down().next_up() also holds.
#![feature(f128)]letx =1.0f128;// Clamp value into range [0, 1).letclamped = x.clamp(0.0,1.0f128.next_down());assert!(clamped <1.0);assert_eq!(clamped.next_up(),1.0);This operation corresponds to IEEE-754nextDown.
Sourcepub const fnto_degrees(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnto_degrees(self) ->f128
f128 #116909)Sourcepub const fnto_radians(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnto_radians(self) ->f128
f128 #116909)Sourcepub const fnmax(self, other:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnmax(self, other:f128) ->f128
f128 #116909)Returns the maximum of the two numbers, ignoring NaN.
If exactly one of the arguments is NaN, then the other argument is returned. If botharguments are NaN, the return value is NaN, with the bit pattern picked using the usualrules for arithmetic operations. If the inputs compare equal (suchas for the case of+0.0 and-0.0), either input may be returned non-deterministically.
This follows the IEEE 754-2008 semantics formaxNum, except for handling of signaling NaNs;this function handles all NaNs the same way and avoidsmaxNum’s problems with associativity.This also matches the behavior of libm’sfmax.
Sourcepub const fnmin(self, other:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnmin(self, other:f128) ->f128
f128 #116909)Returns the minimum of the two numbers, ignoring NaN.
If exactly one of the arguments is NaN, then the other argument is returned. If botharguments are NaN, the return value is NaN, with the bit pattern picked using the usualrules for arithmetic operations. If the inputs compare equal (suchas for the case of+0.0 and-0.0), either input may be returned non-deterministically.
This follows the IEEE 754-2008 semantics forminNum, except for handling of signaling NaNs;this function handles all NaNs the same way and avoidsminNum’s problems with associativity.This also matches the behavior of libm’sfmin.
Sourcepub const fnmaximum(self, other:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnmaximum(self, other:f128) ->f128
f128 #116909)Returns the maximum of the two numbers, propagating NaN.
If at least one of the arguments is NaN, the return value is NaN, with the bit patternpicked using the usualrules for arithmetic operations. Furthermore,-0.0 is considered to be less than+0.0, making this function fully deterministic fornon-NaN inputs.
This is in contrast tof128::max which only returns NaN whenboth arguments are NaN,and which does not reliably order-0.0 and+0.0.
This follows the IEEE 754-2019 semantics formaximum.
Sourcepub const fnminimum(self, other:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnminimum(self, other:f128) ->f128
f128 #116909)Returns the minimum of the two numbers, propagating NaN.
If at least one of the arguments is NaN, the return value is NaN, with the bit patternpicked using the usualrules for arithmetic operations. Furthermore,-0.0 is considered to be less than+0.0, making this function fully deterministic fornon-NaN inputs.
This is in contrast tof128::min which only returns NaN whenboth arguments are NaN,and which does not reliably order-0.0 and+0.0.
This follows the IEEE 754-2019 semantics forminimum.
Sourcepub const fnmidpoint(self, other:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnmidpoint(self, other:f128) ->f128
f128 #116909)Calculates the midpoint (average) betweenself andrhs.
This returns NaN wheneither argument is NaN or if a combination of+inf and -inf is provided as arguments.
§Examples
Sourcepub unsafe fnto_int_unchecked<Int>(self) -> Intwheref128:FloatToInt<Int>,
🔬This is a nightly-only experimental API. (f128 #116909)
pub unsafe fnto_int_unchecked<Int>(self) -> Intwheref128:FloatToInt<Int>,
f128 #116909)Rounds toward zero and converts to any primitive integer type,assuming that the value is finite and fits in that type.
#![feature(f128)]letvalue =4.6_f128;letrounded =unsafe{ value.to_int_unchecked::<u16>() };assert_eq!(rounded,4);letvalue = -128.9_f128;letrounded =unsafe{ value.to_int_unchecked::<i8>() };assert_eq!(rounded, i8::MIN);§Safety
The value must:
- Not be
NaN - Not be infinite
- Be representable in the return type
Int, after truncating off its fractional part
Sourcepub const fnto_bits(self) ->u128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnto_bits(self) ->u128
f128 #116909)Raw transmutation tou128.
This is currently identical totransmute::<f128, u128>(self) on all platforms.
Seefrom_bits for some discussion of theportability of this operation (there are almost no issues).
Note that this function is distinct fromas casting, which attempts topreserve thenumeric value, and not the bitwise value.
Sourcepub const fnfrom_bits(v:u128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnfrom_bits(v:u128) ->f128
f128 #116909)Raw transmutation fromu128.
This is currently identical totransmute::<u128, f128>(v) on all platforms.It turns out this is incredibly portable, for two reasons:
- Floats and Ints have the same endianness on all supported platforms.
- IEEE 754 very precisely specifies the bit layout of floats.
However there is one caveat: prior to the 2008 version of IEEE 754, howto interpret the NaN signaling bit wasn’t actually specified. Most platforms(notably x86 and ARM) picked the interpretation that was ultimatelystandardized in 2008, but some didn’t (notably MIPS). As a result, allsignaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
Rather than trying to preserve signaling-ness cross-platform, thisimplementation favors preserving the exact bits. This means thatany payloads encoded in NaNs will be preserved even if the result ofthis method is sent over the network from an x86 machine to a MIPS one.
If the results of this method are only manipulated by the samearchitecture that produced them, then there is no portability concern.
If the input isn’t NaN, then there is no portability concern.
If you don’t care about signalingness (very likely), then there is noportability concern.
Note that this function is distinct fromas casting, which attempts topreserve thenumeric value, and not the bitwise value.
Sourcepub const fnto_be_bytes(self) -> [u8;16]
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnto_be_bytes(self) -> [u8;16]
f128 #116909)Sourcepub const fnto_le_bytes(self) -> [u8;16]
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnto_le_bytes(self) -> [u8;16]
f128 #116909)Sourcepub const fnto_ne_bytes(self) -> [u8;16]
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnto_ne_bytes(self) -> [u8;16]
f128 #116909)Returns the memory representation of this floating point number as a byte array innative byte order.
As the target platform’s native endianness is used, portable codeshould useto_be_bytes orto_le_bytes, as appropriate, instead.
Seefrom_bits for some discussion of theportability of this operation (there are almost no issues).
§Examples
Sourcepub const fnfrom_be_bytes(bytes: [u8;16]) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnfrom_be_bytes(bytes: [u8;16]) ->f128
f128 #116909)Sourcepub const fnfrom_le_bytes(bytes: [u8;16]) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnfrom_le_bytes(bytes: [u8;16]) ->f128
f128 #116909)Sourcepub const fnfrom_ne_bytes(bytes: [u8;16]) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnfrom_ne_bytes(bytes: [u8;16]) ->f128
f128 #116909)Creates a floating point value from its representation as a byte array in native endian.
As the target platform’s native endianness is used, portable codelikely wants to usefrom_be_bytes orfrom_le_bytes, asappropriate instead.
Seefrom_bits for some discussion of theportability of this operation (there are almost no issues).
§Examples
Sourcepub const fntotal_cmp(&self, other: &f128) ->Ordering
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fntotal_cmp(&self, other: &f128) ->Ordering
f128 #116909)Returns the ordering betweenself andother.
Unlike the standard partial comparison between floating point numbers,this comparison always produces an ordering in accordance tothetotalOrder predicate as defined in the IEEE 754 (2008 revision)floating point standard. The values are ordered in the following sequence:
- negative quiet NaN
- negative signaling NaN
- negative infinity
- negative numbers
- negative subnormal numbers
- negative zero
- positive zero
- positive subnormal numbers
- positive numbers
- positive infinity
- positive signaling NaN
- positive quiet NaN.
The ordering established by this function does not always agree with thePartialOrd andPartialEq implementations off128. For example,they consider negative and positive zero equal, whiletotal_cmpdoesn’t.
The interpretation of the signaling NaN bit follows the definition inthe IEEE 754 standard, which may not match the interpretation by some ofthe older, non-conformant (e.g. MIPS) hardware implementations.
§Example
#![feature(f128)]structGoodBoy { name:&'staticstr, weight: f128,}letmutbois =vec![ GoodBoy { name:"Pucci", weight:0.1}, GoodBoy { name:"Woofer", weight:99.0}, GoodBoy { name:"Yapper", weight:10.0}, GoodBoy { name:"Chonk", weight: f128::INFINITY }, GoodBoy { name:"Abs. Unit", weight: f128::NAN }, GoodBoy { name:"Floaty", weight: -5.0},];bois.sort_by(|a, b| a.weight.total_cmp(&b.weight));// `f128::NAN` could be positive or negative, which will affect the sort order.iff128::NAN.is_sign_negative() { bois.into_iter().map(|b| b.weight) .zip([f128::NAN, -5.0,0.1,10.0,99.0, f128::INFINITY].iter()) .for_each(|(a, b)|assert_eq!(a.to_bits(), b.to_bits()))}else{ bois.into_iter().map(|b| b.weight) .zip([-5.0,0.1,10.0,99.0, f128::INFINITY, f128::NAN].iter()) .for_each(|(a, b)|assert_eq!(a.to_bits(), b.to_bits()))}Sourcepub const fnclamp(self, min:f128, max:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnclamp(self, min:f128, max:f128) ->f128
f128 #116909)Restrict a value to a certain interval unless it is NaN.
Returnsmax ifself is greater thanmax, andmin ifself isless thanmin. Otherwise this returnsself.
Note that this function returns NaN if the initial value was NaN aswell. If the result is zero and among the three inputsself,min, andmax there arezeros with different sign, either0.0 or-0.0 is returned non-deterministically.
§Panics
Panics ifmin > max,min is NaN, ormax is NaN.
§Examples
#![feature(f128)]assert!((-3.0f128).clamp(-2.0,1.0) == -2.0);assert!((0.0f128).clamp(-2.0,1.0) ==0.0);assert!((2.0f128).clamp(-2.0,1.0) ==1.0);assert!((f128::NAN).clamp(-2.0,1.0).is_nan());// These always returns zero, but the sign (which is ignored by `==`) is non-deterministic.assert!((0.0f128).clamp(-0.0, -0.0) ==0.0);assert!((1.0f128).clamp(-0.0,0.0) ==0.0);// This is definitely a negative zero.assert!((-1.0f128).clamp(-0.0,1.0).is_sign_negative());Sourcepub fnclamp_magnitude(self, limit:f128) ->f128
🔬This is a nightly-only experimental API. (clamp_magnitude #148519)
pub fnclamp_magnitude(self, limit:f128) ->f128
clamp_magnitude #148519)Clamps this number to a symmetric range centered around zero.
The method clamps the number’s magnitude (absolute value) to be at mostlimit.
This is functionally equivalent toself.clamp(-limit, limit), but is moreexplicit about the intent.
§Panics
Panics iflimit is negative or NaN, as this indicates a logic error.
§Examples
Sourcepub const fnabs(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnabs(self) ->f128
f128 #116909)Sourcepub const fnsignum(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnsignum(self) ->f128
f128 #116909)Returns a number that represents the sign ofself.
1.0if the number is positive,+0.0orINFINITY-1.0if the number is negative,-0.0orNEG_INFINITY- NaN if the number is NaN
§Examples
Sourcepub const fncopysign(self, sign:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fncopysign(self, sign:f128) ->f128
f128 #116909)Returns a number composed of the magnitude ofself and the sign ofsign.
Equal toself if the sign ofself andsign are the same, otherwise equal to-self.Ifself is a NaN, then a NaN with the same payload asself and the sign bit ofsign isreturned.
Ifsign is a NaN, then this operation will still carry over its sign into the result. Notethat IEEE 754 doesn’t assign any meaning to the sign bit in case of a NaN, and as Rustdoesn’t guarantee that the bit pattern of NaNs are conserved over arithmetic operations, theresult ofcopysign withsign being a NaN might produce an unexpected or non-portableresult. See thespecification of NaN bit patterns for moreinfo.
§Examples
Sourcepub const fnalgebraic_add(self, rhs:f128) ->f128
🔬This is a nightly-only experimental API. (float_algebraic #136469)
pub const fnalgebraic_add(self, rhs:f128) ->f128
float_algebraic #136469)Float addition that allows optimizations based on algebraic rules.
Seealgebraic operators for more info.
Sourcepub const fnalgebraic_sub(self, rhs:f128) ->f128
🔬This is a nightly-only experimental API. (float_algebraic #136469)
pub const fnalgebraic_sub(self, rhs:f128) ->f128
float_algebraic #136469)Float subtraction that allows optimizations based on algebraic rules.
Seealgebraic operators for more info.
Sourcepub const fnalgebraic_mul(self, rhs:f128) ->f128
🔬This is a nightly-only experimental API. (float_algebraic #136469)
pub const fnalgebraic_mul(self, rhs:f128) ->f128
float_algebraic #136469)Float multiplication that allows optimizations based on algebraic rules.
Seealgebraic operators for more info.
Sourcepub const fnalgebraic_div(self, rhs:f128) ->f128
🔬This is a nightly-only experimental API. (float_algebraic #136469)
pub const fnalgebraic_div(self, rhs:f128) ->f128
float_algebraic #136469)Float division that allows optimizations based on algebraic rules.
Seealgebraic operators for more info.
Sourcepub const fnalgebraic_rem(self, rhs:f128) ->f128
🔬This is a nightly-only experimental API. (float_algebraic #136469)
pub const fnalgebraic_rem(self, rhs:f128) ->f128
float_algebraic #136469)Float remainder that allows optimizations based on algebraic rules.
Seealgebraic operators for more info.
Source§implf128
implf128
Sourcepub const fnfloor(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnfloor(self) ->f128
f128 #116909)Returns the largest integer less than or equal toself.
This function always returns the precise result.
§Examples
Sourcepub const fnceil(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnceil(self) ->f128
f128 #116909)Returns the smallest integer greater than or equal toself.
This function always returns the precise result.
§Examples
Sourcepub const fnround(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnround(self) ->f128
f128 #116909)Returns the nearest integer toself. If a value is half-way between twointegers, round away from0.0.
This function always returns the precise result.
§Examples
Sourcepub const fnround_ties_even(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnround_ties_even(self) ->f128
f128 #116909)Returns the nearest integer to a number. Rounds half-way cases to the numberwith an even least significant digit.
This function always returns the precise result.
§Examples
Sourcepub const fntrunc(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fntrunc(self) ->f128
f128 #116909)Returns the integer part ofself.This means that non-integer numbers are always truncated towards zero.
This function always returns the precise result.
§Examples
Sourcepub const fnfract(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnfract(self) ->f128
f128 #116909)Sourcepub const fnmul_add(self, a:f128, b:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub const fnmul_add(self, a:f128, b:f128) ->f128
f128 #116909)Fused multiply-add. Computes(self * a) + b with only one roundingerror, yielding a more accurate result than an unfused multiply-add.
Usingmul_addmay be more performant than an unfused multiply-add ifthe target architecture has a dedicatedfma CPU instruction. However,this is not always true, and will be heavily dependant on designingalgorithms with specific target hardware in mind.
§Precision
The result of this operation is guaranteed to be the roundedinfinite-precision result. It is specified by IEEE 754 asfusedMultiplyAdd and guaranteed not to change.
§Examples
#![feature(f128)]letm =10.0_f128;letx =4.0_f128;letb =60.0_f128;assert_eq!(m.mul_add(x, b),100.0);assert_eq!(m * x + b,100.0);letone_plus_eps =1.0_f128+ f128::EPSILON;letone_minus_eps =1.0_f128- f128::EPSILON;letminus_one = -1.0_f128;// The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.assert_eq!(one_plus_eps.mul_add(one_minus_eps, minus_one), -f128::EPSILON * f128::EPSILON);// Different rounding with the non-fused multiply and add.assert_eq!(one_plus_eps * one_minus_eps + minus_one,0.0);Sourcepub fndiv_euclid(self, rhs:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fndiv_euclid(self, rhs:f128) ->f128
f128 #116909)Calculates Euclidean division, the matching method forrem_euclid.
This computes the integern such thatself = n * rhs + self.rem_euclid(rhs).In other words, the result isself / rhs rounded to the integernsuch thatself >= n * rhs.
§Precision
The result of this operation is guaranteed to be the roundedinfinite-precision result.
§Examples
Sourcepub fnrem_euclid(self, rhs:f128) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnrem_euclid(self, rhs:f128) ->f128
f128 #116909)Calculates the least nonnegative remainder ofself (mod rhs).
In particular, the return valuer satisfies0.0 <= r < rhs.abs() inmost cases. However, due to a floating point round-off error it canresult inr == rhs.abs(), violating the mathematical definition, ifself is much smaller thanrhs.abs() in magnitude andself < 0.0.This result is not an element of the function’s codomain, but it is theclosest floating point number in the real numbers and thus fulfills thepropertyself == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)approximately.
§Precision
The result of this operation is guaranteed to be the roundedinfinite-precision result.
§Examples
Sourcepub fnpowi(self, n:i32) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnpowi(self, n:i32) ->f128
f128 #116909)Raises a number to an integer power.
Using this function is generally faster than usingpowf.It might have a different sequence of rounding operations thanpowf,so the results are not guaranteed to agree.
Note that this function is special in that it can return non-NaN results for NaN inputs. Forexample,f128::powi(f128::NAN, 0) returns1.0. However, if an input is asignalingNaN, then the result is non-deterministically either a NaN or the result that thecorresponding quiet NaN would produce.
§Unspecified precision
The precision of this function is non-deterministic. This means it varies by platform,Rust version, and can even differ within the same execution from one invocation to the next.
§Examples
Sourcepub fnsqrt(self) ->f128
🔬This is a nightly-only experimental API. (f128 #116909)
pub fnsqrt(self) ->f128
f128 #116909)Trait Implementations§
1.22.0 (const:unstable) ·Source§implAddAssign<&f128> forf128
implAddAssign<&f128> forf128
Source§fnadd_assign(&mut self, other: &f128)
fnadd_assign(&mut self, other: &f128)
+= operation.Read more1.8.0 (const:unstable) ·Source§implAddAssign forf128
implAddAssign forf128
Source§fnadd_assign(&mut self, other:f128)
fnadd_assign(&mut self, other:f128)
+= operation.Read more1.22.0 (const:unstable) ·Source§implDivAssign<&f128> forf128
implDivAssign<&f128> forf128
Source§fndiv_assign(&mut self, other: &f128)
fndiv_assign(&mut self, other: &f128)
/= operation.Read more1.8.0 (const:unstable) ·Source§implDivAssign forf128
implDivAssign forf128
Source§fndiv_assign(&mut self, other:f128)
fndiv_assign(&mut self, other:f128)
/= operation.Read more1.22.0 (const:unstable) ·Source§implMulAssign<&f128> forf128
implMulAssign<&f128> forf128
Source§fnmul_assign(&mut self, other: &f128)
fnmul_assign(&mut self, other: &f128)
*= operation.Read more1.8.0 (const:unstable) ·Source§implMulAssign forf128
implMulAssign forf128
Source§fnmul_assign(&mut self, other:f128)
fnmul_assign(&mut self, other:f128)
*= operation.Read more1.0.0 (const:unstable) ·Source§implPartialOrd forf128
implPartialOrd forf128
1.0.0 (const:unstable) ·Source§implRem forf128
The remainder from the division of two floats.
implRem forf128
The remainder from the division of two floats.
The remainder has the same sign as the dividend and is computed as:x - (x / y).trunc() * y.
§Examples
1.22.0 (const:unstable) ·Source§implRemAssign<&f128> forf128
implRemAssign<&f128> forf128
Source§fnrem_assign(&mut self, other: &f128)
fnrem_assign(&mut self, other: &f128)
%= operation.Read more1.8.0 (const:unstable) ·Source§implRemAssign forf128
implRemAssign forf128
Source§fnrem_assign(&mut self, other:f128)
fnrem_assign(&mut self, other:f128)
%= operation.Read more1.22.0 (const:unstable) ·Source§implSubAssign<&f128> forf128
implSubAssign<&f128> forf128
Source§fnsub_assign(&mut self, other: &f128)
fnsub_assign(&mut self, other: &f128)
-= operation.Read more1.8.0 (const:unstable) ·Source§implSubAssign forf128
implSubAssign forf128
Source§fnsub_assign(&mut self, other:f128)
fnsub_assign(&mut self, other:f128)
-= operation.Read more