use crate::num::{Complex64, F128};
use crate::value::Array;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Wide {
Bool(bool),
Int(i128),
UInt(u128),
Float(F128),
Complex(Complex64),
}
impl Wide {
pub fn as_f64(self) -> f64 {
match self {
Wide::Bool(b) => b as u8 as f64,
Wide::Int(i) => i as f64,
Wide::UInt(u) => u as f64,
Wide::Float(f) => f.to_f64(),
Wide::Complex(c) => c.re,
}
}
pub fn as_f128(self) -> F128 {
match self {
Wide::Bool(b) => F128::from_f64(b as u8 as f64),
Wide::Int(i) => F128::from_f64(i as f64),
Wide::UInt(u) => F128::from_f64(u as f64),
Wide::Float(f) => f,
Wide::Complex(c) => F128::from_f64(c.re),
}
}
pub fn as_i128(self) -> Option<i128> {
match self {
Wide::Bool(b) => Some(b as i128),
Wide::Int(i) => Some(i),
Wide::UInt(u) => i128::try_from(u).ok(),
Wide::Float(f) => {
let v = f.to_f64();
if v.is_finite() && v.fract() == 0.0 && v.abs() < 1.7e38 {
Some(v as i128)
} else {
None
}
}
Wide::Complex(c) => {
if c.im == 0.0 && c.re.fract() == 0.0 {
Some(c.re as i128)
} else {
None
}
}
}
}
pub fn as_complex(self) -> Complex64 {
match self {
Wide::Complex(c) => c,
other => Complex64::new(other.as_f64(), 0.0),
}
}
pub fn as_bool(self) -> bool {
match self {
Wide::Bool(b) => b,
Wide::Int(i) => i != 0,
Wide::UInt(u) => u != 0,
Wide::Float(f) => f.to_f64() != 0.0,
Wide::Complex(c) => c.re != 0.0 || c.im != 0.0,
}
}
}
pub fn to_wide(a: &Array) -> Vec<Wide> {
match a {
Array::Bool(x) => x.iter().map(|v| Wide::Bool(*v)).collect(),
Array::I8(x) => x.iter().map(|v| Wide::Int(*v as i128)).collect(),
Array::I16(x) => x.iter().map(|v| Wide::Int(*v as i128)).collect(),
Array::I32(x) => x.iter().map(|v| Wide::Int(*v as i128)).collect(),
Array::I64(x) => x.iter().map(|v| Wide::Int(*v as i128)).collect(),
Array::U8(x) => x.iter().map(|v| Wide::UInt(*v as u128)).collect(),
Array::U16(x) => x.iter().map(|v| Wide::UInt(*v as u128)).collect(),
Array::U32(x) => x.iter().map(|v| Wide::UInt(*v as u128)).collect(),
Array::U64(x) => x.iter().map(|v| Wide::UInt(*v as u128)).collect(),
Array::F8E4M3(x) => x.iter().map(|v| Wide::Float(F128::from_f32(v.to_f32()))).collect(),
Array::F8E5M2(x) => x.iter().map(|v| Wide::Float(F128::from_f32(v.to_f32()))).collect(),
Array::F16(x) => x.iter().map(|v| Wide::Float(F128::from_f32(v.to_f32()))).collect(),
Array::BF16(x) => x.iter().map(|v| Wide::Float(F128::from_f32(v.to_f32()))).collect(),
Array::F32(x) => x.iter().map(|v| Wide::Float(F128::from_f32(*v))).collect(),
Array::F64(x) => x.iter().map(|v| Wide::Float(F128::from_f64(*v))).collect(),
Array::F128(x) => x.iter().map(|v| Wide::Float(*v)).collect(),
Array::C64(x) => {
x.iter().map(|v| Wide::Complex(Complex64::new(v.re as f64, v.im as f64))).collect()
}
Array::C128(x) => x.iter().map(|v| Wide::Complex(*v)).collect(),
Array::Str(x) => x
.iter()
.map(|s| {
s.trim()
.parse::<f64>()
.map(|f| Wide::Float(F128::from_f64(f)))
.unwrap_or(Wide::Float(F128::NAN))
})
.collect(),
Array::Bytes(x) => {
x.iter().map(|b| Wide::UInt(b.first().copied().unwrap_or(0) as u128)).collect()
}
}
}