Add a bunch of .round()s before conversions

Previously this was leading to inconsistencies when using f32 v.s. f64
This commit is contained in:
2020-12-17 15:09:53 -08:00
parent 75fde43aa7
commit d8ae766099
5 changed files with 12 additions and 9 deletions

View File

@@ -18,8 +18,8 @@ fn main() {
let current_break = 0.5e-9; // time between 'set' pulse and 'clear' pulse
let conductivity = 1.0e9;
let from_m = |m| (m/feat_size) as u32;
let m_to_um = |px| (px * 1e6) as u32;
let from_m = |m: Flt| (m/feat_size).round() as u32;
let m_to_um = |m: Flt| (m * 1e6).round() as u32;
let half_width = width * 0.5;
let half_depth = depth * 0.5;
@@ -76,8 +76,8 @@ fn main() {
std::mem::size_of::<Flt>() * 8,
*size_px,
m_to_um(feat_size),
(peak_current * 1e3) as i64,
(current_duration * 1e12) as i64,
(peak_current * 1e3).round() as i64,
(current_duration * 1e12).round() as i64,
m_to_um(ferro_major),
m_to_um(ferro_minor),
m_to_um(wire_major),

View File

@@ -18,7 +18,7 @@ impl Coord for Meters {
*self
}
fn to_index(&self, feature_size: Flt) -> Index {
Index((self.0 / feature_size).into())
Index((self.0 / feature_size).round().into())
}
fn from_meters(other: Meters, _feature_size: Flt) -> Self {
other

View File

@@ -208,6 +208,9 @@ impl Vec3 {
self.with_mag(1.0)
}
}
pub fn round(&self) -> Vec3 {
Self::new(self.x().round(), self.y().round(), self.z().round())
}
}
impl Into<(Flt, Flt, Flt)> for Vec3 {

View File

@@ -300,7 +300,7 @@ impl AbstractMeasurement for Magnetization {
fn loc(v: Meters) -> String {
let (x, y, z): (Flt, Flt, Flt) = (*v * 1e6).into();
format!("({}, {}, {}) um", x as i32, y as i32, z as i32)
format!("({}, {}, {}) um", x.round() as i32, y.round() as i32, z.round() as i32)
}
/// M

View File

@@ -64,7 +64,7 @@ fn im_size(state: &dyn GenericSim, max_w: u32, max_h: u32) -> (u32, u32) {
let mut height = width * state.height() / state.width();
if height > max_h {
let stretch = max_h as f32 / height as f32;
width = (width as f32 * stretch) as _;
width = (width as f32 * stretch).round() as _;
height = max_h;
}
(width, height)
@@ -249,8 +249,8 @@ impl ImageRenderExt for RgbImage {
fn draw_field_arrow(&mut self, center: Vec2, rel: Vec2, color: Rgb<u8>, alpha: f32) {
let start = (center - rel * 0.5).round();
let end = (center + rel * 0.5).round();
let i_start = (start.x() as _, start.y() as _);
let i_end = (end.x() as _, end.y() as _);
let i_start = (start.x().round() as _, start.y().round() as _);
let i_end = (end.x().round() as _, end.y().round() as _);
let interpolate_with_alpha = |left, right, left_weight| {
pixelops::interpolate(left, right, left_weight*alpha)
};