Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Regular float literals do not work:

extern crate num_traits;

use num_traits::float::Float;

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

fn main() {
    let a: f64 = scale_float(1.23);
}
error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
6 |     x * 0.54
  |         ^^^^ expected type parameter, found floating-point variable
  |
  = note: expected type `T`
             found type `{float}`
like image 820
goertzenator Avatar asked Jun 08 '18 20:06

goertzenator


1 Answers

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?
like image 182
Shepmaster Avatar answered Mar 22 '23 21:03

Shepmaster