gpt4 book ai didi

rust - 如何获得两个整数的底商?

转载 作者:行者123 更新时间:2023-11-29 08:26:11 26 4
gpt4 key购买 nike

我有两个 u64,我想将它们分开并讨论结果。 (因为它们是无符号的,所以 floor 和 round to zero 是相同的。)

我该怎么做?

最佳答案

impl Div<i32> for i32文档说:

This operation rounds towards zero, truncating any fractional part of the exact result.

它适用于所有整数类型:

macro_rules! div_impl_integer {
($($t:ty)*) => ($(
/// This operation rounds towards zero, truncating any
/// fractional part of the exact result.
#[stable(feature = "rust1", since = "1.0.0")]
impl Div for $t {
type Output = $t;

#[inline]
fn div(self, other: $t) -> $t { self / other }
}

forward_ref_binop! { impl Div, div for $t, $t }
)*)
}

div_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }

因此,您需要做的就是正常划分。

assert_eq!(3/2, 1);
assert_eq!(9/4, 2);

关于rust - 如何获得两个整数的底商?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57741820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com