#[inline]
pub(crate) fn floor_f32(x: f32) -> f32 {
#[cfg(feature = "std")]
{
x.floor()
}
#[cfg(not(feature = "std"))]
{
libm::floorf(x)
}
}
#[inline]
pub(crate) fn sqrt_f32(x: f32) -> f32 {
#[cfg(feature = "std")]
{
x.sqrt()
}
#[cfg(not(feature = "std"))]
{
libm::sqrtf(x)
}
}
#[inline]
pub(crate) fn sqrt_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
x.sqrt()
}
#[cfg(not(feature = "std"))]
{
libm::sqrt(x)
}
}
#[inline]
pub(crate) fn round_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
x.round()
}
#[cfg(not(feature = "std"))]
{
libm::round(x)
}
}
#[cfg(test)]
mod tests {
use super::{floor_f32, round_f64, sqrt_f32, sqrt_f64};
#[test]
fn exact_operations_return_the_ieee_result() {
assert_eq!(floor_f32(-0.5).to_bits(), (-1.0_f32).to_bits());
assert_eq!(floor_f32(2.999_999_8).to_bits(), 2.0_f32.to_bits());
assert_eq!(floor_f32(-0.0).to_bits(), (-0.0_f32).to_bits());
assert_eq!(sqrt_f32(2.25).to_bits(), 1.5_f32.to_bits());
assert_eq!(sqrt_f32(-0.0).to_bits(), (-0.0_f32).to_bits());
assert!(sqrt_f32(-1.0).is_nan());
assert_eq!(sqrt_f64(0.0625).to_bits(), 0.25_f64.to_bits());
assert_eq!(sqrt_f64(2.0).to_bits(), 0x3FF6_A09E_667F_3BCD);
assert_eq!(round_f64(2.5).to_bits(), 3.0_f64.to_bits());
assert_eq!(round_f64(-2.5).to_bits(), (-3.0_f64).to_bits());
assert_eq!(round_f64(-0.4).to_bits(), (-0.0_f64).to_bits());
}
}