Skip to content

Commit e43fb41

Browse files
committed
Add from_f64, into_real
1 parent 4204a51 commit e43fb41

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/types.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,31 @@ impl Scalar for f64 {}
4242
impl Scalar for c32 {}
4343
impl Scalar for c64 {}
4444

45-
pub trait RealScalar: Scalar + Float + Sum {}
45+
pub trait RealScalar: Scalar + Float + Sum {
46+
fn from_f64(f64) -> Self;
47+
}
48+
49+
impl RealScalar for f32 {
50+
fn from_f64(f: f64) -> Self {
51+
f as f32
52+
}
53+
}
54+
impl RealScalar for f64 {
55+
fn from_f64(f: f64) -> Self {
56+
f
57+
}
58+
}
4659

47-
impl RealScalar for f32 {}
48-
impl RealScalar for f64 {}
60+
/// Convert `f64` into `RealScalar`
61+
///
62+
/// ```rust
63+
/// fn mult2<A: RealScalar>(a: A) -> A {
64+
/// a * into_real(2.0);
65+
/// }
66+
/// ```
67+
pub fn into_real<T: RealScalar>(f: f64) -> T {
68+
T::from_f64(f)
69+
}
4970

5071
/// Define associating real float type
5172
pub trait AssociatedReal: Sized {

0 commit comments

Comments
 (0)