Skip to content

Commit

Permalink
fix: epsilon close function
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewMckee4 committed Aug 18, 2024
1 parent a839604 commit 241a67c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use pyo3::prelude::*;

use crate::utils::geometry::round_to_decimals;

pub mod gds_file_types;

pub const FLOATING_POINT_INACCURACY_ROUND_DECIMALS: u32 = 10;
Expand All @@ -18,5 +20,5 @@ pub fn get_epsilon() -> f64 {
}

pub fn epsilon_is_close(a: f64, b: f64) -> bool {
unsafe { (a - b).abs() < EPSILON }
unsafe { round_to_decimals((a - b).abs(), FLOATING_POINT_INACCURACY_ROUND_DECIMALS) < EPSILON }
}
12 changes: 6 additions & 6 deletions src/point/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Point {
Ok(Some(dy.atan2(dx).to_degrees()))
}

#[pyo3(signature = (other, rel_tol=1e-7, abs_tol=1e-10))]
#[pyo3(signature = (other, rel_tol=1e-6, abs_tol=1e-10))]
pub fn is_close(
&self,
#[pyo3(from_py_with = "py_any_to_point")] other: Point,
Expand Down Expand Up @@ -216,13 +216,13 @@ impl Point {

#[pyo3(signature = (ndigits=None))]
pub fn __round__(&self, ndigits: Option<i32>) -> PyResult<Self> {
let factor = match ndigits {
Some(d) => 10f64.powi(d),
None => 1.0,
let ndigits = match ndigits {
Some(ndigits) => ndigits as u32,
None => 0,
};
Ok(Self {
x: (self.x * factor).round() / factor,
y: (self.y * factor).round() / factor,
x: round_to_decimals(self.x, ndigits),
y: round_to_decimals(self.y, ndigits),
})
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_grid_eq_to_copy(grid: Grid):


@given(grid=grid_strategy(), point=point_strategy())
def test_grid_eq_to_different_origin(grid: Grid, point: Point):
def test_grid_not_eq_to_different_origin(grid: Grid, point: Point):
assume(grid.origin != point)
new_grid = grid.copy()
new_grid.origin = point
Expand Down

0 comments on commit 241a67c

Please sign in to comment.