How do I use floating point number literals when using generic types?

前端 未结 3 1388
庸人自扰
庸人自扰 2020-11-29 12:39

Regular float literals do not work:

extern crate num_traits;

use num_traits::float::Float;

fn scale_float(x: T) -> T {
    x * 0.54
}

f         


        
相关标签:
3条回答
  • 2020-11-29 13:12

    In certain cases, you can add a restriction that the generic type must be able to be multiplied by the type of the literal. Here, we allow any type that can be multiplied by a f64 so long as it produces the output type of T via the trait bound Mul<f64, Output = T>:

    use num_traits::float::Float; // 0.2.6
    use std::ops::Mul;
    
    fn scale_float<T>(x: T) -> T
    where
        T: Float + Mul<f64, Output = T>,
    {
        x * 0.54
    }
    
    fn main() {
        let a: f64 = scale_float(1.23);
    }
    

    This may not work directly for the original problem, but it might depending on what concrete types you need to work with.

    0 讨论(0)
  • 2020-11-29 13:15

    You can't create a Float from a literal directly. I suggest an approach similar to the FloatConst trait:

    trait SomeDomainSpecificScaleFactor {
        fn factor() -> Self;
    }
    
    impl SomeDomainSpecificScaleFactor for f32 {
        fn factor() -> Self {
            0.54
        }
    }
    
    impl SomeDomainSpecificScaleFactor for f64 {
        fn factor() -> Self {
            0.54
        }
    }
    
    fn scale_float<T: Float + SomeDomainSpecificScaleFactor>(x: T) -> T {
        x * T::factor()
    }
    

    (link to playground)

    0 讨论(0)
  • 2020-11-29 13:17

    Use the FromPrimitive trait:

    use num_traits::{cast::FromPrimitive, float::Float};
    
    fn scale_float<T: Float + FromPrimitive>(x: T) -> T {
        x * T::from_f64(0.54).unwrap()
    }
    

    Or the standard library From / Into traits

    fn scale_float<T>(x: T) -> T
    where
        T: Float,
        f64: Into<T>
    {
        x * 0.54.into()
    }
    

    See also:

    • How do I use number literals with the Integer trait from the num crate?
    0 讨论(0)
提交回复
热议问题