use crate::CBig;
use oxinum_float::precision::with_precision;
use oxinum_float::DBig;
#[inline]
fn exact_dbig(n: i64) -> DBig {
with_precision(&DBig::from(n), 0)
}
impl From<(DBig, DBig)> for CBig {
fn from((re, im): (DBig, DBig)) -> Self {
CBig::from_parts(re, im)
}
}
impl From<DBig> for CBig {
fn from(re: DBig) -> Self {
CBig::from_real(re)
}
}
impl From<&DBig> for CBig {
fn from(re: &DBig) -> Self {
CBig::from_real(re.clone())
}
}
impl From<(i64, i64)> for CBig {
fn from((re, im): (i64, i64)) -> Self {
CBig::from_parts(exact_dbig(re), exact_dbig(im))
}
}
impl From<i64> for CBig {
fn from(re: i64) -> Self {
CBig::from_real(exact_dbig(re))
}
}
impl CBig {
pub fn to_f64_parts(&self) -> (f64, f64) {
(self.re.to_f64().value(), self.im.to_f64().value())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_dbig_pair() {
let re = DBig::from(7);
let im = DBig::from(-4);
let z: CBig = (re, im).into();
assert_eq!(z.re().to_string(), "7");
assert_eq!(z.im().to_string(), "-4");
}
#[test]
fn from_dbig_lands_on_real_axis() {
let d = DBig::from(5);
let z: CBig = d.into();
assert_eq!(z.re().to_string(), "5");
assert_eq!(z.im().to_string(), "0");
assert!(z.is_real());
}
#[test]
fn from_dbig_ref_lands_on_real_axis() {
let d = DBig::from(9);
let z: CBig = (&d).into();
assert_eq!(z.re().to_string(), "9");
assert_eq!(z.im().to_string(), "0");
assert_eq!(d.to_string(), "9");
}
#[test]
fn from_integer_pair() {
let z: CBig = (1i64, 2i64).into();
assert_eq!(z.re().to_string(), "1");
assert_eq!(z.im().to_string(), "2");
}
#[test]
fn from_integer_lands_on_real_axis() {
let z: CBig = 42i64.into();
assert_eq!(z.re().to_string(), "42");
assert_eq!(z.im().to_string(), "0");
assert!(z.is_real());
}
#[test]
fn to_f64_parts_round_trips() {
let z = CBig::from_f64(3.5, -1.25).expect("finite parts");
assert_eq!(z.to_f64_parts(), (3.5, -1.25));
}
#[test]
fn integer_parts_carry_unlimited_precision() {
let z: CBig = (3i64, 4i64).into();
assert_eq!(
z.re().precision(),
0,
"real part must be unlimited-precision"
);
assert_eq!(
z.im().precision(),
0,
"imag part must be unlimited-precision"
);
let r: CBig = 7i64.into();
assert_eq!(
r.re().precision(),
0,
"real-axis part must be unlimited-precision"
);
assert_eq!(r.im().to_string(), "0");
}
#[test]
fn integer_norm_sqr_is_exact() {
let z: CBig = (3i64, 4i64).into();
assert_eq!(z.norm_sqr().to_string(), "25");
}
#[test]
fn integer_product_is_exact() {
let prod = CBig::from((1i64, 2i64)) * CBig::from((3i64, 4i64));
assert_eq!(prod.re().to_string(), "-5");
assert_eq!(prod.im().to_string(), "10");
}
#[test]
fn integer_large_magnitude_norm_sqr_is_exact() {
let z: CBig = (1_000_000_007i64, 0i64).into();
assert_eq!(z.norm_sqr().to_string(), "1000000014000000049");
}
#[test]
fn integer_i64_max_norm_sqr_is_exact() {
let z: CBig = (i64::MAX, 0i64).into();
assert_eq!(
z.norm_sqr().to_string(),
"85070591730234615847396907784232501249"
);
}
}