Primitive Type f32

1.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 (不是一个数字): 此值由 (-1.0).sqrt() 等计算得出。NaN 有一些潜在的意外行为:
    • 它不等于任何浮点数,包括它自己! 这就是 f32 没有实现 Eq trait 的原因。
    • 它既不小于也不大于任何浮点数,因此无法通过默认比较操作进行排序,这就是 f32 没有实现 Ord trait 的原因。
    • 它也被认为是传染的,因为几乎所有操作数之一为 NaN 的计算也会导致 NaN。如果此默认值偏离,此页面上的说明仅明确记录 NaN 操作数的行为。
    • 最后,有多个位模式被认为是 NaN。 Rust 目前不保证 NaN 的位模式在算术运算中得到保留,也不保证它们是可移植的,甚至是完全确定的! 这意味着在检查位模式时可能会有一些令人惊讶的结果,因为相同的计算可能会产生具有不同位模式的 NaN。

当此类型的原始操作 (加法、减法、乘法或除法) 产生的数字不能完全表示为 f32 时,它会根据 IEEE 754-2008 中定义的 roundTiesToEven 方向进行四舍五入。这意味着:

  • 如果存在唯一的最接近的可表示值,则结果是最接近真实值的可表示值。
  • 如果真值恰好在两个可表示值的中间,则结果是具有偶数最低有效二进制数字的结果。
  • 如果真值的大小 ≥ f32::MAX + 2 (f32::MAX_EXP-f32::MANTISSA_DIGITS-1),则结果为 ∞ 或 -∞ (保留真值的符号)。

有关浮点数的更多信息,请参见 维基百科

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

Implementations§

source§

impl f32

source

pub fn floor(self) -> f32

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

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
source

pub fn ceil(self) -> f32

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

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

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

pub fn round(self) -> f32

返回最接近 self 的整数。 如果值介于两个整数之间,则舍入 0.0

Examples
let f = 3.3_f32;
let g = -3.3_f32;
let h = -3.7_f32;
let i = 3.5_f32;
let j = 4.5_f32;

assert_eq!(f.round(), 3.0);
assert_eq!(g.round(), -3.0);
assert_eq!(h.round(), -4.0);
assert_eq!(i.round(), 4.0);
assert_eq!(j.round(), 5.0);
Run
source

pub fn round_ties_even(self) -> f32

🔬This is a nightly-only experimental API. (round_ties_even #96710)

返回最接近整数的数字。 将中途个案四舍五入到具有最低有效数字的数字。

Examples
#![feature(round_ties_even)]

let f = 3.3_f32;
let g = -3.3_f32;
let h = 3.5_f32;
let i = 4.5_f32;

assert_eq!(f.round_ties_even(), 3.0);
assert_eq!(g.round_ties_even(), -3.0);
assert_eq!(h.round_ties_even(), 4.0);
assert_eq!(i.round_ties_even(), 4.0);
Run
source

pub fn trunc(self) -> f32

返回 self 的整数部分。 这意味着非整数总是被截断为零。

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
source

pub fn fract(self) -> f32

返回 self 的小数部分。

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
source

pub fn abs(self) -> f32

计算 self 的绝对值。

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
source

pub fn signum(self) -> f32

返回一个表示 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
1.35.0 · source

pub fn copysign(self, sign: f32) -> f32

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

如果 selfsign 的符号相同,则等于 self,否则等于 -self。 如果 self 是 NaN,则返回符号位为 sign 的 NaN。 但是请注意,通常不能保证在算术运算中保留 NaN 上的符号位。

有关详细信息,请参见 将 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
source

pub fn mul_add(self, a: f32, b: f32) -> f32

融合乘法加法。 仅用一个舍入误差计算 (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
1.38.0 · source

pub fn div_euclid(self, rhs: f32) -> f32

计算欧几里得除法,即 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
1.38.0 · source

pub fn rem_euclid(self, rhs: f32) -> f32

计算 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
source

pub fn powi(self, n: i32) -> f32

将数字提高到整数幂。

使用这个函数通常比使用 powf 更快。 它可能具有与 powf 不同的舍入操作序列,因此不能保证结果一致。

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

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

pub fn powf(self, n: f32) -> f32

将数字加到浮点幂。

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

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

pub fn sqrt(self) -> f32

返回数字的平方根。

如果 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
source

pub fn exp(self) -> f32

返回 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
source

pub fn exp2(self) -> f32

返回 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
source

pub fn ln(self) -> f32

返回数字的自然对数。

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
source

pub fn log(self, base: f32) -> f32

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

由于实现细节,结果可能无法正确四舍五入; 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
source

pub fn log2(self) -> f32

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

Examples
let two = 2.0f32;

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

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

pub fn log10(self) -> f32

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

Examples
let ten = 10.0f32;

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

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

pub fn abs_sub(self, other: f32) -> f32

👎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
  • 否则: 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
source

pub fn cbrt(self) -> f32

返回数字的立方根。

Examples
let x = 8.0f32;

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

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

pub fn hypot(self, other: f32) -> f32

计算原点与欧几里德平面上的点 (x, y) 之间的距离。 等价地,计算直角三角形的斜边长度,其他边的长度分别为 x.abs()y.abs()

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
source

pub fn sin(self) -> f32

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

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

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

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

pub fn cos(self) -> f32

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

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

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

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

pub fn tan(self) -> f32

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

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

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

pub fn asin(self) -> f32

计算数字的反正弦。 如果数字超出 [-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
source

pub fn acos(self) -> f32

计算数字的反余弦值。 如果数字超出 [-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
source

pub fn atan(self) -> f32

计算数字的反正切。 返回值以弧度为单位,范围为 [-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
source

pub fn atan2(self, other: f32) -> f32

计算弧度 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
source

pub fn sin_cos(self) -> (f32, f32)

同时计算数字 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
source

pub fn exp_m1(self) -> f32

即使数字接近零,也以准确的方式返回 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
source

pub fn ln_1p(self) -> f32

与单独执行操作相比,返回 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
source

pub fn sinh(self) -> f32

双曲正弦函数。

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
source

pub fn cosh(self) -> f32

双曲余弦函数。

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
source

pub fn tanh(self) -> f32

双曲正切函数。

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
source

pub fn asinh(self) -> f32

反双曲正弦函数。

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

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

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

pub fn acosh(self) -> f32

反双曲余弦函数。

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

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

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

pub fn atanh(self) -> f32

反双曲正切函数。

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

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

assert!(abs_difference <= 1e-5);
Run
source§

impl f32

1.43.0 · source

pub const RADIX: u32 = 2u32

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

1.43.0 · source

pub const MANTISSA_DIGITS: u32 = 24u32

基数中的有效位数 2.

1.43.0 · source

pub const DIGITS: u32 = 6u32

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

1.43.0 · source

pub const EPSILON: f32 = 1.1920929E-7f32

f32机器精度 值。

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

1.43.0 · source

pub const MIN: f32 = -3.40282347E+38f32

最小的 f32 有限值。

1.43.0 · source

pub const MIN_POSITIVE: f32 = 1.17549435E-38f32

最小正 f32 正值。

1.43.0 · source

pub const MAX: f32 = 3.40282347E+38f32

最大的有限 f32 值。

1.43.0 · source

pub const MIN_EXP: i32 = -125i32

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

1.43.0 · source

pub const MAX_EXP: i32 = 128i32

2 指数的最大可能乘方。

1.43.0 · source

pub const MIN_10_EXP: i32 = -37i32

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

1.43.0 · source

pub const MAX_10_EXP: i32 = 38i32

最大可能功效为 10 指数。

1.43.0 · source

pub const NAN: f32 = NaNf32

不是数字 (NaN)。

请注意,IEEE 754 不只定义一个 NaN 值; 过多的位模式被认为是 NaN。 此外,该标准区分了 “signaling” 和 “quiet” NaN,并允许检查其 “payload” (位模式中未指定的位)。 不保证此特性等于任何特定的 NaN 位模式,并且不保证其表示在 Rust 版本和目标平台上的稳定性。

1.43.0 · source

pub const INFINITY: f32 = +Inff32

无限 (∞)。

1.43.0 · source

pub const NEG_INFINITY: f32 = -Inff32

负无穷大 (−∞)。

const: unstable · source

pub fn is_nan(self) -> bool

如果此值为 NaN,则返回 true

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

assert!(nan.is_nan());
assert!(!f.is_nan());
Run
const: unstable · source

pub fn is_infinite(self) -> bool

如果此值是正无穷大或负无穷大,则返回 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
const: unstable · source

pub fn is_finite(self) -> bool

如果此数字既不是无穷大也不是 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
1.53.0 (const: unstable) · source

pub fn is_subnormal(self) -> bool

如果数字为 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
const: unstable · source

pub fn is_normal(self) -> bool

如果数字既不是零、无穷大、subnormal 或 NaN,则返回 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
const: unstable · source

pub fn classify(self) -> FpCategory

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

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
const: unstable · source

pub fn is_sign_positive(self) -> bool

如果 self 有正号,则返回 true,包括 +0.0、带正号位的 NaN 和正无穷大。 请注意,在 NaN 的情况下,IEEE 754 不会为符号位分配任何含义,并且由于 Rust 不保证 NaN 的位模式在算术运算中保持不变,因此 is_sign_positive 对 NaN 的结果可能会产生意外在某些情况下导致。

有关详细信息,请参见 将 NaN 解释为特殊值

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

assert!(f.is_sign_positive());
assert!(!g.is_sign_positive());
Run
const: unstable · source

pub fn is_sign_negative(self) -> bool

如果 self 具有 negative 符号,则返回 true,包括 -0.0、具有 negative 符号位的 NaN 和 negative 无穷大。 请注意,在 NaN 的情况下,IEEE 754 不会为符号位分配任何含义,并且由于 Rust 不保证 NaN 的位模式在算术运算中保持不变,因此 is_sign_negative 对 NaN 的结果可能会产生意外在某些情况下导致。

有关详细信息,请参见 将 NaN 解释为特殊值

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

assert!(!f.is_sign_negative());
assert!(g.is_sign_negative());
Run
const: unstable · source

pub fn next_up(self) -> f32

🔬This is a nightly-only experimental API. (float_next_up_down #91399)

返回大于 self 的最小数字。

TINY 为可表示的最小正 f32。 Then,

  • 如果是 self.is_nan(),则返回 self;
  • 如果 selfNEG_INFINITY,则返回 MIN;
  • 如果 self-TINY,则返回 - 0.0;
  • 如果 self 为 - 0.0 或 + 0.0,则返回 TINY;
  • 如果 selfMAXINFINITY,则返回 INFINITY;
  • 否则返回大于 self 的唯一最小值。

恒等式 x.next_up() == -(-x).next_down() 适用于所有非 NaN x。当 x 为有限时,x == x.next_up().next_down() 也成立。

#![feature(float_next_up_down)]
// f32::EPSILON 是 1.0 和下一个数字之间的差。
assert_eq!(1.0f32.next_up(), 1.0 + f32::EPSILON);
// 但不适用于大多数数字。
assert!(0.1f32.next_up() < 0.1 + f32::EPSILON);
assert_eq!(16777216f32.next_up(), 16777218.0);
Run
const: unstable · source

pub fn next_down(self) -> f32

🔬This is a nightly-only experimental API. (float_next_up_down #91399)

返回小于 self 的最大数。

TINY 为可表示的最小正 f32。 Then,

  • 如果是 self.is_nan(),则返回 self;
  • 如果 selfINFINITY,则返回 MAX;
  • 如果 selfTINY,则返回 0.0;
  • 如果 self 为 - 0.0 或 + 0.0,则返回 -TINY;
  • 如果 selfMINNEG_INFINITY,则返回 NEG_INFINITY;
  • 否则返回小于 self 的唯一最大值。

恒等式 x.next_down() == -(-x).next_up() 适用于所有非 NaN x。当 x 为有限时,x == x.next_down().next_up() 也成立。

#![feature(float_next_up_down)]
let x = 1.0f32;
// 将值限制在 [0, 1) 范围内。
let clamped = x.clamp(0.0, 1.0f32.next_down());
assert!(clamped < 1.0);
assert_eq!(clamped.next_up(), 1.0);
Run
source

pub fn recip(self) -> f32

取一个数 1/x 的倒数 (inverse)。

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

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

pub fn to_degrees(self) -> f32

将弧度转换为度数。

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

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

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

pub fn to_radians(self) -> f32

将度数转换为弧度。

let angle = 180.0f32;

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

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

pub fn max(self, other: f32) -> f32

返回两个数字中的最大值,忽略 NaN。

如果参数之一是 NaN,则返回另一个参数。 这遵循 maxNum 的 IEEE 754-2008 语义,除了处理信令 NaN; 这个函数以相同的方式处理所有的 NaN,并避免了 maxNum 的关联性问题。 这也符合 libm 的 fmax 的行为。

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

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

pub fn min(self, other: f32) -> f32

返回两个数字中的最小值,忽略 NaN。

如果参数之一是 NaN,则返回另一个参数。 这遵循 minNum 的 IEEE 754-2008 语义,除了处理信令 NaN; 这个函数以相同的方式处理所有的 NaN,并避免了 minNum 的关联性问题。 这也符合 libm 的 fmin 的行为。

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

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

pub fn maximum(self, other: f32) -> f32

🔬This is a nightly-only experimental API. (float_minimum_maximum #91079)

返回两个数字中的最大值,传播 NaN。

这在任一参数为 NaN 时,这将返回 NaN,而 f32::max 仅当两个参数都为 NaN 时才返回 NaN。

#![feature(float_minimum_maximum)]
let x = 1.0f32;
let y = 2.0f32;

assert_eq!(x.maximum(y), y);
assert!(x.maximum(f32::NAN).is_nan());
Run

如果参数之一是 NaN,则返回 NaN。否则,这将返回两个数字中较大的一个。对于此操作,-0.0 被认为小于 +0.0。 请注意,这遵循 IEEE 754-2019 中指定的语义。

另请注意,此处 NaN 的 “propagation” 并不一定意味着 NaN 操作数的位模式是守恒的; 有关详细信息,请参见 将 NaN 解释为特殊值

source

pub fn minimum(self, other: f32) -> f32

🔬This is a nightly-only experimental API. (float_minimum_maximum #91079)

返回两个数字中的最小值,传播 NaN。

当任一参数是 NaN 时,这将返回 NaN,而 f32::min 只有当两个参数都是 NaN 时才返回 NaN。

#![feature(float_minimum_maximum)]
let x = 1.0f32;
let y = 2.0f32;

assert_eq!(x.minimum(y), x);
assert!(x.minimum(f32::NAN).is_nan());
Run

如果参数之一是 NaN,则返回 NaN。否则,这将返回两个数字中的较小者。对于此操作,-0.0 被认为小于 +0.0。 请注意,这遵循 IEEE 754-2019 中指定的语义。

另请注意,此处 NaN 的 “propagation” 并不一定意味着 NaN 操作数的位模式是守恒的; 有关详细信息,请参见 将 NaN 解释为特殊值

source

pub fn midpoint(self, other: f32) -> f32

🔬This is a nightly-only experimental API. (num_midpoint #110840)

计算 selfrhs 的中点。

当 * 参数为 NaN 或者 + inf 和 -inf 的组合作为参数提供时,这将返回 NaN。

Examples
#![feature(num_midpoint)]
assert_eq!(1f32.midpoint(4.0), 2.5);
assert_eq!((-5.5f32).midpoint(8.0), 1.25);
Run
1.44.0 · source

pub unsafe fn to_int_unchecked<Int>(self) -> Intwhere f32: FloatToInt<Int>,

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

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 中表示
1.20.0 (const: unstable) · source

pub fn to_bits(self) -> u32

原始 trans 变为 u32

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

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

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

Examples
assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() 不是 casting!
assert_eq!((12.5f32).to_bits(), 0x41480000);
Run
1.20.0 (const: unstable) · source

pub fn from_bits(v: u32) -> f32

来自 u32 的原始 mut 变。

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

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

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

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

如果这个方法的结果只由产生它们的同一个架构操纵,那么就没有可移植性的问题。

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

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

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

Examples
let v = f32::from_bits(0x41480000);
assert_eq!(v, 12.5);
Run
1.40.0 (const: unstable) · source

pub fn to_be_bytes(self) -> [u8; 4]

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

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

Examples
let bytes = 12.5f32.to_be_bytes();
assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]);
Run
1.40.0 (const: unstable) · source

pub fn to_le_bytes(self) -> [u8; 4]

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

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

Examples
let bytes = 12.5f32.to_le_bytes();
assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]);
Run
1.40.0 (const: unstable) · source

pub fn to_ne_bytes(self) -> [u8; 4]

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

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

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

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
1.40.0 (const: unstable) · source

pub fn from_be_bytes(bytes: [u8; 4]) -> f32

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

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

Examples
let value = f32::from_be_bytes([0x41, 0x48, 0x00, 0x00]);
assert_eq!(value, 12.5);
Run
1.40.0 (const: unstable) · source

pub fn from_le_bytes(bytes: [u8; 4]) -> f32

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

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

Examples
let value = f32::from_le_bytes([0x00, 0x00, 0x48, 0x41]);
assert_eq!(value, 12.5);
Run
1.40.0 (const: unstable) · source

pub fn from_ne_bytes(bytes: [u8; 4]) -> f32

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

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

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

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
1.62.0 · source

pub fn total_cmp(&self, other: &f32) -> Ordering

返回 selfother 之间的顺序。

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

  • 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.

这个函数建立的顺序并不总是与 f32PartialOrdPartialEq 实现一致。 例如,他们认为负零和正零相等,而 total_cmp doesn’t.

信令 NaN 位的解释遵循 IEEE 754 标准中的定义,这可能与一些旧的、不符合标准的 (例如 MIPS) 硬件实现的解释不匹配。

Example
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
1.50.0 · source

pub fn clamp(self, min: f32, max: f32) -> f32

除非是 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§

source§

impl Add<&f32> for &f32

§

type Output = <f32 as Add<f32>>::Output

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

fn add(self, other: &f32) -> <f32 as Add<f32>>::Output

执行 + 操作。 Read more
source§

impl Add<&f32> for f32

§

type Output = <f32 as Add<f32>>::Output

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

fn add(self, other: &f32) -> <f32 as Add<f32>>::Output

执行 + 操作。 Read more
source§

impl<'a> Add<f32> for &'a f32

§

type Output = <f32 as Add<f32>>::Output

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

fn add(self, other: f32) -> <f32 as Add<f32>>::Output

执行 + 操作。 Read more
source§

impl Add<f32> for f32

§

type Output = f32

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

fn add(self, other: f32) -> f32

执行 + 操作。 Read more
1.22.0 · source§

impl AddAssign<&f32> for f32

source§

fn add_assign(&mut self, other: &f32)

执行 += 操作。 Read more
1.8.0 · source§

impl AddAssign<f32> for f32

source§

fn add_assign(&mut self, other: f32)

执行 += 操作。 Read more
source§

impl Clone for f32

source§

fn clone(&self) -> f32

返回值的副本。 Read more
source§

fn clone_from(&mut self, source: &Self)

source 执行复制分配。 Read more
source§

impl Debug for f32

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

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

impl Default for f32

source§

fn default() -> f32

Returns the default value of 0.0

source§

impl Display for f32

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

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

impl Div<&f32> for &f32

§

type Output = <f32 as Div<f32>>::Output

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

fn div(self, other: &f32) -> <f32 as Div<f32>>::Output

执行 / 操作。 Read more
source§

impl Div<&f32> for f32

§

type Output = <f32 as Div<f32>>::Output

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

fn div(self, other: &f32) -> <f32 as Div<f32>>::Output

执行 / 操作。 Read more
source§

impl<'a> Div<f32> for &'a f32

§

type Output = <f32 as Div<f32>>::Output

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

fn div(self, other: f32) -> <f32 as Div<f32>>::Output

执行 / 操作。 Read more
source§

impl Div<f32> for f32

§

type Output = f32

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

fn div(self, other: f32) -> f32

执行 / 操作。 Read more
1.22.0 · source§

impl DivAssign<&f32> for f32

source§

fn div_assign(&mut self, other: &f32)

执行 /= 操作。 Read more
1.8.0 · source§

impl DivAssign<f32> for f32

source§

fn div_assign(&mut self, other: f32)

执行 /= 操作。 Read more
1.68.0 · source§

impl From<bool> for f32

source§

fn from(small: bool) -> f32

无损地将 bool 转换为 f32false 的结果值为正 0.0true 值为 1.0

Examples
let x: f32 = false.into();
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());

let y: f32 = true.into();
assert_eq!(y, 1.0);
Run
1.6.0 · source§

impl From<f32> for f64

source§

fn from(small: f32) -> f64

Converts f32 to f64 losslessly.

1.6.0 · source§

impl From<i16> for f32

source§

fn from(small: i16) -> f32

Converts i16 to f32 losslessly.

1.6.0 · source§

impl From<i8> for f32

source§

fn from(small: i8) -> f32

Converts i8 to f32 losslessly.

1.6.0 · source§

impl From<u16> for f32

source§

fn from(small: u16) -> f32

Converts u16 to f32 losslessly.

1.6.0 · source§

impl From<u8> for f32

source§

fn from(small: u8) -> f32

Converts u8 to f32 losslessly.

source§

impl FromStr for f32

source§

fn from_str(src: &str) -> Result<f32, ParseFloatError>

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

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

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

请注意,字母字符不区分大小写。

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

Grammar

小写时遵循以下 EBNF 语法的所有字符串都将返回 Ok:

Float  ::= Sign? ( 'inf' | 'infinity' | 'nan' | Number )
Number ::= ( Digit+ |
             Digit+ '.' Digit* |
             Digit* '.' Digit+ ) Exp?
Exp    ::= 'e' Sign? Digit+
Sign   ::= [+-]
Digit  ::= [0-9]
Arguments
  • src - 字符串
返回值

Err(ParseFloatError) 如果字符串不代表有效数字。 否则,Ok(n),其中 n 是与 src 表示的数字最接近的可表示浮点数 (遵循与原始运算结果相同的舍入规则)。

§

type Err = ParseFloatError

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

impl LowerExp for f32

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

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

impl Mul<&f32> for &f32

§

type Output = <f32 as Mul<f32>>::Output

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

fn mul(self, other: &f32) -> <f32 as Mul<f32>>::Output

执行 * 操作。 Read more
source§

impl Mul<&f32> for f32

§

type Output = <f32 as Mul<f32>>::Output

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

fn mul(self, other: &f32) -> <f32 as Mul<f32>>::Output

执行 * 操作。 Read more
source§

impl<'a> Mul<f32> for &'a f32

§

type Output = <f32 as Mul<f32>>::Output

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

fn mul(self, other: f32) -> <f32 as Mul<f32>>::Output

执行 * 操作。 Read more
source§

impl Mul<f32> for f32

§

type Output = f32

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

fn mul(self, other: f32) -> f32

执行 * 操作。 Read more
1.22.0 · source§

impl MulAssign<&f32> for f32

source§

fn mul_assign(&mut self, other: &f32)

执行 *= 操作。 Read more
1.8.0 · source§

impl MulAssign<f32> for f32

source§

fn mul_assign(&mut self, other: f32)

执行 *= 操作。 Read more
source§

impl Neg for &f32

§

type Output = <f32 as Neg>::Output

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

fn neg(self) -> <f32 as Neg>::Output

执行一元 - 运算。 Read more
source§

impl Neg for f32

§

type Output = f32

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

fn neg(self) -> f32

执行一元 - 运算。 Read more
source§

impl PartialEq<f32> for f32

source§

fn eq(&self, other: &f32) -> bool

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

fn ne(&self, other: &f32) -> bool

此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。
source§

impl PartialOrd<f32> for f32

source§

fn partial_cmp(&self, other: &f32) -> Option<Ordering>

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

fn lt(&self, other: &f32) -> bool

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

fn le(&self, other: &f32) -> bool

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

fn ge(&self, other: &f32) -> bool

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

fn gt(&self, other: &f32) -> bool

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

impl<'a> Product<&'a f32> for f32

source§

fn product<I>(iter: I) -> f32where I: Iterator<Item = &'a f32>,

该方法采用迭代器并通过乘以项从元素生成 Self
1.12.0 · source§

impl Product<f32> for f32

source§

fn product<I>(iter: I) -> f32where I: Iterator<Item = f32>,

该方法采用迭代器并通过乘以项从元素生成 Self
source§

impl Rem<&f32> for &f32

§

type Output = <f32 as Rem<f32>>::Output

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

fn rem(self, other: &f32) -> <f32 as Rem<f32>>::Output

执行 % 操作。 Read more
source§

impl Rem<&f32> for f32

§

type Output = <f32 as Rem<f32>>::Output

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

fn rem(self, other: &f32) -> <f32 as Rem<f32>>::Output

执行 % 操作。 Read more
source§

impl<'a> Rem<f32> for &'a f32

§

type Output = <f32 as Rem<f32>>::Output

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

fn rem(self, other: f32) -> <f32 as Rem<f32>>::Output

执行 % 操作。 Read more
source§

impl Rem<f32> for f32

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

余数与被除数同号,计算公式为: 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
§

type Output = f32

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

fn rem(self, other: f32) -> f32

执行 % 操作。 Read more
1.22.0 · source§

impl RemAssign<&f32> for f32

source§

fn rem_assign(&mut self, other: &f32)

执行 %= 操作。 Read more
1.8.0 · source§

impl RemAssign<f32> for f32

source§

fn rem_assign(&mut self, other: f32)

执行 %= 操作。 Read more
source§

impl SimdElement for f32

§

type Mask = i32

🔬This is a nightly-only experimental API. (portable_simd #86656)
此元素类型对应的掩码元素类型。
source§

impl Sub<&f32> for &f32

§

type Output = <f32 as Sub<f32>>::Output

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

fn sub(self, other: &f32) -> <f32 as Sub<f32>>::Output

执行 - 操作。 Read more
source§

impl Sub<&f32> for f32

§

type Output = <f32 as Sub<f32>>::Output

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

fn sub(self, other: &f32) -> <f32 as Sub<f32>>::Output

执行 - 操作。 Read more
source§

impl<'a> Sub<f32> for &'a f32

§

type Output = <f32 as Sub<f32>>::Output

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

fn sub(self, other: f32) -> <f32 as Sub<f32>>::Output

执行 - 操作。 Read more
source§

impl Sub<f32> for f32

§

type Output = f32

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

fn sub(self, other: f32) -> f32

执行 - 操作。 Read more
1.22.0 · source§

impl SubAssign<&f32> for f32

source§

fn sub_assign(&mut self, other: &f32)

执行 -= 操作。 Read more
1.8.0 · source§

impl SubAssign<f32> for f32

source§

fn sub_assign(&mut self, other: f32)

执行 -= 操作。 Read more
1.12.0 · source§

impl<'a> Sum<&'a f32> for f32

source§

fn sum<I>(iter: I) -> f32where I: Iterator<Item = &'a f32>,

使用迭代器并通过 “summing up” 项从元素生成 Self 的方法。
1.12.0 · source§

impl Sum<f32> for f32

source§

fn sum<I>(iter: I) -> f32where I: Iterator<Item = f32>,

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

impl UpperExp for f32

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

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

impl Copy for f32

source§

impl FloatToInt<i128> for f32

source§

impl FloatToInt<i16> for f32

source§

impl FloatToInt<i32> for f32

source§

impl FloatToInt<i64> for f32

source§

impl FloatToInt<i8> for f32

source§

impl FloatToInt<isize> for f32

source§

impl FloatToInt<u128> for f32

source§

impl FloatToInt<u16> for f32

source§

impl FloatToInt<u32> for f32

source§

impl FloatToInt<u64> for f32

source§

impl FloatToInt<u8> for f32

source§

impl FloatToInt<usize> for f32

source§

impl SimdCast for f32

Auto Trait Implementations§

§

impl RefUnwindSafe for f32

§

impl Send for f32

§

impl Sync for f32

§

impl Unpin for f32

§

impl UnwindSafe for f32

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

获取 selfTypeIdRead more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

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

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

调用 U::from(self)

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

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

fn to_owned(&self) -> T

从借用的数据创建拥有的数据,通常是通过克隆。 Read more
source§

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

将给定值转换为 StringRead more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。