Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add epsilon API #248

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#![allow(missing_docs)]

use crate::epsilon::Epsilon;

use arrayvec::ArrayVec;

/// Defines a trait that chooses between libstd or libm implementations of float methods.
Expand Down Expand Up @@ -291,6 +293,7 @@ pub fn solve_itp(
mut ya: f64,
mut yb: f64,
) -> f64 {
debug_assert!(epsilon >= Epsilon::default().value);
let n1_2 = (((b - a) / epsilon).log2().ceil() - 1.0).max(0.0) as usize;
let nmax = n0 + n1_2;
let mut scaled_epsilon = epsilon * (1u64 << nmax) as f64;
Expand Down
48 changes: 48 additions & 0 deletions src/epsilon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use core::ops::Mul;

const EPSILON: f64 = f64::EPSILON;
const MAX_10_EXP: i32 = f64::MAX_10_EXP;

/// `kurbo`'s epsilon (ε).
#[repr(packed)]
#[derive(Copy, Clone, Debug)]
pub struct Epsilon {
/// Value of this epsilon. By default, [`f64::EPSILON`].
pub value: f64,
/// Magnitude of this epsilon. By default, [`f64::MAX_10_EXP`].
pub magnitude: i32,
}

impl Default for Epsilon {
#[inline]
fn default() -> Epsilon {
Self::new()
}
}

impl Epsilon {
/// Raises or lowers the magnitude of `kurbo`'s epsilon by `magnitude`, returning a new
/// [`Epsilon`].
///
/// Returns a new `Epsilon` representing **ε × 10_ᵐ_** where _m_ = magnitude.
#[inline]
pub const fn ten_pow(magnitude: i32) -> Self {
debug_assert!(MAX_10_EXP + magnitude <= MAX_10_EXP);

Epsilon {
magnitude: MAX_10_EXP + magnitude,
value: f64::mul(EPSILON, 10.0 * magnitude as f64)
}
}
}

impl Epsilon {
/// Create `kurbo`'s default epsilon.
#[inline]
pub const fn new() -> Self {
Epsilon {
value: EPSILON,
magnitude: MAX_10_EXP
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
//! [`Druid`]: https://docs.rs/druid
//! [`libm`]: https://docs.rs/libm

#![feature(const_fn_floating_point_arithmetic, const_trait_impl, const_ops)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: i can remove these and make the functions non-const but someone here may have better idea

#![forbid(unsafe_code)]
#![deny(missing_docs, clippy::trivially_copy_pass_by_ref)]
#![warn(rustdoc::broken_intra_doc_links)]
Expand All @@ -92,6 +93,7 @@ mod circle;
pub mod common;
mod cubicbez;
mod ellipse;
mod epsilon;
mod insets;
mod line;
mod mindist;
Expand All @@ -115,6 +117,7 @@ pub use crate::bezpath::*;
pub use crate::circle::*;
pub use crate::cubicbez::*;
pub use crate::ellipse::*;
pub use crate::epsilon::*;
pub use crate::insets::*;
pub use crate::line::*;
pub use crate::param_curve::*;
Expand Down