Skip to main content

refloxide_core/
real.rs

1//! Floating-point scalar abstraction for the transfer-matrix kernels.
2//!
3//! [`Real`] is implemented for `f32` and `f64` only. It does not carry
4//! complex or tensor types.
5
6use num_traits::{Float, FloatConst, NumAssign};
7
8/// Floating-point scalar accepted by the kernel (`f32` or `f64`).
9pub trait Real: Float + FloatConst + NumAssign + Send + Sync + std::fmt::Debug + 'static {
10    /// Rounds an `f64` literal to this precision.
11    fn lit(x: f64) -> Self;
12    /// Widens this value to `f64`.
13    fn to_f64_lossless(self) -> f64;
14}
15
16impl Real for f32 {
17    #[inline]
18    fn lit(x: f64) -> Self {
19        x as f32
20    }
21    #[inline]
22    fn to_f64_lossless(self) -> f64 {
23        f64::from(self)
24    }
25}
26
27impl Real for f64 {
28    #[inline]
29    fn lit(x: f64) -> Self {
30        x
31    }
32    #[inline]
33    fn to_f64_lossless(self) -> f64 {
34        self
35    }
36}