diff options
| author | hachem <im@hachem.wtf> | 2026-08-24 14:48:36 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-24 14:48:36 +0200 |
| commit | 24d639224ca11112025289065d7538851606b56e (patch) | |
| tree | e7e26c89cd20099f03ff32727ba0af2cf4762088 /libpsi-core/src/maths/format.rs | |
| parent | 548dc42d9f454cb8c29fddb88ca41f8b1b595882 (diff) | |
[chore]: unwrap project
Diffstat (limited to 'libpsi-core/src/maths/format.rs')
| -rw-r--r-- | libpsi-core/src/maths/format.rs | 141 |
1 files changed, 0 insertions, 141 deletions
diff --git a/libpsi-core/src/maths/format.rs b/libpsi-core/src/maths/format.rs deleted file mode 100644 index 957f1b6..0000000 --- a/libpsi-core/src/maths/format.rs +++ /dev/null @@ -1,141 +0,0 @@ -use crate::Complex; - -const EPSILON: f64 = 1e-10; -const SQRT_2: f64 = 1.4142135623730951; -const INV_SQRT_2: f64 = 0.7071067811865475; -const INV_SQRT_8: f64 = 0.3535533905932738; -const INV_SQRT_32: f64 = 0.1767766952966369; - -fn approx_eq(a: f64, b: f64) -> bool { - (a - b).abs() < EPSILON -} - -fn format_real_symbolic(v: f64) -> Option<String> { - let abs_v = v.abs(); - let sign = if v < 0.0 { "-" } else { "" }; - - if approx_eq(abs_v, 0.0) { - return Some("0".to_string()); - } - if approx_eq(abs_v, 1.0) { - return Some(format!("{}1", sign)); - } - if approx_eq(abs_v, 0.5) { - return Some(format!("{}½", sign)); - } - if approx_eq(abs_v, 0.25) { - return Some(format!("{}¼", sign)); - } - if approx_eq(abs_v, 0.75) { - return Some(format!("{}¾", sign)); - } - if approx_eq(abs_v, 0.125) { - return Some(format!("{}⅛", sign)); - } - if approx_eq(abs_v, SQRT_2) { - return Some(format!("{}√2", sign)); - } - if approx_eq(abs_v, INV_SQRT_2) { - return Some(format!("{}¹⁄√2", sign)); - } - if approx_eq(abs_v, INV_SQRT_8) { - return Some(format!("{}¹⁄√8", sign)); - } - if approx_eq(abs_v, INV_SQRT_32) { - return Some(format!("{}¹⁄√32", sign)); - } - if approx_eq(abs_v, 2.0) { - return Some(format!("{}2", sign)); - } - if approx_eq(abs_v, 1.0 / 3.0) { - return Some(format!("{}⅓", sign)); - } - if approx_eq(abs_v, 2.0 / 3.0) { - return Some(format!("{}⅔", sign)); - } - - None -} - -pub fn format_amplitude(c: &Complex<f64>) -> String { - let re = c.real; - let im = c.imaginary; - - let re_zero = approx_eq(re.abs(), 0.0); - let im_zero = approx_eq(im.abs(), 0.0); - - if re_zero && im_zero { - return "0".to_string(); - } - - if im_zero { - if let Some(s) = format_real_symbolic(re) { - return s; - } - return format!("{:.4}", re); - } - - if re_zero { - if approx_eq(im.abs(), 1.0) { - return if im > 0.0 { - "i".to_string() - } else { - "-i".to_string() - }; - } - if let Some(s) = format_real_symbolic(im) { - return format!("{}i", s); - } - return format!("{:.4}i", im); - } - - let re_str = format_real_symbolic(re).unwrap_or_else(|| format!("{:.4}", re)); - let im_str = if approx_eq(im.abs(), 1.0) { - if im > 0.0 { - "+i".to_string() - } else { - "-i".to_string() - } - } else { - let im_sym = format_real_symbolic(im.abs()); - let sign = if im > 0.0 { "+" } else { "-" }; - match im_sym { - Some(s) => format!("{}{}i", sign, s.trim_start_matches('-')), - None => format!("{}{:.4}i", sign, im.abs()), - } - }; - - format!("{}{}", re_str, im_str) -} - -pub fn format_probability(p: f64) -> String { - if approx_eq(p, 0.0) { - return "0".to_string(); - } - if approx_eq(p, 1.0) { - return "1".to_string(); - } - if approx_eq(p, 0.5) { - return "½".to_string(); - } - if approx_eq(p, 0.25) { - return "¼".to_string(); - } - if approx_eq(p, 0.75) { - return "¾".to_string(); - } - if approx_eq(p, 0.125) { - return "⅛".to_string(); - } - if approx_eq(p, 0.0625) { - return "¹⁄₁₆".to_string(); - } - if approx_eq(p, 1.0 / 3.0) { - return "⅓".to_string(); - } - if approx_eq(p, 2.0 / 3.0) { - return "⅔".to_string(); - } - - format!("{:.4}", p) -} |
