Missing remainder and rint functions

We generate this code:

mod ffi {
    use std::os::raw::{c_float};
    #[link(name = "m")]
    extern {
        pub fn remainderf(from: c_float, to: c_float) -> c_float;
        pub fn rintf(val: c_float) -> c_float;
    }
}
fn remainder_f32(from: f32, to: f32) -> f32 {
    unsafe { ffi::remainderf(from, to) }
}
fn rint_f32(val: f32) -> f32 {
    unsafe { ffi::rintf(val) }
}

to have the missing remainderf and rintf . But this does not compile on Windows which does not have "m" libray. How to solve that ?

1 Like

rint is f32::round.

I can't find remainderf equivalents, but Analog of c++ std::remainder? suggests that it's easy to implement yourself with what Rust provides, and that asking for it via a Rust language issue might get you a solution in a later version of Rust, since it's an IEEE 754 requirement.

I'm not sure:

man rint gives:

The rint() functions return the integral value nearest to x (according to the prevailing rounding mode) in floating-point format.

The round() functions return the integral value nearest to x rounding half-way cases away from zero, regardless of the current rounding direction.

And Analog of c++ std::remainder? does not give a proper final answer AFAICS.

The current rounding direction in Rust is always "roundTiesToEven"[1], so you maybe want f32::round_ties_even instead of f32::round.


  1. Search https://doc.rust-lang.org/std/primitive.f32.html for "roundTiesToEven" to confirm. ↩ī¸Ž

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.