| // Copyright 2025 the Vello Authors and the Parley Authors |
| // SPDX-License-Identifier: Apache-2.0 OR MIT |
| |
| //! Mathematical helper functions. |
| |
| use core::ops::Sub; |
| |
| // From <https://github.com/linebender/tiny-skia/blob/68b198a7210a6bbf752b43d6bc4db62445730313/path/src/scalar.rs#L12> |
| const SCALAR_NEARLY_ZERO: f32 = 1.0 / (1 << 12) as f32; |
| |
| /// A number of useful methods for f32 numbers. |
| pub(crate) trait FloatExt: Sized + Sub<f32, Output = f32> { |
| /// Whether the number is approximately 0. |
| fn is_nearly_zero(&self) -> bool { |
| self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO) |
| } |
| |
| /// Whether the number is approximately 0, with a given tolerance. |
| fn is_nearly_zero_within_tolerance(&self, tolerance: f32) -> bool; |
| } |
| |
| impl FloatExt for f32 { |
| #[inline(always)] |
| fn is_nearly_zero_within_tolerance(&self, tolerance: f32) -> bool { |
| debug_assert!(tolerance >= 0.0, "tolerance must be positive"); |
| |
| self.abs() <= tolerance |
| } |
| } |