pub(in crate::svg::parity::flowchart) fn maybe_truncate_data_point(v: f64) -> f64 {
if !v.is_finite() {
return 0.0;
}
let scale = 262_144.0; let scaled = v * scale;
let floor = scaled.floor();
let frac = scaled - floor;
let eps = 1e-12;
let one_third = 1.0 / 3.0;
let two_thirds = 2.0 / 3.0;
let should_truncate = (frac - one_third).abs() < eps || (frac - two_thirds).abs() < eps;
if !should_truncate {
return v;
}
let out = floor / scale;
if out == -0.0 { 0.0 } else { out }
}
pub(in crate::svg::parity::flowchart) fn maybe_snap_data_point_to_f32(v: f64) -> f64 {
if !v.is_finite() {
return 0.0;
}
fn next_up(v: f64) -> f64 {
if !v.is_finite() {
return v;
}
if v == 0.0 {
return f64::from_bits(1);
}
let bits = v.to_bits();
if v > 0.0 {
f64::from_bits(bits + 1)
} else {
f64::from_bits(bits - 1)
}
}
fn next_down(v: f64) -> f64 {
if !v.is_finite() {
return v;
}
if v == 0.0 {
return -f64::from_bits(1);
}
let bits = v.to_bits();
if v > 0.0 {
f64::from_bits(bits - 1)
} else {
f64::from_bits(bits + 1)
}
}
let snapped = (v as f32) as f64;
if !snapped.is_finite() {
return v;
}
let diff = (v - snapped).abs();
if diff > 1e-12 {
return if v == -0.0 { 0.0 } else { v };
}
let v_bits = v.to_bits();
let snapped_bits = snapped.to_bits();
if v_bits == snapped_bits
|| v_bits == next_up(snapped).to_bits()
|| v_bits == next_down(snapped).to_bits()
{
return if v == -0.0 { 0.0 } else { v };
}
if diff < 1e-14 {
if snapped == -0.0 { 0.0 } else { snapped }
} else {
v
}
}