You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pub trait RoundToFraction {
/// Round `float_number` to specified number of digits in the fraction.
/// # Errors
/// Throws error if:
/// * can't instantiate `10.0`
/// * can't convert `digits` to a Float number
/// * `digits` is zero
fn round_to_fraction<F: Float>(&self, digits: u32) -> Result<F>
where
Self: Float,
{
if digits == 0 {
return Err(anyhow::Error::msg("`digits` must be a positive integer"));
}
let ten = F::from(10.0).ok_or(anyhow::Error::msg("Failed to instantiate value `10.0`."))?;
let digits =
F::from(digits).ok_or(anyhow::Error::msg("Failed to convert `digits``to float"))?;
let round_factor = ten * digits;
let rounded_float = (self.mul(round_factor)).round() / round_factor;
Ok(rounded_float)
}
}
The idea is that Self is a float (f32 or f64), and F is another float. They need to somehow recognize that they are the same type so that they can be multiplied with each other.
Suggested Solution
add the ability to specify that Self = F like this:
Problem
I'm trying to implement a trait method like this:
The idea is that Self is a float (f32 or f64), and F is another float. They need to somehow recognize that they are the same type so that they can be multiplied with each other.
Suggested Solution
add the ability to specify that
Self = F
like this:The text was updated successfully, but these errors were encountered: