From fcf4d05baa9ae37335cd7bd6abdea0113c643e7d Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 13 Oct 2023 14:41:30 -0700 Subject: [PATCH] Planar64::try_from(f32|f64) --- src/integer.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/integer.rs b/src/integer.rs index 116726e..8100074 100644 --- a/src/integer.rs +++ b/src/integer.rs @@ -399,6 +399,47 @@ impl From for Planar64{ Self(Self::ONE.0*ratio.num/ratio.den.get() as i64) } } +#[derive(Debug)] +enum Planar64TryFromFloatError{ + Nan, + Infinite, + Subnormal, + HighlyNegativeExponent(i16), + HighlyPositiveExponent(i16), +} +fn planar64_from_mes((m,e,s):(u64,i16,i8))->Result{ + if e< -32{ + Err(Planar64TryFromFloatError::HighlyNegativeExponent(e)) + }else if e<32-52{ + Ok(Planar64((m as i64)*(s as i64)< for Planar64{ + type Error=Planar64TryFromFloatError; + fn try_from(value:f32)->Result{ + match value.classify(){ + std::num::FpCategory::Nan=>Err(Self::Error::Nan), + std::num::FpCategory::Infinite=>Err(Self::Error::Infinite), + std::num::FpCategory::Zero=>Ok(Self::ZERO), + std::num::FpCategory::Subnormal=>Err(Self::Error::Subnormal), + std::num::FpCategory::Normal=>planar64_from_mes(integer_decode_f32(value)), + } + } +} +impl TryFrom for Planar64{ + type Error=Planar64TryFromFloatError; + fn try_from(value:f64)->Result{ + match value.classify(){ + std::num::FpCategory::Nan=>Err(Self::Error::Nan), + std::num::FpCategory::Infinite=>Err(Self::Error::Infinite), + std::num::FpCategory::Zero=>Ok(Self::ZERO), + std::num::FpCategory::Subnormal=>Err(Self::Error::Subnormal), + std::num::FpCategory::Normal=>planar64_from_mes(integer_decode_f64(value)), + } + } +} impl std::ops::Neg for Planar64{ type Output=Planar64; #[inline] @@ -544,6 +585,16 @@ impl Into for Planar64Vec3{ ) } } +impl TryFrom<[f32;3]> for Planar64Vec3{ + type Error=Planar64TryFromFloatError; + fn try_from(value:[f32;3])->Result{ + Ok(Self(glam::i64vec3( + Planar64::try_from(value[0])?.0, + Planar64::try_from(value[1])?.0, + Planar64::try_from(value[2])?.0, + ))) + } +} impl std::ops::Neg for Planar64Vec3{ type Output=Planar64Vec3; #[inline]