compiler_builtins/math/libm_math/
trunc.rs

1/// Rounds the number toward 0 to the closest integral value (f16).
2///
3/// This effectively removes the decimal part of the number, leaving the integral part.
4#[cfg(f16_enabled)]
5#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6pub fn truncf16(x: f16) -> f16 {
7    super::generic::trunc(x)
8}
9
10/// Rounds the number toward 0 to the closest integral value (f32).
11///
12/// This effectively removes the decimal part of the number, leaving the integral part.
13#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
14pub fn truncf(x: f32) -> f32 {
15    select_implementation! {
16        name: truncf,
17        use_arch: all(target_arch = "wasm32", intrinsics_enabled),
18        args: x,
19    }
20
21    super::generic::trunc(x)
22}
23
24/// Rounds the number toward 0 to the closest integral value (f64).
25///
26/// This effectively removes the decimal part of the number, leaving the integral part.
27#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
28pub fn trunc(x: f64) -> f64 {
29    select_implementation! {
30        name: trunc,
31        use_arch: all(target_arch = "wasm32", intrinsics_enabled),
32        args: x,
33    }
34
35    super::generic::trunc(x)
36}
37
38/// Rounds the number toward 0 to the closest integral value (f128).
39///
40/// This effectively removes the decimal part of the number, leaving the integral part.
41#[cfg(f128_enabled)]
42#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
43pub fn truncf128(x: f128) -> f128 {
44    super::generic::trunc(x)
45}
46
47#[cfg(test)]
48mod tests {
49    #[test]
50    fn sanity_check() {
51        assert_eq!(super::truncf(1.1), 1.0);
52    }
53}