Primitive Type f321.0.0[]

Expand description

32 位浮点类型 (特别是 IEEE 754-2008 中定义的 “binary32” 类型)。

此类型可以表示各种十进制数字,例如 3.527-113.750.0078125343597383680-1。因此,与整数类型 (例如 i32) 不同,浮点类型也可以表示非整数。

但是,能够表示这么广泛的数字是以牺牲精度为代价的: 浮点数只能表示某些实数,并且计算时会将浮点数舍入到附近的可表示数字。 例如,5.01.0 可以精确地表示为 f32,但是 1.0 / 5.0 会导致 0.20000000298023223876953125,因为 0.2 不能精确地表示为 f32。 但是请注意,带有 println 的浮动彩信和朋友经常会丢弃无关紧要的数字: println!("{}", 1.0f32 / 5.0f32) 会打印 0.2

此外,f32 可以表示一些特殊值:

  • -0.0: IEEE 754 浮点数有一个表示它们的符号的位,所以 -0.0 是一个可能的值。对于比较 -0.0 = +0.0,但浮点运算可以通过算术运算携带符号位。 这意味着 -0.0 × +0.0 产生 -0.0,四舍五入到小于浮点值的负数也产生 -0.0
  • −∞: 这些是通过 1.0 / 0.0 之类的计算得出的。
  • NaN (not a number): 该值是由 (-1.0).sqrt() 之类的计算得出的。NaN 有一些潜在的意外行为: 它不等于任何浮点数,包括它本身! 它也不小于或大于任何浮点数,因此无法分类。

最后,由于操作数之一为 NaN 的几乎所有计算也将产生 NaN,因此它被认为具有传染性。

有关浮点数的更多信息,请参见 Wikipedia

See also the std::f32::consts module.

Implementations

返回小于或等于数字的最大整数。

Examples

let f = 3.7_f32;
let g = 3.0_f32;
let h = -3.7_f32;

assert_eq!(f.floor(), 3.0);
assert_eq!(g.floor(), 3.0);
assert_eq!(h.floor(), -4.0);
Run

返回大于或等于数字的最小整数。

Examples

let f = 3.01_f32;
let g = 4.0_f32;

assert_eq!(f.ceil(), 4.0);
assert_eq!(g.ceil(), 4.0);
Run

返回最接近整数的数字。 远离 0.0 的圆形中途机箱。

Examples

let f = 3.3_f32;
let g = -3.3_f32;

assert_eq!(f.round(), 3.0);
assert_eq!(g.round(), -3.0);
Run

返回数字的整数部分。

Examples

let f = 3.7_f32;
let g = 3.0_f32;
let h = -3.7_f32;

assert_eq!(f.trunc(), 3.0);
assert_eq!(g.trunc(), 3.0);
assert_eq!(h.trunc(), -3.0);
Run

返回数字的小数部分。

Examples

let x = 3.6_f32;
let y = -3.6_f32;
let abs_difference_x = (x.fract() - 0.6).abs();
let abs_difference_y = (y.fract() - (-0.6)).abs();

assert!(abs_difference_x <= f32::EPSILON);
assert!(abs_difference_y <= f32::EPSILON);
Run

计算 self 的绝对值。 如果数字为 NAN,则返回 NAN

Examples

let x = 3.5_f32;
let y = -3.5_f32;

let abs_difference_x = (x.abs() - x).abs();
let abs_difference_y = (y.abs() - (-y)).abs();

assert!(abs_difference_x <= f32::EPSILON);
assert!(abs_difference_y <= f32::EPSILON);

assert!(f32::NAN.abs().is_nan());
Run

返回一个表示 self 符号的数字。

  • 1.0 如果数字为正,则为 +0.0INFINITY
  • -1.0 如果数字为负,则 -0.0NEG_INFINITY
  • NAN 如果数字是 NAN

Examples

let f = 3.5_f32;

assert_eq!(f.signum(), 1.0);
assert_eq!(f32::NEG_INFINITY.signum(), -1.0);

assert!(f32::NAN.signum().is_nan());
Run

返回一个数字,该数字由 self 的大小和 sign 的符号组成。

如果 selfsign 的符号相同,则等于 self,否则等于 -self。 如果 selfNAN,则返回带有 sign 符号的 NAN

Examples

let f = 3.5_f32;

assert_eq!(f.copysign(0.42), 3.5_f32);
assert_eq!(f.copysign(-0.42), -3.5_f32);
assert_eq!((-f).copysign(0.42), 3.5_f32);
assert_eq!((-f).copysign(-0.42), -3.5_f32);

assert!(f32::NAN.copysign(1.0).is_nan());
Run

融合乘法加法。 仅用一个舍入误差计算 (self * a) + b,比未融合的乘法加法产生更准确的结果。

如果目标体系结构具有专用的 fma CPU 指令,则使用 mul_add 的性能可能比未融合的乘加性能更高。

但是,这并不总是正确的,并且在很大程度上取决于设计算法时要考虑特定的目标硬件。

Examples

let m = 10.0_f32;
let x = 4.0_f32;
let b = 60.0_f32;

// 100.0
let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算欧几里得除法,即 rem_euclid 的匹配方法。

这将计算整数 n,如 self = n * rhs + self.rem_euclid(rhs)。 换句话说,结果是将 self / rhs 舍入为 n 的整数 n

Examples

let a: f32 = 7.0;
let b = 4.0;
assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
Run

计算 self (mod rhs) 的最小非负余数。

特别地,在大多数情况下,返回值 r 满足 0.0 <= r < rhs.abs()。 但是,由于浮点舍入误差,如果 self 的幅值和 self < 0.0 远小于 rhs.abs(),则可能会导致 r == rhs.abs() 违反数学定义。 此结果不是函数共域的元素,但它是实数中最接近的浮点数,因此近似满足属性 self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)

Examples

let a: f32 = 7.0;
let b = 4.0;
assert_eq!(a.rem_euclid(b), 3.0);
assert_eq!((-a).rem_euclid(b), 1.0);
assert_eq!(a.rem_euclid(-b), 3.0);
assert_eq!((-a).rem_euclid(-b), 1.0);
// 由于舍入误差而造成的限制
assert!((-f32::EPSILON).rem_euclid(3.0) != 0.0);
Run

将数字提高到整数幂。

使用此函数通常比使用 powf 更快

Examples

let x = 2.0_f32;
let abs_difference = (x.powi(2) - (x * x)).abs();

assert!(abs_difference <= f32::EPSILON);
Run

将数字加到浮点幂。

Examples

let x = 2.0_f32;
let abs_difference = (x.powf(2.0) - (x * x)).abs();

assert!(abs_difference <= f32::EPSILON);
Run

返回数字的平方根。

如果 self-0.0 以外的负数,则返回 NaN。

Examples

let positive = 4.0_f32;
let negative = -4.0_f32;
let negative_zero = -0.0_f32;

let abs_difference = (positive.sqrt() - 2.0).abs();

assert!(abs_difference <= f32::EPSILON);
assert!(negative.sqrt().is_nan());
assert!(negative_zero.sqrt() == negative_zero);
Run

返回 e^(self) (指数函数)。

Examples

let one = 1.0f32;
// e^1
let e = one.exp();

// ln(e) - 1 == 0
let abs_difference = (e.ln() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

返回 2^(self)

Examples

let f = 2.0f32;

// 2^2 - 4 == 0
let abs_difference = (f.exp2() - 4.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

返回数字的自然对数。

Examples

let one = 1.0f32;
// e^1
let e = one.exp();

// ln(e) - 1 == 0
let abs_difference = (e.ln() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

返回数字相对于任意基数的对数。

由于实现细节,结果可能无法正确舍入; self.log2() 可以针对基准 2 产生更准确的结果,而 self.log10() 可以针对基准 10 产生更准确的结果。

Examples

let five = 5.0f32;

// log5(5) - 1 == 0
let abs_difference = (five.log(5.0) - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

返回数字的以 2 为底的对数。

Examples

let two = 2.0f32;

// log2(2) - 1 == 0
let abs_difference = (two.log2() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

返回数字的以 10 为底的对数。

Examples

let ten = 10.0f32;

// log10(10) - 1 == 0
let abs_difference = (ten.log10() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run
👎 Deprecated since 1.10.0:

you probably meant (self - other).abs(): this operation is (self - other).max(0.0) except that abs_sub also propagates NaNs (also known as fdimf in C). If you truly need the positive difference, consider using that expression or the C function fdimf, depending on how you wish to handle NaN (please consider filing an issue describing your use-case too).

两个数字的正差。

  • 如果是 self <= other: 0:0
  • Else: self - other

Examples

let x = 3.0f32;
let y = -3.0f32;

let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();

assert!(abs_difference_x <= f32::EPSILON);
assert!(abs_difference_y <= f32::EPSILON);
Run

返回数字的立方根。

Examples

let x = 8.0f32;

// x^(1/3) - 2 == 0
let abs_difference = (x.cbrt() - 2.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

给定长度为 xy 的支路,计算直角三角形的斜边的长度。

Examples

let x = 2.0f32;
let y = 3.0f32;

// sqrt(x^2 + y^2)
let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算数字的正弦 (以弧度为单位)。

Examples

let x = std::f32::consts::FRAC_PI_2;

let abs_difference = (x.sin() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算数字的余弦 (以弧度为单位)。

Examples

let x = 2.0 * std::f32::consts::PI;

let abs_difference = (x.cos() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算一个数的正切 (以弧度为单位)。

Examples

let x = std::f32::consts::FRAC_PI_4;
let abs_difference = (x.tan() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算数字的反正弦。 如果数字超出 [-1, 1] 范围,则返回值的弧度范围为 [-pi/2, pi/2] 或 NaN。

Examples

let f = std::f32::consts::FRAC_PI_2;

// asin(sin(pi/2))
let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算数字的反余弦值。 如果数字超出 [-1, 1] 范围,则返回值的弧度范围为 [0, pi] 或 NaN。

Examples

let f = std::f32::consts::FRAC_PI_4;

// acos(cos(pi/4))
let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算数字的反正切。 返回值的弧度范围为 [-pi/2, pi/2];

Examples

let f = 1.0f32;

// atan(tan(1))
let abs_difference = (f.tan().atan() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

计算弧度 self (y) 和 other (x) 的四个象限反正切。

  • x = 0, y = 0: 0
  • x >= 0: arctan(y/x) -> [-pi/2, pi/2]
  • y >= 0: arctan(y/x) + pi -> (pi/2, pi]
  • y < 0: arctan(y/x) - pi -> (-pi, -pi/2)

Examples

// 从正 x 轴 -pi/4 弧度逆时针测量的正角 (顺时针 45 度)
let x1 = 3.0f32;
let y1 = -3.0f32;

// 3pi/4 弧度 (逆时针 135 度)
let x2 = -3.0f32;
let y2 = 3.0f32;

let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();

assert!(abs_difference_1 <= f32::EPSILON);
assert!(abs_difference_2 <= f32::EPSILON);
Run

同时计算数字的正弦和余弦, x. 返回 (sin(x), cos(x))

Examples

let x = std::f32::consts::FRAC_PI_4;
let f = x.sin_cos();

let abs_difference_0 = (f.0 - x.sin()).abs();
let abs_difference_1 = (f.1 - x.cos()).abs();

assert!(abs_difference_0 <= f32::EPSILON);
assert!(abs_difference_1 <= f32::EPSILON);
Run

即使数字接近零,也以准确的方式返回 e^(self) - 1

Examples

let x = 1e-8_f32;

// 对于非常小的 x,e^x 约为 1 + x + x^2 / 2
let approx = x + x * x / 2.0;
let abs_difference = (x.exp_m1() - approx).abs();

assert!(abs_difference < 1e-10);
Run

与单独执行操作相比,返回 ln(1+n) (自然对数) 的准确性更高。

Examples

let x = 1e-8_f32;

// 对于非常小的 x,ln(1 + x) 大约为 x - x^2 / 2
let approx = x - x * x / 2.0;
let abs_difference = (x.ln_1p() - approx).abs();

assert!(abs_difference < 1e-10);
Run

双曲正弦函数。

Examples

let e = std::f32::consts::E;
let x = 1.0f32;

let f = x.sinh();
// 将 sinh() 求解为 1 得到 `(e^2-1)/(2e)`
let g = ((e * e) - 1.0) / (2.0 * e);
let abs_difference = (f - g).abs();

assert!(abs_difference <= f32::EPSILON);
Run

双曲余弦函数。

Examples

let e = std::f32::consts::E;
let x = 1.0f32;
let f = x.cosh();
// 将 cosh() 求解为 1 可得出此结果
let g = ((e * e) + 1.0) / (2.0 * e);
let abs_difference = (f - g).abs();

// 结果相同
assert!(abs_difference <= f32::EPSILON);
Run

双曲正切函数。

Examples

let e = std::f32::consts::E;
let x = 1.0f32;

let f = x.tanh();
// 将 tanh() 求解为 1 得到 `(1 - e^(-2))/(1 + e^(-2))`
let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
let abs_difference = (f - g).abs();

assert!(abs_difference <= f32::EPSILON);
Run

反双曲正弦函数。

Examples

let x = 1.0f32;
let f = x.sinh().asinh();

let abs_difference = (f - x).abs();

assert!(abs_difference <= f32::EPSILON);
Run

反双曲余弦函数。

Examples

let x = 1.0f32;
let f = x.cosh().acosh();

let abs_difference = (f - x).abs();

assert!(abs_difference <= f32::EPSILON);
Run

反双曲正切函数。

Examples

let e = std::f32::consts::E;
let f = e.tanh().atanh();

let abs_difference = (f - e).abs();

assert!(abs_difference <= 1e-5);
Run
🔬 This is a nightly-only experimental API. (float_interpolation #86269)

startend 之间的线性插值。

这可以在 startend 之间启用线性插值,其中开始由 self == 0.0 表示,endself == 1.0 表示。 这是所有 “transition”、“easing” 或 “step” 函数的基础; 如果您以给定的速率将 self 从 0.0 更改为 1.0,结果将以相似的速率从 start 更改为 end

允许低于 0.0 或高于 1.0 的值,允许您推断 startend 范围之外的值。 这对于可能稍微移动到结尾或开始以获得所需效果的过渡函数也很有用。 在数学上,返回的值等同于 start + self * (end - start),尽管我们做出了一些特别对线性插值有用的特定保证。

这些保证是:

  • 如果 startendfinite,则 0.0 处的值始终为 start,1.0 处的值始终为 end。(exactness)
  • 如果 startendfinite,则值将始终沿 startend (monotonicity) 的方向移动
  • 如果 selffinitestart == end,则任何一点的值将始终为 start == end。(consistency)

f32 内部表示形式的基数或基数。

以 2 为底的有效位数。

以 10 为基数的有效位数的大概数字。

Machine epsilon f32 的值。

这是 1.0 与下一个较大的可表示数字之间的差异。

最小的 f32 有限值。

最小正 f32 正值。

最大的有限 f32 值。

比 2 的最小可能标准幂大一。

2 指数的最大可能乘方。

最小可能的标准幂为 10 指数。

最大可能功效为 10 指数。

不是数字 (NaN)。

无限 (∞)。

负无穷大 (−∞)。

如果此值为 NaN,则返回 true

let nan = f32::NAN;
let f = 7.0_f32;

assert!(nan.is_nan());
assert!(!f.is_nan());
Run

如果此值是正无穷大或负无穷大,则返回 true,否则返回 false

let f = 7.0f32;
let inf = f32::INFINITY;
let neg_inf = f32::NEG_INFINITY;
let nan = f32::NAN;

assert!(!f.is_infinite());
assert!(!nan.is_infinite());

assert!(inf.is_infinite());
assert!(neg_inf.is_infinite());
Run

如果此数字既不是无限的也不是 NaN,则返回 true

let f = 7.0f32;
let inf = f32::INFINITY;
let neg_inf = f32::NEG_INFINITY;
let nan = f32::NAN;

assert!(f.is_finite());

assert!(!nan.is_finite());
assert!(!inf.is_finite());
assert!(!neg_inf.is_finite());
Run

如果数字为 subnormal,则返回 true

let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
let max = f32::MAX;
let lower_than_min = 1.0e-40_f32;
let zero = 0.0_f32;

assert!(!min.is_subnormal());
assert!(!max.is_subnormal());

assert!(!zero.is_subnormal());
assert!(!f32::NAN.is_subnormal());
assert!(!f32::INFINITY.is_subnormal());
// `0` 和 `min` 之间的值是次标准的。
assert!(lower_than_min.is_subnormal());
Run

如果数字不为零,无穷大,subnormalNaN,则返回 true

let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
let max = f32::MAX;
let lower_than_min = 1.0e-40_f32;
let zero = 0.0_f32;

assert!(min.is_normal());
assert!(max.is_normal());

assert!(!zero.is_normal());
assert!(!f32::NAN.is_normal());
assert!(!f32::INFINITY.is_normal());
// `0` 和 `min` 之间的值是次标准的。
assert!(!lower_than_min.is_normal());
Run

返回数字的浮点类别。 如果仅要测试一个属性,则通常使用特定谓词会更快。

use std::num::FpCategory;

let num = 12.4_f32;
let inf = f32::INFINITY;

assert_eq!(num.classify(), FpCategory::Normal);
assert_eq!(inf.classify(), FpCategory::Infinite);
Run

如果 self 具有正号,则返回 true,包括 +0.0,带有正号位和正无穷大的 NaN。

let f = 7.0_f32;
let g = -7.0_f32;

assert!(f.is_sign_positive());
assert!(!g.is_sign_positive());
Run

如果 self 带有负号,则返回 true,包括 -0.0,带有负号位和负无穷大的 NaN。

let f = 7.0f32;
let g = -7.0f32;

assert!(!f.is_sign_negative());
assert!(g.is_sign_negative());
Run

取数字的倒数 (inverse), 1/x.

let x = 2.0_f32;
let abs_difference = (x.recip() - (1.0 / x)).abs();

assert!(abs_difference <= f32::EPSILON);
Run

将弧度转换为度。

let angle = std::f32::consts::PI;

let abs_difference = (angle.to_degrees() - 180.0).abs();

assert!(abs_difference <= f32::EPSILON);
Run

将度数转换为弧度。

let angle = 180.0f32;

let abs_difference = (angle.to_radians() - std::f32::consts::PI).abs();

assert!(abs_difference <= f32::EPSILON);
Run

返回两个数字的最大值。

let x = 1.0f32;
let y = 2.0f32;

assert_eq!(x.max(y), y);
Run

如果参数之一是 NaN,则返回另一个参数。

返回两个数字中的最小值。

let x = 1.0f32;
let y = 2.0f32;

assert_eq!(x.min(y), x);
Run

如果参数之一是 NaN,则返回另一个参数。

舍入为零并转换为任何原始整数类型,前提是该值是有限的并且适合该类型。

let value = 4.6_f32;
let rounded = unsafe { value.to_int_unchecked::<u16>() };
assert_eq!(rounded, 4);

let value = -128.9_f32;
let rounded = unsafe { value.to_int_unchecked::<i8>() };
assert_eq!(rounded, i8::MIN);
Run

Safety

该值必须:

  • 不是 NaN
  • 不是无限的
  • 截断小数部分后,可以在返回类型 Int 中表示

原始 trans 变为 u32

当前,这与所有平台上的 transmute::<f32, u32>(self) 相同。

有关此操作的可移植性的一些讨论,请参见 from_bits (几乎没有问题)。

请注意,此函数与 as 强制转换不同,后者试图保留 数字 值,而不是按位值。

Examples

assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() 没有铸造!
assert_eq!((12.5f32).to_bits(), 0x41480000);
Run

来自 u32 的原始 mut 变。

当前,这与所有平台上的 transmute::<u32, f32>(v) 相同。 事实证明,此方法具有很高的可移植性,其原因有两个:

  • 浮点数和整数在所有受支持的平台上具有相同的字节序。
  • IEEE-754 非常精确地指定了 float 的位布局。

但是,有一个警告: 在 2008 年版本的 IEEE-754 之前,实际上并未指定如何解释 NaN 信令位。 大多数平台 (特别是 x86 和 ARM) 采用了最终在 2008 年标准化的解释,但有些则没有 (特别是 MIPS)。 结果,MIPS 上的所有信令 NaN 都是 x86 上的安静 NaN,反之亦然。

该实现方式不是尝试保留跨信令的信令,而是倾向于保留确切的位。 这意味着,即使通过网络从 x86 机器向 MIPS 机器发送此方法的结果,所有以 NaNs 编码的有效载荷都将保留。

如果此方法的结果仅由产生它们的相同体系结构来操纵,则无需考虑可移植性。

如果输入的不是 NaN,则不存在可移植性问题。

如果您不太在意信号传递 (非常可能),那么就不必担心可移植性。

请注意,此函数与 as 强制转换不同,后者试图保留 数字 值,而不是按位值。

Examples

let v = f32::from_bits(0x41480000);
assert_eq!(v, 12.5);
Run

以大端 (网络) 字节顺序的字节数组形式返回此浮点数的内存表示形式。

Examples

let bytes = 12.5f32.to_be_bytes();
assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]);
Run

以小字节序字节顺序将浮点数的内存表示形式返回为字节数组。

Examples

let bytes = 12.5f32.to_le_bytes();
assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]);
Run

返回此浮点数的内存表示形式,以原生字节顺序的字节数组形式。

由于使用了目标平台的原生字节序,因此,可移植代码应酌情使用 to_be_bytesto_le_bytes

Examples

let bytes = 12.5f32.to_ne_bytes();
assert_eq!(
    bytes,
    if cfg!(target_endian = "big") {
        [0x41, 0x48, 0x00, 0x00]
    } else {
        [0x00, 0x00, 0x48, 0x41]
    }
);
Run

从其表示形式以 big endian 的字节数组创建一个浮点值。

Examples

let value = f32::from_be_bytes([0x41, 0x48, 0x00, 0x00]);
assert_eq!(value, 12.5);
Run

从它的表示形式以 Little Endian 的字节数组创建一个浮点值。

Examples

let value = f32::from_le_bytes([0x00, 0x00, 0x48, 0x41]);
assert_eq!(value, 12.5);
Run

从其表示形式 (以原生字节序形式的字节数组形式) 创建浮点值。

由于使用了目标平台的原生字节序,因此可移植代码可能希望酌情使用 from_be_bytesfrom_le_bytes

Examples

let value = f32::from_ne_bytes(if cfg!(target_endian = "big") {
    [0x41, 0x48, 0x00, 0x00]
} else {
    [0x00, 0x00, 0x48, 0x41]
});
assert_eq!(value, 12.5);
Run
🔬 This is a nightly-only experimental API. (total_cmp #72599)

返回 self 和其他值之间的顺序。 与浮点数之间的标准部分比较不同,此比较始终根据 IEEE 754 (2008 修订版) 浮点标准中定义的 totalOrder 谓词产生排序。 值按以下顺序排序:

  • 负安静 NaN
  • 负信号 NaN
  • 负无穷大
  • 负数
  • 负次正规数
  • 负零
  • 正零
  • 次正数
  • 正数
  • 正无穷大
  • 阳性信号 NaN
  • 积极安静的 NaN

请注意,此函数并不总是与 f32PartialOrdPartialEq 实现一致。特别是,他们将负零和正零视为相等,而 total_cmp 则不一样。

Example

#![feature(total_cmp)]
struct GoodBoy {
    name: String,
    weight: f32,
}

let mut bois = vec![
    GoodBoy { name: "Pucci".to_owned(), weight: 0.1 },
    GoodBoy { name: "Woofer".to_owned(), weight: 99.0 },
    GoodBoy { name: "Yapper".to_owned(), weight: 10.0 },
    GoodBoy { name: "Chonk".to_owned(), weight: f32::INFINITY },
    GoodBoy { name: "Abs. Unit".to_owned(), weight: f32::NAN },
    GoodBoy { name: "Floaty".to_owned(), weight: -5.0 },
];

bois.sort_by(|a, b| a.weight.total_cmp(&b.weight));
Run

除非是 NaN,否则将值限制为一定的时间间隔。

如果 self 大于 max,则返回 max; 如果 self 小于 min,则返回 min。 否则,将返回 self

请注意,如果初始值也为 NaN,则此函数将返回 NaN。

Panics

如果 min > maxmin 为 NaN 或 max 为 NaN,则 Panics。

Examples

assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
Run

Trait Implementations

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

应用 + 运算符后的结果类型。

执行 + 操作。 Read more

执行 += 操作。 Read more

执行 += 操作。 Read more

返回值的副本。 Read more

source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

Returns the default value of 0.0

使用给定的格式化程序格式化该值。 Read more

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

应用 / 运算符后的结果类型。

执行 / 操作。 Read more

执行 /= 操作。 Read more

执行 /= 操作。 Read more

Converts i16 to f32 losslessly.

Converts i8 to f32 losslessly.

Converts u16 to f32 losslessly.

Converts u8 to f32 losslessly.

将以 10 为底的字符串转换为浮点数。 接受可选的十进制指数。

该函数接受诸如以下的字符串

  • ‘3.14’
  • ‘-3.14’
  • ‘2.5E10’, 或等效地, ‘2.5e10’
  • ‘2.5E-10’
  • ‘5.’
  • ‘.5’, 或者,等效地, ‘0.5’
  • ‘inf’, ‘-inf’, ‘NaN’

前导和尾随空格表示错误。

Grammar

遵循以下 EBNF 语法的所有字符串都将导致返回 Ok:

Float  ::= Sign? ( 'inf' | 'NaN' | Number )
Number ::= ( Digit+ |
             Digit+ '.' Digit* |
             Digit* '.' Digit+ ) Exp?
Exp    ::= [eE] Sign? Digit+
Sign   ::= [+-]
Digit  ::= [0-9]

Arguments

  • src - 字符串

返回值

Err(ParseFloatError) 如果字符串不代表有效数字。 否则,为 Ok(n),其中 nsrc 表示的浮点数。

可以从解析中返回的相关错误。

使用给定的格式化程序格式化该值。

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

应用 * 运算符后的结果类型。

执行 * 操作。 Read more

执行 *= 操作。 Read more

执行 *= 操作。 Read more

应用 - 运算符后的结果类型。

执行一元 - 操作。 Read more

应用 - 运算符后的结果类型。

执行一元 - 操作。 Read more

此方法测试 selfother 值是否相等,并由 == 使用。 Read more

此方法测试 !=

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

该方法采用迭代器并通过乘以项从元素生成 SelfRead more

该方法采用迭代器并通过乘以项从元素生成 SelfRead more

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

其余部分来自两个彩车的划分。

余数与除数具有相同的符号,并计算为: x - (x / y).trunc() * y.

Examples

let x: f32 = 50.50;
let y: f32 = 8.125;
let remainder = x - (x / y).trunc() * y;

// 两种操作的答案是 1.75
assert_eq!(x % y, remainder);
Run

应用 % 运算符后的结果类型。

执行 % 操作。 Read more

执行 %= 操作。 Read more

执行 %= 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

执行 -= 操作。 Read more

执行 -= 操作。 Read more

使用迭代器并通过 “summing up” 项从元素生成 Self 的方法。 Read more

使用迭代器并通过 “summing up” 项从元素生成 Self 的方法。 Read more

使用给定的格式化程序格式化该值。

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

获得所有权后的结果类型。

通常通过克隆从借用数据中创建拥有的数据。 Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

将给定值转换为 StringRead more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。