approx-0.5.0/.cargo_vcs_info.json0000644000000001120000000000000123400ustar { "git": { "sha1": "11f6fdac5a32b9367bd8e6cd780ed539e4e9e142" } } approx-0.5.0/.github/dependabot.yml000064400000000000000000000002210000000000000152770ustar 00000000000000version: 2 updates: - package-ecosystem: cargo directory: "/" schedule: interval: daily time: "19:00" open-pull-requests-limit: 10 approx-0.5.0/.gitignore000064400000000000000000000000220000000000000130760ustar 00000000000000target Cargo.lock approx-0.5.0/.travis.yml000064400000000000000000000007140000000000000132270ustar 00000000000000language: rust rust: - 1.31.0 - nightly - beta - stable cache: cargo env: - CARGO_FEATURES="" - CARGO_FEATURES="std" - CARGO_FEATURES="std num-complex" matrix: allow_failures: - rust: nightly before_script: - if [ "$TRAVIS_RUST_VERSION" == "1.31.0" ]; then rm -f tests/macro_import.rs; fi - cargo build --verbose script: - cargo build --verbose --features "$CARGO_FEATURES" - cargo test --verbose --features "$CARGO_FEATURES" approx-0.5.0/Cargo.toml0000644000000023020000000000000103410ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "approx" version = "0.5.0" authors = ["Brendan Zabarauskas "] description = "Approximate floating point equality comparisons and assertions." homepage = "https://github.com/brendanzab/approx" documentation = "https://docs.rs/approx" readme = "README.md" keywords = ["approximate", "assert", "comparison", "equality", "float"] license = "Apache-2.0" repository = "https://github.com/brendanzab/approx" [package.metadata.docs.rs] features = ["std", "num-complex"] [lib] name = "approx" [dependencies.num-complex] version = "0.4.0" optional = true [dependencies.num-traits] version = "0.2.0" default_features = false [features] default = ["std"] std = [] approx-0.5.0/Cargo.toml.orig000064400000000000000000000013160000000000000140040ustar 00000000000000[package] name = "approx" version = "0.5.0" authors = ["Brendan Zabarauskas "] license = "Apache-2.0" description = "Approximate floating point equality comparisons and assertions." documentation = "https://docs.rs/approx" homepage = "https://github.com/brendanzab/approx" repository = "https://github.com/brendanzab/approx" readme = "README.md" keywords = [ "approximate", "assert", "comparison", "equality", "float", ] [package.metadata.docs.rs] features = ["std", "num-complex"] [lib] name = "approx" [features] default = ["std"] std = [] [dependencies] num-traits = { version = "0.2.0", default_features = false } num-complex = { version = "0.4.0", optional = true } approx-0.5.0/LICENSE000064400000000000000000000261360000000000000121310ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. approx-0.5.0/README.md000064400000000000000000000014740000000000000124010ustar 00000000000000# approx [![Build Status][travis_badge]][travis_url] [![Version][version_badge]][crate_url] [![Documentation][docs_badge]][docs_url] [![Downloads][downloads_badge]][crate_url] [![License][license_badge]][license_url] [travis_badge]: https://travis-ci.org/brendanzab/approx.svg?branch=master [docs_badge]: https://docs.rs/approx/badge.svg [version_badge]: https://img.shields.io/crates/v/approx.svg [license_badge]: https://img.shields.io/crates/l/approx.svg [downloads_badge]: https://img.shields.io/crates/d/approx.svg [travis_url]: https://travis-ci.org/brendanzab/approx [docs_url]: https://docs.rs/approx [crate_url]: https://crates.io/crates/approx [license_url]: https://github.com/brendanzab/approx/blob/master/LICENSE Approximate floating point equality comparisons and assertions for the Rust Programming Language. approx-0.5.0/src/abs_diff_eq.rs000064400000000000000000000117720000000000000145030ustar 00000000000000#[cfg(feature = "num-complex")] use num_complex::Complex; #[cfg(not(feature = "std"))] use num_traits::float::FloatCore; use std::{cell, f32, f64}; /// Equality that is defined using the absolute difference of two numbers. pub trait AbsDiffEq: PartialEq where Rhs: ?Sized, { /// Used for specifying relative comparisons. type Epsilon; /// The default tolerance to use when testing values that are close together. /// /// This is used when no `epsilon` value is supplied to the [`abs_diff_eq!`], [`relative_eq!`], /// or [`ulps_eq!`] macros. fn default_epsilon() -> Self::Epsilon; /// A test for equality that uses the absolute difference to compute the approximate /// equality of two numbers. fn abs_diff_eq(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool; /// The inverse of [`AbsDiffEq::abs_diff_eq`]. fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool { !Self::abs_diff_eq(self, other, epsilon) } } /////////////////////////////////////////////////////////////////////////////////////////////////// // Base implementations /////////////////////////////////////////////////////////////////////////////////////////////////// macro_rules! impl_unsigned_abs_diff_eq { ($T:ident, $default_epsilon:expr) => { impl AbsDiffEq for $T { type Epsilon = $T; #[inline] fn default_epsilon() -> $T { $default_epsilon } #[inline] fn abs_diff_eq(&self, other: &$T, epsilon: $T) -> bool { (if self > other { self - other } else { other - self }) <= epsilon } } }; } impl_unsigned_abs_diff_eq!(u8, 0); impl_unsigned_abs_diff_eq!(u16, 0); impl_unsigned_abs_diff_eq!(u32, 0); impl_unsigned_abs_diff_eq!(u64, 0); impl_unsigned_abs_diff_eq!(usize, 0); macro_rules! impl_signed_abs_diff_eq { ($T:ident, $default_epsilon:expr) => { impl AbsDiffEq for $T { type Epsilon = $T; #[inline] fn default_epsilon() -> $T { $default_epsilon } #[inline] fn abs_diff_eq(&self, other: &$T, epsilon: $T) -> bool { $T::abs(self - other) <= epsilon } } }; } impl_signed_abs_diff_eq!(i8, 0); impl_signed_abs_diff_eq!(i16, 0); impl_signed_abs_diff_eq!(i32, 0); impl_signed_abs_diff_eq!(i64, 0); impl_signed_abs_diff_eq!(isize, 0); impl_signed_abs_diff_eq!(f32, f32::EPSILON); impl_signed_abs_diff_eq!(f64, f64::EPSILON); /////////////////////////////////////////////////////////////////////////////////////////////////// // Derived implementations /////////////////////////////////////////////////////////////////////////////////////////////////// impl<'a, T: AbsDiffEq + ?Sized> AbsDiffEq for &'a T { type Epsilon = T::Epsilon; #[inline] fn default_epsilon() -> T::Epsilon { T::default_epsilon() } #[inline] fn abs_diff_eq(&self, other: &&'a T, epsilon: T::Epsilon) -> bool { T::abs_diff_eq(*self, *other, epsilon) } } impl<'a, T: AbsDiffEq + ?Sized> AbsDiffEq for &'a mut T { type Epsilon = T::Epsilon; #[inline] fn default_epsilon() -> T::Epsilon { T::default_epsilon() } #[inline] fn abs_diff_eq(&self, other: &&'a mut T, epsilon: T::Epsilon) -> bool { T::abs_diff_eq(*self, *other, epsilon) } } impl AbsDiffEq for cell::Cell { type Epsilon = T::Epsilon; #[inline] fn default_epsilon() -> T::Epsilon { T::default_epsilon() } #[inline] fn abs_diff_eq(&self, other: &cell::Cell, epsilon: T::Epsilon) -> bool { T::abs_diff_eq(&self.get(), &other.get(), epsilon) } } impl AbsDiffEq for cell::RefCell { type Epsilon = T::Epsilon; #[inline] fn default_epsilon() -> T::Epsilon { T::default_epsilon() } #[inline] fn abs_diff_eq(&self, other: &cell::RefCell, epsilon: T::Epsilon) -> bool { T::abs_diff_eq(&self.borrow(), &other.borrow(), epsilon) } } impl AbsDiffEq<[B]> for [A] where A: AbsDiffEq, A::Epsilon: Clone, { type Epsilon = A::Epsilon; #[inline] fn default_epsilon() -> A::Epsilon { A::default_epsilon() } #[inline] fn abs_diff_eq(&self, other: &[B], epsilon: A::Epsilon) -> bool { self.len() == other.len() && Iterator::zip(self.iter(), other).all(|(x, y)| A::abs_diff_eq(x, y, epsilon.clone())) } } #[cfg(feature = "num-complex")] impl AbsDiffEq for Complex where T::Epsilon: Clone, { type Epsilon = T::Epsilon; #[inline] fn default_epsilon() -> T::Epsilon { T::default_epsilon() } #[inline] fn abs_diff_eq(&self, other: &Complex, epsilon: T::Epsilon) -> bool { T::abs_diff_eq(&self.re, &other.re, epsilon.clone()) && T::abs_diff_eq(&self.im, &other.im, epsilon.clone()) } } approx-0.5.0/src/lib.rs000064400000000000000000000264150000000000000130270ustar 00000000000000// Copyright 2015 Brendan Zabarauskas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! A crate that provides facilities for testing the approximate equality of floating-point //! based types, using either relative difference, or units in the last place (ULPs) //! comparisons. //! //! You can also use the `*_{eq, ne}!` and `assert_*_{eq, ne}!` macros to test for equality using a //! more positional style: //! //! ```rust //! #[macro_use] //! extern crate approx; //! //! use std::f64; //! //! # fn main() { //! abs_diff_eq!(1.0, 1.0); //! abs_diff_eq!(1.0, 1.0, epsilon = f64::EPSILON); //! //! relative_eq!(1.0, 1.0); //! relative_eq!(1.0, 1.0, epsilon = f64::EPSILON); //! relative_eq!(1.0, 1.0, max_relative = 1.0); //! relative_eq!(1.0, 1.0, epsilon = f64::EPSILON, max_relative = 1.0); //! relative_eq!(1.0, 1.0, max_relative = 1.0, epsilon = f64::EPSILON); //! //! ulps_eq!(1.0, 1.0); //! ulps_eq!(1.0, 1.0, epsilon = f64::EPSILON); //! ulps_eq!(1.0, 1.0, max_ulps = 4); //! ulps_eq!(1.0, 1.0, epsilon = f64::EPSILON, max_ulps = 4); //! ulps_eq!(1.0, 1.0, max_ulps = 4, epsilon = f64::EPSILON); //! # } //! ``` //! //! # Implementing approximate equality for custom types //! //! The `*Eq` traits allow approximate equalities to be implemented on types, based on the //! fundamental floating point implementations. //! //! For example, we might want to be able to do approximate assertions on a complex number type: //! //! ```rust //! #[macro_use] //! extern crate approx; //! # use approx::{AbsDiffEq, RelativeEq, UlpsEq}; //! //! #[derive(Debug, PartialEq)] //! struct Complex { //! x: T, //! i: T, //! } //! # impl AbsDiffEq for Complex where T::Epsilon: Copy { //! # type Epsilon = T::Epsilon; //! # fn default_epsilon() -> T::Epsilon { T::default_epsilon() } //! # fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { //! # T::abs_diff_eq(&self.x, &other.x, epsilon) && //! # T::abs_diff_eq(&self.i, &other.i, epsilon) //! # } //! # } //! # impl RelativeEq for Complex where T::Epsilon: Copy { //! # fn default_max_relative() -> T::Epsilon { T::default_max_relative() } //! # fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) //! # -> bool { //! # T::relative_eq(&self.x, &other.x, epsilon, max_relative) && //! # T::relative_eq(&self.i, &other.i, epsilon, max_relative) //! # } //! # } //! # impl UlpsEq for Complex where T::Epsilon: Copy { //! # fn default_max_ulps() -> u32 { T::default_max_ulps() } //! # fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { //! # T::ulps_eq(&self.x, &other.x, epsilon, max_ulps) && //! # T::ulps_eq(&self.i, &other.i, epsilon, max_ulps) //! # } //! # } //! //! # fn main() { //! let x = Complex { x: 1.2, i: 2.3 }; //! //! assert_relative_eq!(x, x); //! assert_ulps_eq!(x, x, max_ulps = 4); //! # } //! ``` //! //! To do this we can implement [`AbsDiffEq`], [`RelativeEq`] and [`UlpsEq`] generically in terms //! of a type parameter that also implements `AbsDiffEq`, `RelativeEq` and `UlpsEq` respectively. //! This means that we can make comparisons for either `Complex` or `Complex`: //! //! ```rust //! # use approx::{AbsDiffEq, RelativeEq, UlpsEq}; //! # #[derive(Debug, PartialEq)] //! # struct Complex { x: T, i: T, } //! # //! impl AbsDiffEq for Complex where //! T::Epsilon: Copy, //! { //! type Epsilon = T::Epsilon; //! //! fn default_epsilon() -> T::Epsilon { //! T::default_epsilon() //! } //! //! fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { //! T::abs_diff_eq(&self.x, &other.x, epsilon) && //! T::abs_diff_eq(&self.i, &other.i, epsilon) //! } //! } //! //! impl RelativeEq for Complex where //! T::Epsilon: Copy, //! { //! fn default_max_relative() -> T::Epsilon { //! T::default_max_relative() //! } //! //! fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { //! T::relative_eq(&self.x, &other.x, epsilon, max_relative) && //! T::relative_eq(&self.i, &other.i, epsilon, max_relative) //! } //! } //! //! impl UlpsEq for Complex where //! T::Epsilon: Copy, //! { //! fn default_max_ulps() -> u32 { //! T::default_max_ulps() //! } //! //! fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { //! T::ulps_eq(&self.x, &other.x, epsilon, max_ulps) && //! T::ulps_eq(&self.i, &other.i, epsilon, max_ulps) //! } //! } //! ``` //! //! # References //! //! Floating point is hard! Thanks goes to these links for helping to make things a _little_ //! easier to understand: //! //! - [Comparing Floating Point Numbers, 2012 Edition]( //! https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) //! - [The Floating Point Guide - Comparison](http://floating-point-gui.de/errors/comparison/) //! - [What Every Computer Scientist Should Know About Floating-Point Arithmetic]( //! https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) #![cfg_attr(not(feature = "std"), no_std)] #[cfg(feature = "num-complex")] extern crate num_complex; extern crate num_traits; #[cfg(not(feature = "std"))] use core as std; mod abs_diff_eq; mod relative_eq; mod ulps_eq; mod macros; pub use abs_diff_eq::AbsDiffEq; pub use relative_eq::RelativeEq; pub use ulps_eq::UlpsEq; /// The requisite parameters for testing for approximate equality using a /// absolute difference based comparison. /// /// This is not normally used directly, rather via the /// `assert_abs_diff_{eq|ne}!` and `abs_diff_{eq|ne}!` macros. /// /// # Example /// /// ```rust /// use std::f64; /// use approx::AbsDiff; /// /// AbsDiff::default().eq(&1.0, &1.0); /// AbsDiff::default().epsilon(f64::EPSILON).eq(&1.0, &1.0); /// ``` pub struct AbsDiff where A: AbsDiffEq + ?Sized, B: ?Sized, { /// The tolerance to use when testing values that are close together. pub epsilon: A::Epsilon, } impl Default for AbsDiff where A: AbsDiffEq + ?Sized, B: ?Sized, { #[inline] fn default() -> AbsDiff { AbsDiff { epsilon: A::default_epsilon(), } } } impl AbsDiff where A: AbsDiffEq + ?Sized, B: ?Sized, { /// Replace the epsilon value with the one specified. #[inline] pub fn epsilon(self, epsilon: A::Epsilon) -> AbsDiff { AbsDiff { epsilon, ..self } } /// Peform the equality comparison #[inline] #[must_use] pub fn eq(self, lhs: &A, rhs: &B) -> bool { A::abs_diff_eq(lhs, rhs, self.epsilon) } /// Peform the inequality comparison #[inline] #[must_use] pub fn ne(self, lhs: &A, rhs: &B) -> bool { A::abs_diff_ne(lhs, rhs, self.epsilon) } } /// The requisite parameters for testing for approximate equality using a /// relative based comparison. /// /// This is not normally used directly, rather via the /// `assert_relative_{eq|ne}!` and `relative_{eq|ne}!` macros. /// /// # Example /// /// ```rust /// use std::f64; /// use approx::Relative; /// /// Relative::default().eq(&1.0, &1.0); /// Relative::default().epsilon(f64::EPSILON).eq(&1.0, &1.0); /// Relative::default().max_relative(1.0).eq(&1.0, &1.0); /// Relative::default().epsilon(f64::EPSILON).max_relative(1.0).eq(&1.0, &1.0); /// Relative::default().max_relative(1.0).epsilon(f64::EPSILON).eq(&1.0, &1.0); /// ``` pub struct Relative where A: RelativeEq + ?Sized, B: ?Sized, { /// The tolerance to use when testing values that are close together. pub epsilon: A::Epsilon, /// The relative tolerance for testing values that are far-apart. pub max_relative: A::Epsilon, } impl Default for Relative where A: RelativeEq + ?Sized, B: ?Sized, { #[inline] fn default() -> Relative { Relative { epsilon: A::default_epsilon(), max_relative: A::default_max_relative(), } } } impl Relative where A: RelativeEq + ?Sized, B: ?Sized, { /// Replace the epsilon value with the one specified. #[inline] pub fn epsilon(self, epsilon: A::Epsilon) -> Relative { Relative { epsilon, ..self } } /// Replace the maximum relative value with the one specified. #[inline] pub fn max_relative(self, max_relative: A::Epsilon) -> Relative { Relative { max_relative, ..self } } /// Peform the equality comparison #[inline] #[must_use] pub fn eq(self, lhs: &A, rhs: &B) -> bool { A::relative_eq(lhs, rhs, self.epsilon, self.max_relative) } /// Peform the inequality comparison #[inline] #[must_use] pub fn ne(self, lhs: &A, rhs: &B) -> bool { A::relative_ne(lhs, rhs, self.epsilon, self.max_relative) } } /// The requisite parameters for testing for approximate equality using an ULPs /// based comparison. /// /// This is not normally used directly, rather via the `assert_ulps_{eq|ne}!` /// and `ulps_{eq|ne}!` macros. /// /// # Example /// /// ```rust /// use std::f64; /// use approx::Ulps; /// /// Ulps::default().eq(&1.0, &1.0); /// Ulps::default().epsilon(f64::EPSILON).eq(&1.0, &1.0); /// Ulps::default().max_ulps(4).eq(&1.0, &1.0); /// Ulps::default().epsilon(f64::EPSILON).max_ulps(4).eq(&1.0, &1.0); /// Ulps::default().max_ulps(4).epsilon(f64::EPSILON).eq(&1.0, &1.0); /// ``` pub struct Ulps where A: UlpsEq + ?Sized, B: ?Sized, { /// The tolerance to use when testing values that are close together. pub epsilon: A::Epsilon, /// The ULPs to tolerate when testing values that are far-apart. pub max_ulps: u32, } impl Default for Ulps where A: UlpsEq + ?Sized, B: ?Sized, { #[inline] fn default() -> Ulps { Ulps { epsilon: A::default_epsilon(), max_ulps: A::default_max_ulps(), } } } impl Ulps where A: UlpsEq + ?Sized, B: ?Sized, { /// Replace the epsilon value with the one specified. #[inline] pub fn epsilon(self, epsilon: A::Epsilon) -> Ulps { Ulps { epsilon, ..self } } /// Replace the max ulps value with the one specified. #[inline] pub fn max_ulps(self, max_ulps: u32) -> Ulps { Ulps { max_ulps, ..self } } /// Peform the equality comparison #[inline] #[must_use] pub fn eq(self, lhs: &A, rhs: &B) -> bool { A::ulps_eq(lhs, rhs, self.epsilon, self.max_ulps) } /// Peform the inequality comparison #[inline] #[must_use] pub fn ne(self, lhs: &A, rhs: &B) -> bool { A::ulps_ne(lhs, rhs, self.epsilon, self.max_ulps) } } approx-0.5.0/src/macros.rs000064400000000000000000000151160000000000000135410ustar 00000000000000// Copyright 2015 Brendan Zabarauskas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /// Approximate equality of using the absolute difference. #[macro_export] macro_rules! abs_diff_eq { ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => { $crate::AbsDiff::default()$(.$opt($val))*.eq(&$lhs, &$rhs) }; ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => { $crate::AbsDiff::default()$(.$opt($val))*.eq(&$lhs, &$rhs) }; } /// Approximate inequality of using the absolute difference. #[macro_export] macro_rules! abs_diff_ne { ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => { $crate::AbsDiff::default()$(.$opt($val))*.ne(&$lhs, &$rhs) }; ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => { $crate::AbsDiff::default()$(.$opt($val))*.ne(&$lhs, &$rhs) }; } /// Approximate equality using both the absolute difference and relative based comparisons. #[macro_export] macro_rules! relative_eq { ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => { $crate::Relative::default()$(.$opt($val))*.eq(&$lhs, &$rhs) }; ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => { $crate::Relative::default()$(.$opt($val))*.eq(&$lhs, &$rhs) }; } /// Approximate inequality using both the absolute difference and relative based comparisons. #[macro_export] macro_rules! relative_ne { ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => { $crate::Relative::default()$(.$opt($val))*.ne(&$lhs, &$rhs) }; ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => { $crate::Relative::default()$(.$opt($val))*.ne(&$lhs, &$rhs) }; } /// Approximate equality using both the absolute difference and ULPs (Units in Last Place). #[macro_export] macro_rules! ulps_eq { ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => { $crate::Ulps::default()$(.$opt($val))*.eq(&$lhs, &$rhs) }; ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => { $crate::Ulps::default()$(.$opt($val))*.eq(&$lhs, &$rhs) }; } /// Approximate inequality using both the absolute difference and ULPs (Units in Last Place). #[macro_export] macro_rules! ulps_ne { ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => { $crate::Ulps::default()$(.$opt($val))*.ne(&$lhs, &$rhs) }; ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => { $crate::Ulps::default()$(.$opt($val))*.ne(&$lhs, &$rhs) }; } #[doc(hidden)] #[macro_export] macro_rules! __assert_approx { ($eq:ident, $given:expr, $expected:expr) => {{ let (given, expected) = (&($given), &($expected)); if !$eq!(*given, *expected) { panic!( "assert_{}!({}, {}) left = {:?} right = {:?} ", stringify!($eq), stringify!($given), stringify!($expected), given, expected, ); } }}; ($eq:ident, $given:expr, $expected:expr, $($opt:ident = $val:expr),+) => {{ let (given, expected) = (&($given), &($expected)); if !$eq!(*given, *expected, $($opt = $val),+) { panic!( "assert_{}!({}, {}, {}) left = {:?} right = {:?} ", stringify!($eq), stringify!($given), stringify!($expected), stringify!($($opt = $val),+), given, expected, ); } }}; } /// An assertion that delegates to [`abs_diff_eq!`], and panics with a helpful error on failure. #[macro_export(local_inner_macros)] macro_rules! assert_abs_diff_eq { ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => { __assert_approx!(abs_diff_eq, $given, $expected $(, $opt = $val)*) }; ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => { __assert_approx!(abs_diff_eq, $given, $expected $(, $opt = $val)*) }; } /// An assertion that delegates to [`abs_diff_ne!`], and panics with a helpful error on failure. #[macro_export(local_inner_macros)] macro_rules! assert_abs_diff_ne { ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => { __assert_approx!(abs_diff_ne, $given, $expected $(, $opt = $val)*) }; ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => { __assert_approx!(abs_diff_ne, $given, $expected $(, $opt = $val)*) }; } /// An assertion that delegates to [`relative_eq!`], and panics with a helpful error on failure. #[macro_export(local_inner_macros)] macro_rules! assert_relative_eq { ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => { __assert_approx!(relative_eq, $given, $expected $(, $opt = $val)*) }; ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => { __assert_approx!(relative_eq, $given, $expected $(, $opt = $val)*) }; } /// An assertion that delegates to [`relative_ne!`], and panics with a helpful error on failure. #[macro_export(local_inner_macros)] macro_rules! assert_relative_ne { ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => { __assert_approx!(relative_ne, $given, $expected $(, $opt = $val)*) }; ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => { __assert_approx!(relative_ne, $given, $expected $(, $opt = $val)*) }; } /// An assertion that delegates to [`ulps_eq!`], and panics with a helpful error on failure. #[macro_export(local_inner_macros)] macro_rules! assert_ulps_eq { ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => { __assert_approx!(ulps_eq, $given, $expected $(, $opt = $val)*) }; ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => { __assert_approx!(ulps_eq, $given, $expected $(, $opt = $val)*) }; } /// An assertion that delegates to [`ulps_ne!`], and panics with a helpful error on failure. #[macro_export(local_inner_macros)] macro_rules! assert_ulps_ne { ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => { __assert_approx!(ulps_ne, $given, $expected $(, $opt = $val)*) }; ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => { __assert_approx!(ulps_ne, $given, $expected $(, $opt = $val)*) }; } approx-0.5.0/src/relative_eq.rs000064400000000000000000000126660000000000000145640ustar 00000000000000#[cfg(feature = "num-complex")] use num_complex::Complex; #[cfg(not(feature = "std"))] use num_traits::float::FloatCore; use std::{cell, f32, f64}; use AbsDiffEq; /// Equality comparisons between two numbers using both the absolute difference and /// relative based comparisons. pub trait RelativeEq: AbsDiffEq where Rhs: ?Sized, { /// The default relative tolerance for testing values that are far-apart. /// /// This is used when no `max_relative` value is supplied to the [`relative_eq`] macro. fn default_max_relative() -> Self::Epsilon; /// A test for equality that uses a relative comparison if the values are far apart. fn relative_eq( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool; /// The inverse of [`RelativeEq::relative_eq`]. fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool { !Self::relative_eq(self, other, epsilon, max_relative) } } /////////////////////////////////////////////////////////////////////////////////////////////////// // Base implementations /////////////////////////////////////////////////////////////////////////////////////////////////// // Implementation based on: [Comparing Floating Point Numbers, 2012 Edition] // (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) macro_rules! impl_relative_eq { ($T:ident, $U:ident) => { impl RelativeEq for $T { #[inline] fn default_max_relative() -> $T { $T::EPSILON } #[inline] fn relative_eq(&self, other: &$T, epsilon: $T, max_relative: $T) -> bool { // Handle same infinities if self == other { return true; } // Handle remaining infinities if $T::is_infinite(*self) || $T::is_infinite(*other) { return false; } let abs_diff = $T::abs(self - other); // For when the numbers are really close together if abs_diff <= epsilon { return true; } let abs_self = $T::abs(*self); let abs_other = $T::abs(*other); let largest = if abs_other > abs_self { abs_other } else { abs_self }; // Use a relative difference comparison abs_diff <= largest * max_relative } } }; } impl_relative_eq!(f32, i32); impl_relative_eq!(f64, i64); /////////////////////////////////////////////////////////////////////////////////////////////////// // Derived implementations /////////////////////////////////////////////////////////////////////////////////////////////////// impl<'a, T: RelativeEq + ?Sized> RelativeEq for &'a T { #[inline] fn default_max_relative() -> T::Epsilon { T::default_max_relative() } #[inline] fn relative_eq(&self, other: &&'a T, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { T::relative_eq(*self, *other, epsilon, max_relative) } } impl<'a, T: RelativeEq + ?Sized> RelativeEq for &'a mut T { #[inline] fn default_max_relative() -> T::Epsilon { T::default_max_relative() } #[inline] fn relative_eq( &self, other: &&'a mut T, epsilon: T::Epsilon, max_relative: T::Epsilon, ) -> bool { T::relative_eq(*self, *other, epsilon, max_relative) } } impl RelativeEq for cell::Cell { #[inline] fn default_max_relative() -> T::Epsilon { T::default_max_relative() } #[inline] fn relative_eq( &self, other: &cell::Cell, epsilon: T::Epsilon, max_relative: T::Epsilon, ) -> bool { T::relative_eq(&self.get(), &other.get(), epsilon, max_relative) } } impl RelativeEq for cell::RefCell { #[inline] fn default_max_relative() -> T::Epsilon { T::default_max_relative() } #[inline] fn relative_eq( &self, other: &cell::RefCell, epsilon: T::Epsilon, max_relative: T::Epsilon, ) -> bool { T::relative_eq(&self.borrow(), &other.borrow(), epsilon, max_relative) } } impl RelativeEq<[B]> for [A] where A: RelativeEq, A::Epsilon: Clone, { #[inline] fn default_max_relative() -> A::Epsilon { A::default_max_relative() } #[inline] fn relative_eq(&self, other: &[B], epsilon: A::Epsilon, max_relative: A::Epsilon) -> bool { self.len() == other.len() && Iterator::zip(self.iter(), other) .all(|(x, y)| A::relative_eq(x, y, epsilon.clone(), max_relative.clone())) } } #[cfg(feature = "num-complex")] impl RelativeEq for Complex where T::Epsilon: Clone, { #[inline] fn default_max_relative() -> T::Epsilon { T::default_max_relative() } #[inline] fn relative_eq( &self, other: &Complex, epsilon: T::Epsilon, max_relative: T::Epsilon, ) -> bool { T::relative_eq(&self.re, &other.re, epsilon.clone(), max_relative.clone()) && T::relative_eq(&self.im, &other.im, epsilon.clone(), max_relative.clone()) } } approx-0.5.0/src/ulps_eq.rs000064400000000000000000000110310000000000000137150ustar 00000000000000#[cfg(feature = "num-complex")] use num_complex::Complex; #[cfg(not(feature = "std"))] use num_traits::float::FloatCore; use std::{cell, mem}; use AbsDiffEq; /// Equality comparisons between two numbers using both the absolute difference and ULPs /// (Units in Last Place) based comparisons. pub trait UlpsEq: AbsDiffEq where Rhs: ?Sized, { /// The default ULPs to tolerate when testing values that are far-apart. /// /// This is used when no `max_ulps` value is supplied to the [`ulps_eq`] macro. fn default_max_ulps() -> u32; /// A test for equality that uses units in the last place (ULP) if the values are far apart. fn ulps_eq(&self, other: &Rhs, epsilon: Self::Epsilon, max_ulps: u32) -> bool; /// The inverse of [`UlpsEq::ulps_eq`]. fn ulps_ne(&self, other: &Rhs, epsilon: Self::Epsilon, max_ulps: u32) -> bool { !Self::ulps_eq(self, other, epsilon, max_ulps) } } /////////////////////////////////////////////////////////////////////////////////////////////////// // Base implementations /////////////////////////////////////////////////////////////////////////////////////////////////// // Implementation based on: [Comparing Floating Point Numbers, 2012 Edition] // (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) macro_rules! impl_ulps_eq { ($T:ident, $U:ident) => { impl UlpsEq for $T { #[inline] fn default_max_ulps() -> u32 { 4 } #[inline] fn ulps_eq(&self, other: &$T, epsilon: $T, max_ulps: u32) -> bool { // For when the numbers are really close together if $T::abs_diff_eq(self, other, epsilon) { return true; } // Trivial negative sign check if self.signum() != other.signum() { return false; } // ULPS difference comparison let int_self: $U = unsafe { mem::transmute(*self) }; let int_other: $U = unsafe { mem::transmute(*other) }; $U::abs(int_self - int_other) <= max_ulps as $U } } }; } impl_ulps_eq!(f32, i32); impl_ulps_eq!(f64, i64); /////////////////////////////////////////////////////////////////////////////////////////////////// // Derived implementations /////////////////////////////////////////////////////////////////////////////////////////////////// impl<'a, T: UlpsEq + ?Sized> UlpsEq for &'a T { #[inline] fn default_max_ulps() -> u32 { T::default_max_ulps() } #[inline] fn ulps_eq(&self, other: &&'a T, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(*self, *other, epsilon, max_ulps) } } impl<'a, T: UlpsEq + ?Sized> UlpsEq for &'a mut T { #[inline] fn default_max_ulps() -> u32 { T::default_max_ulps() } #[inline] fn ulps_eq(&self, other: &&'a mut T, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(*self, *other, epsilon, max_ulps) } } impl UlpsEq for cell::Cell { #[inline] fn default_max_ulps() -> u32 { T::default_max_ulps() } #[inline] fn ulps_eq(&self, other: &cell::Cell, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(&self.get(), &other.get(), epsilon, max_ulps) } } impl UlpsEq for cell::RefCell { #[inline] fn default_max_ulps() -> u32 { T::default_max_ulps() } #[inline] fn ulps_eq(&self, other: &cell::RefCell, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(&self.borrow(), &other.borrow(), epsilon, max_ulps) } } impl UlpsEq<[B]> for [A] where A: UlpsEq, A::Epsilon: Clone, { #[inline] fn default_max_ulps() -> u32 { A::default_max_ulps() } #[inline] fn ulps_eq(&self, other: &[B], epsilon: A::Epsilon, max_ulps: u32) -> bool { self.len() == other.len() && Iterator::zip(self.iter(), other) .all(|(x, y)| A::ulps_eq(x, y, epsilon.clone(), max_ulps.clone())) } } #[cfg(feature = "num-complex")] impl UlpsEq for Complex where T::Epsilon: Clone, { #[inline] fn default_max_ulps() -> u32 { T::default_max_ulps() } #[inline] fn ulps_eq(&self, other: &Complex, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(&self.re, &other.re, epsilon.clone(), max_ulps) && T::ulps_eq(&self.im, &other.im, epsilon.clone(), max_ulps) } } approx-0.5.0/tests/abs_diff_eq.rs000064400000000000000000000355200000000000000150530ustar 00000000000000// Copyright 2015 Brendan Zabarauskas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Test cases derived from: // https://github.com/Pybonacci/puntoflotante.org/blob/master/content/errors/NearlyEqualsTest.java #[macro_use] extern crate approx; mod test_f32 { use std::f32; #[test] fn test_basic() { assert_abs_diff_eq!(1.0f32, 1.0f32); assert_abs_diff_ne!(1.0f32, 2.0f32); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_abs_diff_eq!(1.0f32, 2.0f32); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_abs_diff_ne!(1.0f32, 1.0f32); } #[test] fn test_big() { assert_abs_diff_eq!(100000000.0f32, 100000001.0f32); assert_abs_diff_eq!(100000001.0f32, 100000000.0f32); assert_abs_diff_ne!(10000.0f32, 10001.0f32); assert_abs_diff_ne!(10001.0f32, 10000.0f32); } #[test] fn test_big_neg() { assert_abs_diff_eq!(-100000000.0f32, -100000001.0f32); assert_abs_diff_eq!(-100000001.0f32, -100000000.0f32); assert_abs_diff_ne!(-10000.0f32, -10001.0f32); assert_abs_diff_ne!(-10001.0f32, -10000.0f32); } #[test] fn test_mid() { assert_abs_diff_eq!(1.0000001f32, 1.0000002f32); assert_abs_diff_eq!(1.0000002f32, 1.0000001f32); assert_abs_diff_ne!(1.000001f32, 1.000002f32); assert_abs_diff_ne!(1.000002f32, 1.000001f32); } #[test] fn test_mid_neg() { assert_abs_diff_eq!(-1.0000001f32, -1.0000002f32); assert_abs_diff_eq!(-1.0000002f32, -1.0000001f32); assert_abs_diff_ne!(-1.000001f32, -1.000002f32); assert_abs_diff_ne!(-1.000002f32, -1.000001f32); } #[test] fn test_small() { assert_abs_diff_eq!(0.000010001f32, 0.000010002f32); assert_abs_diff_eq!(0.000010002f32, 0.000010001f32); assert_abs_diff_ne!(0.000001002f32, 0.0000001001f32); assert_abs_diff_ne!(0.000001001f32, 0.0000001002f32); } #[test] fn test_small_neg() { assert_abs_diff_eq!(-0.000010001f32, -0.000010002f32); assert_abs_diff_eq!(-0.000010002f32, -0.000010001f32); assert_abs_diff_ne!(-0.000001002f32, -0.0000001001f32); assert_abs_diff_ne!(-0.000001001f32, -0.0000001002f32); } #[test] fn test_zero() { assert_abs_diff_eq!(0.0f32, 0.0f32); assert_abs_diff_eq!(0.0f32, -0.0f32); assert_abs_diff_eq!(-0.0f32, -0.0f32); assert_abs_diff_ne!(0.000001f32, 0.0f32); assert_abs_diff_ne!(0.0f32, 0.000001f32); assert_abs_diff_ne!(-0.000001f32, 0.0f32); assert_abs_diff_ne!(0.0f32, -0.000001f32); } #[test] fn test_epsilon() { assert_abs_diff_eq!(0.0f32, 1e-40f32, epsilon = 1e-40f32); assert_abs_diff_eq!(1e-40f32, 0.0f32, epsilon = 1e-40f32); assert_abs_diff_eq!(0.0f32, -1e-40f32, epsilon = 1e-40f32); assert_abs_diff_eq!(-1e-40f32, 0.0f32, epsilon = 1e-40f32); assert_abs_diff_ne!(1e-40f32, 0.0f32, epsilon = 1e-41f32); assert_abs_diff_ne!(0.0f32, 1e-40f32, epsilon = 1e-41f32); assert_abs_diff_ne!(-1e-40f32, 0.0f32, epsilon = 1e-41f32); assert_abs_diff_ne!(0.0f32, -1e-40f32, epsilon = 1e-41f32); } #[test] fn test_max() { assert_abs_diff_eq!(f32::MAX, f32::MAX); assert_abs_diff_ne!(f32::MAX, -f32::MAX); assert_abs_diff_ne!(-f32::MAX, f32::MAX); assert_abs_diff_ne!(f32::MAX, f32::MAX / 2.0); assert_abs_diff_ne!(f32::MAX, -f32::MAX / 2.0); assert_abs_diff_ne!(-f32::MAX, f32::MAX / 2.0); } // NOTE: abs_diff_eq fails as numbers begin to get very large // #[test] // fn test_infinity() { // assert_abs_diff_eq!(f32::INFINITY, f32::INFINITY); // assert_abs_diff_eq!(f32::NEG_INFINITY, f32::NEG_INFINITY); // assert_abs_diff_ne!(f32::NEG_INFINITY, f32::INFINITY); // assert_abs_diff_eq!(f32::INFINITY, f32::MAX); // assert_abs_diff_eq!(f32::NEG_INFINITY, -f32::MAX); // } #[test] fn test_nan() { assert_abs_diff_ne!(f32::NAN, f32::NAN); assert_abs_diff_ne!(f32::NAN, 0.0); assert_abs_diff_ne!(-0.0, f32::NAN); assert_abs_diff_ne!(f32::NAN, -0.0); assert_abs_diff_ne!(0.0, f32::NAN); assert_abs_diff_ne!(f32::NAN, f32::INFINITY); assert_abs_diff_ne!(f32::INFINITY, f32::NAN); assert_abs_diff_ne!(f32::NAN, f32::NEG_INFINITY); assert_abs_diff_ne!(f32::NEG_INFINITY, f32::NAN); assert_abs_diff_ne!(f32::NAN, f32::MAX); assert_abs_diff_ne!(f32::MAX, f32::NAN); assert_abs_diff_ne!(f32::NAN, -f32::MAX); assert_abs_diff_ne!(-f32::MAX, f32::NAN); assert_abs_diff_ne!(f32::NAN, f32::MIN_POSITIVE); assert_abs_diff_ne!(f32::MIN_POSITIVE, f32::NAN); assert_abs_diff_ne!(f32::NAN, -f32::MIN_POSITIVE); assert_abs_diff_ne!(-f32::MIN_POSITIVE, f32::NAN); } #[test] fn test_opposite_signs() { assert_abs_diff_ne!(1.000000001f32, -1.0f32); assert_abs_diff_ne!(-1.0f32, 1.000000001f32); assert_abs_diff_ne!(-1.000000001f32, 1.0f32); assert_abs_diff_ne!(1.0f32, -1.000000001f32); assert_abs_diff_eq!(10.0 * f32::MIN_POSITIVE, 10.0 * -f32::MIN_POSITIVE); } #[test] fn test_close_to_zero() { assert_abs_diff_eq!(f32::MIN_POSITIVE, f32::MIN_POSITIVE); assert_abs_diff_eq!(f32::MIN_POSITIVE, -f32::MIN_POSITIVE); assert_abs_diff_eq!(-f32::MIN_POSITIVE, f32::MIN_POSITIVE); assert_abs_diff_eq!(f32::MIN_POSITIVE, 0.0f32); assert_abs_diff_eq!(0.0f32, f32::MIN_POSITIVE); assert_abs_diff_eq!(-f32::MIN_POSITIVE, 0.0f32); assert_abs_diff_eq!(0.0f32, -f32::MIN_POSITIVE); assert_abs_diff_ne!(0.000001f32, -f32::MIN_POSITIVE); assert_abs_diff_ne!(0.000001f32, f32::MIN_POSITIVE); assert_abs_diff_ne!(f32::MIN_POSITIVE, 0.000001f32); assert_abs_diff_ne!(-f32::MIN_POSITIVE, 0.000001f32); } } #[cfg(test)] mod test_f64 { use std::f64; #[test] fn test_basic() { assert_abs_diff_eq!(1.0f64, 1.0f64); assert_abs_diff_ne!(1.0f64, 2.0f64); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_abs_diff_eq!(1.0f64, 2.0f64); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_abs_diff_ne!(1.0f64, 1.0f64); } #[test] fn test_big() { assert_abs_diff_eq!(10000000000000000.0f64, 10000000000000001.0f64); assert_abs_diff_eq!(10000000000000001.0f64, 10000000000000000.0f64); assert_abs_diff_ne!(1000000000000000.0f64, 1000000000000001.0f64); assert_abs_diff_ne!(1000000000000001.0f64, 1000000000000000.0f64); } #[test] fn test_big_neg() { assert_abs_diff_eq!(-10000000000000000.0f64, -10000000000000001.0f64); assert_abs_diff_eq!(-10000000000000001.0f64, -10000000000000000.0f64); assert_abs_diff_ne!(-1000000000000000.0f64, -1000000000000001.0f64); assert_abs_diff_ne!(-1000000000000001.0f64, -1000000000000000.0f64); } #[test] fn test_mid() { assert_abs_diff_eq!(1.0000000000000001f64, 1.0000000000000002f64); assert_abs_diff_eq!(1.0000000000000002f64, 1.0000000000000001f64); assert_abs_diff_ne!(1.000000000000001f64, 1.000000000000002f64); assert_abs_diff_ne!(1.000000000000002f64, 1.000000000000001f64); } #[test] fn test_mid_neg() { assert_abs_diff_eq!(-1.0000000000000001f64, -1.0000000000000002f64); assert_abs_diff_eq!(-1.0000000000000002f64, -1.0000000000000001f64); assert_abs_diff_ne!(-1.000000000000001f64, -1.000000000000002f64); assert_abs_diff_ne!(-1.000000000000002f64, -1.000000000000001f64); } #[test] fn test_small() { assert_abs_diff_eq!(0.0000000100000001f64, 0.0000000100000002f64); assert_abs_diff_eq!(0.0000000100000002f64, 0.0000000100000001f64); assert_abs_diff_ne!(0.0000000100000001f64, 0.0000000010000002f64); assert_abs_diff_ne!(0.0000000100000002f64, 0.0000000010000001f64); } #[test] fn test_small_neg() { assert_abs_diff_eq!(-0.0000000100000001f64, -0.0000000100000002f64); assert_abs_diff_eq!(-0.0000000100000002f64, -0.0000000100000001f64); assert_abs_diff_ne!(-0.0000000100000001f64, -0.0000000010000002f64); assert_abs_diff_ne!(-0.0000000100000002f64, -0.0000000010000001f64); } #[test] fn test_zero() { assert_abs_diff_eq!(0.0f64, 0.0f64); assert_abs_diff_eq!(0.0f64, -0.0f64); assert_abs_diff_eq!(-0.0f64, -0.0f64); assert_abs_diff_ne!(0.000000000000001f64, 0.0f64); assert_abs_diff_ne!(0.0f64, 0.000000000000001f64); assert_abs_diff_ne!(-0.000000000000001f64, 0.0f64); assert_abs_diff_ne!(0.0f64, -0.000000000000001f64); } #[test] fn test_epsilon() { assert_abs_diff_eq!(0.0f64, 1e-40f64, epsilon = 1e-40f64); assert_abs_diff_eq!(1e-40f64, 0.0f64, epsilon = 1e-40f64); assert_abs_diff_eq!(0.0f64, -1e-40f64, epsilon = 1e-40f64); assert_abs_diff_eq!(-1e-40f64, 0.0f64, epsilon = 1e-40f64); assert_abs_diff_ne!(1e-40f64, 0.0f64, epsilon = 1e-41f64); assert_abs_diff_ne!(0.0f64, 1e-40f64, epsilon = 1e-41f64); assert_abs_diff_ne!(-1e-40f64, 0.0f64, epsilon = 1e-41f64); assert_abs_diff_ne!(0.0f64, -1e-40f64, epsilon = 1e-41f64); } #[test] fn test_max() { assert_abs_diff_eq!(f64::MAX, f64::MAX); assert_abs_diff_ne!(f64::MAX, -f64::MAX); assert_abs_diff_ne!(-f64::MAX, f64::MAX); assert_abs_diff_ne!(f64::MAX, f64::MAX / 2.0); assert_abs_diff_ne!(f64::MAX, -f64::MAX / 2.0); assert_abs_diff_ne!(-f64::MAX, f64::MAX / 2.0); } // NOTE: abs_diff_eq fails as numbers begin to get very large // #[test] // fn test_infinity() { // assert_abs_diff_eq!(f64::INFINITY, f64::INFINITY); // assert_abs_diff_eq!(f64::NEG_INFINITY, f64::NEG_INFINITY); // assert_abs_diff_ne!(f64::NEG_INFINITY, f64::INFINITY); // assert_abs_diff_eq!(f64::INFINITY, f64::MAX); // assert_abs_diff_eq!(f64::NEG_INFINITY, -f64::MAX); // } #[test] fn test_nan() { assert_abs_diff_ne!(f64::NAN, f64::NAN); assert_abs_diff_ne!(f64::NAN, 0.0); assert_abs_diff_ne!(-0.0, f64::NAN); assert_abs_diff_ne!(f64::NAN, -0.0); assert_abs_diff_ne!(0.0, f64::NAN); assert_abs_diff_ne!(f64::NAN, f64::INFINITY); assert_abs_diff_ne!(f64::INFINITY, f64::NAN); assert_abs_diff_ne!(f64::NAN, f64::NEG_INFINITY); assert_abs_diff_ne!(f64::NEG_INFINITY, f64::NAN); assert_abs_diff_ne!(f64::NAN, f64::MAX); assert_abs_diff_ne!(f64::MAX, f64::NAN); assert_abs_diff_ne!(f64::NAN, -f64::MAX); assert_abs_diff_ne!(-f64::MAX, f64::NAN); assert_abs_diff_ne!(f64::NAN, f64::MIN_POSITIVE); assert_abs_diff_ne!(f64::MIN_POSITIVE, f64::NAN); assert_abs_diff_ne!(f64::NAN, -f64::MIN_POSITIVE); assert_abs_diff_ne!(-f64::MIN_POSITIVE, f64::NAN); } #[test] fn test_opposite_signs() { assert_abs_diff_ne!(1.000000001f64, -1.0f64); assert_abs_diff_ne!(-1.0f64, 1.000000001f64); assert_abs_diff_ne!(-1.000000001f64, 1.0f64); assert_abs_diff_ne!(1.0f64, -1.000000001f64); assert_abs_diff_eq!(10.0 * f64::MIN_POSITIVE, 10.0 * -f64::MIN_POSITIVE); } #[test] fn test_close_to_zero() { assert_abs_diff_eq!(f64::MIN_POSITIVE, f64::MIN_POSITIVE); assert_abs_diff_eq!(f64::MIN_POSITIVE, -f64::MIN_POSITIVE); assert_abs_diff_eq!(-f64::MIN_POSITIVE, f64::MIN_POSITIVE); assert_abs_diff_eq!(f64::MIN_POSITIVE, 0.0f64); assert_abs_diff_eq!(0.0f64, f64::MIN_POSITIVE); assert_abs_diff_eq!(-f64::MIN_POSITIVE, 0.0f64); assert_abs_diff_eq!(0.0f64, -f64::MIN_POSITIVE); assert_abs_diff_ne!(0.000000000000001f64, -f64::MIN_POSITIVE); assert_abs_diff_ne!(0.000000000000001f64, f64::MIN_POSITIVE); assert_abs_diff_ne!(f64::MIN_POSITIVE, 0.000000000000001f64); assert_abs_diff_ne!(-f64::MIN_POSITIVE, 0.000000000000001f64); } } mod test_ref { mod test_f32 { #[test] fn test_basic() { assert_abs_diff_eq!(&1.0f32, &1.0f32); assert_abs_diff_ne!(&1.0f32, &2.0f32); } } mod test_f64 { #[test] fn test_basic() { assert_abs_diff_eq!(&1.0f64, &1.0f64); assert_abs_diff_ne!(&1.0f64, &2.0f64); } } } mod test_slice { mod test_f32 { #[test] fn test_basic() { assert_abs_diff_eq!([1.0f32, 2.0f32][..], [1.0f32, 2.0f32][..]); assert_abs_diff_ne!([1.0f32, 2.0f32][..], [2.0f32, 1.0f32][..]); } } mod test_f64 { #[test] fn test_basic() { assert_abs_diff_eq!([1.0f64, 2.0f64][..], [1.0f64, 2.0f64][..]); assert_abs_diff_ne!([1.0f64, 2.0f64][..], [2.0f64, 1.0f64][..]); } } } #[cfg(feature = "num-complex")] mod test_complex { extern crate num_complex; pub use self::num_complex::Complex; mod test_f32 { use super::Complex; #[test] fn test_basic() { assert_abs_diff_eq!(Complex::new(1.0f32, 2.0f32), Complex::new(1.0f32, 2.0f32)); assert_abs_diff_ne!(Complex::new(1.0f32, 2.0f32), Complex::new(2.0f32, 1.0f32)); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_abs_diff_eq!(Complex::new(1.0f32, 2.0f32), Complex::new(2.0f32, 1.0f32)); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_abs_diff_ne!(Complex::new(1.0f32, 2.0f32), Complex::new(1.0f32, 2.0f32)); } } mod test_f64 { use super::Complex; #[test] fn test_basic() { assert_abs_diff_eq!(Complex::new(1.0f64, 2.0f64), Complex::new(1.0f64, 2.0f64)); assert_abs_diff_ne!(Complex::new(1.0f64, 2.0f64), Complex::new(2.0f64, 1.0f64)); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_abs_diff_eq!(Complex::new(1.0f64, 2.0f64), Complex::new(2.0f64, 1.0f64)); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_abs_diff_ne!(Complex::new(1.0f64, 2.0f64), Complex::new(1.0f64, 2.0f64)); } } } approx-0.5.0/tests/macro_import.rs000064400000000000000000000007620000000000000153240ustar 00000000000000extern crate approx; mod test_macro_import { use approx::{ assert_abs_diff_eq, assert_abs_diff_ne, assert_relative_eq, assert_relative_ne, assert_ulps_eq, assert_ulps_ne, }; #[test] fn test() { assert_abs_diff_eq!(1.0f32, 1.0f32); assert_abs_diff_ne!(1.0f32, 2.0f32); assert_relative_eq!(1.0f32, 1.0f32); assert_relative_ne!(1.0f32, 2.0f32); assert_ulps_eq!(1.0f32, 1.0f32); assert_ulps_ne!(1.0f32, 2.0f32); } } approx-0.5.0/tests/macros.rs000064400000000000000000000055420000000000000141160ustar 00000000000000// Copyright 2015 Brendan Zabarauskas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Macro instantiation tests #[macro_use] extern crate approx; #[test] fn test_abs_diff_eq() { let _: bool = abs_diff_eq!(1.0, 1.0); let _: bool = abs_diff_eq!(1.0, 1.0, epsilon = 1.0); } #[test] fn test_abs_diff_eq_trailing_commas() { let _: bool = abs_diff_eq!(1.0, 1.0,); let _: bool = abs_diff_eq!(1.0, 1.0, epsilon = 1.0,); } #[test] fn test_abs_diff_ne() { let _: bool = abs_diff_ne!(1.0, 1.0); let _: bool = abs_diff_ne!(1.0, 1.0, epsilon = 1.0); } #[test] fn test_abs_diff_ne_trailing_commas() { let _: bool = abs_diff_ne!(1.0, 1.0,); let _: bool = abs_diff_ne!(1.0, 1.0, epsilon = 1.0,); } #[test] fn test_relative_eq() { let _: bool = relative_eq!(1.0, 1.0); let _: bool = relative_eq!(1.0, 1.0, epsilon = 1.0); let _: bool = relative_eq!(1.0, 1.0, max_relative = 1.0); let _: bool = relative_eq!(1.0, 1.0, epsilon = 1.0, max_relative = 1.0); } #[test] fn test_relative_eq_trailing_commas() { let _: bool = relative_eq!(1.0, 1.0,); let _: bool = relative_eq!(1.0, 1.0, epsilon = 1.0, max_relative = 1.0,); } #[test] fn test_relative_ne() { let _: bool = relative_ne!(1.0, 1.0); let _: bool = relative_ne!(1.0, 1.0, epsilon = 1.0); let _: bool = relative_ne!(1.0, 1.0, max_relative = 1.0); let _: bool = relative_ne!(1.0, 1.0, epsilon = 1.0, max_relative = 1.0); } #[test] fn test_relative_ne_trailing_commas() { let _: bool = relative_ne!(1.0, 1.0,); let _: bool = relative_ne!(1.0, 1.0, epsilon = 1.0, max_relative = 1.0,); } #[test] fn test_ulps_eq() { let _: bool = ulps_eq!(1.0, 1.0); let _: bool = ulps_eq!(1.0, 1.0, epsilon = 1.0); let _: bool = ulps_eq!(1.0, 1.0, max_ulps = 1); let _: bool = ulps_eq!(1.0, 1.0, epsilon = 1.0, max_ulps = 1); } #[test] fn test_ulps_eq_trailing_commas() { let _: bool = ulps_eq!(1.0, 1.0,); let _: bool = ulps_eq!(1.0, 1.0, epsilon = 1.0, max_ulps = 1,); } #[test] fn test_ulps_ne() { let _: bool = ulps_ne!(1.0, 1.0); let _: bool = ulps_ne!(1.0, 1.0, epsilon = 1.0); let _: bool = ulps_ne!(1.0, 1.0, max_ulps = 1); let _: bool = ulps_ne!(1.0, 1.0, epsilon = 1.0, max_ulps = 1); } #[test] fn test_ulps_ne_trailing_commas() { let _: bool = ulps_ne!(1.0, 1.0,); let _: bool = ulps_ne!(1.0, 1.0, epsilon = 1.0, max_ulps = 1,); } approx-0.5.0/tests/relative_eq.rs000064400000000000000000000351210000000000000151260ustar 00000000000000// Copyright 2015 Brendan Zabarauskas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Test cases derived from: // https://github.com/Pybonacci/puntoflotante.org/blob/master/content/errors/NearlyEqualsTest.java #[macro_use] extern crate approx; mod test_f32 { use std::f32; #[test] fn test_basic() { assert_relative_eq!(1.0f32, 1.0f32); assert_relative_ne!(1.0f32, 2.0f32); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_relative_eq!(1.0f32, 2.0f32); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_relative_ne!(1.0f32, 1.0f32); } #[test] fn test_big() { assert_relative_eq!(100000000.0f32, 100000001.0f32); assert_relative_eq!(100000001.0f32, 100000000.0f32); assert_relative_ne!(10000.0f32, 10001.0f32); assert_relative_ne!(10001.0f32, 10000.0f32); } #[test] fn test_big_neg() { assert_relative_eq!(-100000000.0f32, -100000001.0f32); assert_relative_eq!(-100000001.0f32, -100000000.0f32); assert_relative_ne!(-10000.0f32, -10001.0f32); assert_relative_ne!(-10001.0f32, -10000.0f32); } #[test] fn test_mid() { assert_relative_eq!(1.0000001f32, 1.0000002f32); assert_relative_eq!(1.0000002f32, 1.0000001f32); assert_relative_ne!(1.000001f32, 1.000002f32); assert_relative_ne!(1.000002f32, 1.000001f32); } #[test] fn test_mid_neg() { assert_relative_eq!(-1.0000001f32, -1.0000002f32); assert_relative_eq!(-1.0000002f32, -1.0000001f32); assert_relative_ne!(-1.000001f32, -1.000002f32); assert_relative_ne!(-1.000002f32, -1.000001f32); } #[test] fn test_small() { assert_relative_eq!(0.000010001f32, 0.000010002f32); assert_relative_eq!(0.000010002f32, 0.000010001f32); assert_relative_ne!(0.000001002f32, 0.0000001001f32); assert_relative_ne!(0.000001001f32, 0.0000001002f32); } #[test] fn test_small_neg() { assert_relative_eq!(-0.000010001f32, -0.000010002f32); assert_relative_eq!(-0.000010002f32, -0.000010001f32); assert_relative_ne!(-0.000001002f32, -0.0000001001f32); assert_relative_ne!(-0.000001001f32, -0.0000001002f32); } #[test] fn test_zero() { assert_relative_eq!(0.0f32, 0.0f32); assert_relative_eq!(0.0f32, -0.0f32); assert_relative_eq!(-0.0f32, -0.0f32); assert_relative_ne!(0.000001f32, 0.0f32); assert_relative_ne!(0.0f32, 0.000001f32); assert_relative_ne!(-0.000001f32, 0.0f32); assert_relative_ne!(0.0f32, -0.000001f32); } #[test] fn test_epsilon() { assert_relative_eq!(0.0f32, 1e-40f32, epsilon = 1e-40f32); assert_relative_eq!(1e-40f32, 0.0f32, epsilon = 1e-40f32); assert_relative_eq!(0.0f32, -1e-40f32, epsilon = 1e-40f32); assert_relative_eq!(-1e-40f32, 0.0f32, epsilon = 1e-40f32); assert_relative_ne!(1e-40f32, 0.0f32, epsilon = 1e-41f32); assert_relative_ne!(0.0f32, 1e-40f32, epsilon = 1e-41f32); assert_relative_ne!(-1e-40f32, 0.0f32, epsilon = 1e-41f32); assert_relative_ne!(0.0f32, -1e-40f32, epsilon = 1e-41f32); } #[test] fn test_max() { assert_relative_eq!(f32::MAX, f32::MAX); assert_relative_ne!(f32::MAX, -f32::MAX); assert_relative_ne!(-f32::MAX, f32::MAX); assert_relative_ne!(f32::MAX, f32::MAX / 2.0); assert_relative_ne!(f32::MAX, -f32::MAX / 2.0); assert_relative_ne!(-f32::MAX, f32::MAX / 2.0); } #[test] fn test_infinity() { assert_relative_eq!(f32::INFINITY, f32::INFINITY); assert_relative_eq!(f32::NEG_INFINITY, f32::NEG_INFINITY); assert_relative_ne!(f32::NEG_INFINITY, f32::INFINITY); } #[test] fn test_zero_infinity() { assert_relative_ne!(0f32, f32::INFINITY); assert_relative_ne!(0f32, f32::NEG_INFINITY); } #[test] fn test_nan() { assert_relative_ne!(f32::NAN, f32::NAN); assert_relative_ne!(f32::NAN, 0.0); assert_relative_ne!(-0.0, f32::NAN); assert_relative_ne!(f32::NAN, -0.0); assert_relative_ne!(0.0, f32::NAN); assert_relative_ne!(f32::NAN, f32::INFINITY); assert_relative_ne!(f32::INFINITY, f32::NAN); assert_relative_ne!(f32::NAN, f32::NEG_INFINITY); assert_relative_ne!(f32::NEG_INFINITY, f32::NAN); assert_relative_ne!(f32::NAN, f32::MAX); assert_relative_ne!(f32::MAX, f32::NAN); assert_relative_ne!(f32::NAN, -f32::MAX); assert_relative_ne!(-f32::MAX, f32::NAN); assert_relative_ne!(f32::NAN, f32::MIN_POSITIVE); assert_relative_ne!(f32::MIN_POSITIVE, f32::NAN); assert_relative_ne!(f32::NAN, -f32::MIN_POSITIVE); assert_relative_ne!(-f32::MIN_POSITIVE, f32::NAN); } #[test] fn test_opposite_signs() { assert_relative_ne!(1.000000001f32, -1.0f32); assert_relative_ne!(-1.0f32, 1.000000001f32); assert_relative_ne!(-1.000000001f32, 1.0f32); assert_relative_ne!(1.0f32, -1.000000001f32); assert_relative_eq!(10.0 * f32::MIN_POSITIVE, 10.0 * -f32::MIN_POSITIVE); } #[test] fn test_close_to_zero() { assert_relative_eq!(f32::MIN_POSITIVE, f32::MIN_POSITIVE); assert_relative_eq!(f32::MIN_POSITIVE, -f32::MIN_POSITIVE); assert_relative_eq!(-f32::MIN_POSITIVE, f32::MIN_POSITIVE); assert_relative_eq!(f32::MIN_POSITIVE, 0.0f32); assert_relative_eq!(0.0f32, f32::MIN_POSITIVE); assert_relative_eq!(-f32::MIN_POSITIVE, 0.0f32); assert_relative_eq!(0.0f32, -f32::MIN_POSITIVE); assert_relative_ne!(0.000001f32, -f32::MIN_POSITIVE); assert_relative_ne!(0.000001f32, f32::MIN_POSITIVE); assert_relative_ne!(f32::MIN_POSITIVE, 0.000001f32); assert_relative_ne!(-f32::MIN_POSITIVE, 0.000001f32); } } #[cfg(test)] mod test_f64 { use std::f64; #[test] fn test_basic() { assert_relative_eq!(1.0f64, 1.0f64); assert_relative_ne!(1.0f64, 2.0f64); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_relative_eq!(1.0f64, 2.0f64); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_relative_ne!(1.0f64, 1.0f64); } #[test] fn test_big() { assert_relative_eq!(10000000000000000.0f64, 10000000000000001.0f64); assert_relative_eq!(10000000000000001.0f64, 10000000000000000.0f64); assert_relative_ne!(1000000000000000.0f64, 1000000000000001.0f64); assert_relative_ne!(1000000000000001.0f64, 1000000000000000.0f64); } #[test] fn test_big_neg() { assert_relative_eq!(-10000000000000000.0f64, -10000000000000001.0f64); assert_relative_eq!(-10000000000000001.0f64, -10000000000000000.0f64); assert_relative_ne!(-1000000000000000.0f64, -1000000000000001.0f64); assert_relative_ne!(-1000000000000001.0f64, -1000000000000000.0f64); } #[test] fn test_mid() { assert_relative_eq!(1.0000000000000001f64, 1.0000000000000002f64); assert_relative_eq!(1.0000000000000002f64, 1.0000000000000001f64); assert_relative_ne!(1.000000000000001f64, 1.000000000000002f64); assert_relative_ne!(1.000000000000002f64, 1.000000000000001f64); } #[test] fn test_mid_neg() { assert_relative_eq!(-1.0000000000000001f64, -1.0000000000000002f64); assert_relative_eq!(-1.0000000000000002f64, -1.0000000000000001f64); assert_relative_ne!(-1.000000000000001f64, -1.000000000000002f64); assert_relative_ne!(-1.000000000000002f64, -1.000000000000001f64); } #[test] fn test_small() { assert_relative_eq!(0.0000000100000001f64, 0.0000000100000002f64); assert_relative_eq!(0.0000000100000002f64, 0.0000000100000001f64); assert_relative_ne!(0.0000000100000001f64, 0.0000000010000002f64); assert_relative_ne!(0.0000000100000002f64, 0.0000000010000001f64); } #[test] fn test_small_neg() { assert_relative_eq!(-0.0000000100000001f64, -0.0000000100000002f64); assert_relative_eq!(-0.0000000100000002f64, -0.0000000100000001f64); assert_relative_ne!(-0.0000000100000001f64, -0.0000000010000002f64); assert_relative_ne!(-0.0000000100000002f64, -0.0000000010000001f64); } #[test] fn test_zero() { assert_relative_eq!(0.0f64, 0.0f64); assert_relative_eq!(0.0f64, -0.0f64); assert_relative_eq!(-0.0f64, -0.0f64); assert_relative_ne!(0.000000000000001f64, 0.0f64); assert_relative_ne!(0.0f64, 0.000000000000001f64); assert_relative_ne!(-0.000000000000001f64, 0.0f64); assert_relative_ne!(0.0f64, -0.000000000000001f64); } #[test] fn test_epsilon() { assert_relative_eq!(0.0f64, 1e-40f64, epsilon = 1e-40f64); assert_relative_eq!(1e-40f64, 0.0f64, epsilon = 1e-40f64); assert_relative_eq!(0.0f64, -1e-40f64, epsilon = 1e-40f64); assert_relative_eq!(-1e-40f64, 0.0f64, epsilon = 1e-40f64); assert_relative_ne!(1e-40f64, 0.0f64, epsilon = 1e-41f64); assert_relative_ne!(0.0f64, 1e-40f64, epsilon = 1e-41f64); assert_relative_ne!(-1e-40f64, 0.0f64, epsilon = 1e-41f64); assert_relative_ne!(0.0f64, -1e-40f64, epsilon = 1e-41f64); } #[test] fn test_max() { assert_relative_eq!(f64::MAX, f64::MAX); assert_relative_ne!(f64::MAX, -f64::MAX); assert_relative_ne!(-f64::MAX, f64::MAX); assert_relative_ne!(f64::MAX, f64::MAX / 2.0); assert_relative_ne!(f64::MAX, -f64::MAX / 2.0); assert_relative_ne!(-f64::MAX, f64::MAX / 2.0); } #[test] fn test_infinity() { assert_relative_eq!(f64::INFINITY, f64::INFINITY); assert_relative_eq!(f64::NEG_INFINITY, f64::NEG_INFINITY); assert_relative_ne!(f64::NEG_INFINITY, f64::INFINITY); } #[test] fn test_nan() { assert_relative_ne!(f64::NAN, f64::NAN); assert_relative_ne!(f64::NAN, 0.0); assert_relative_ne!(-0.0, f64::NAN); assert_relative_ne!(f64::NAN, -0.0); assert_relative_ne!(0.0, f64::NAN); assert_relative_ne!(f64::NAN, f64::INFINITY); assert_relative_ne!(f64::INFINITY, f64::NAN); assert_relative_ne!(f64::NAN, f64::NEG_INFINITY); assert_relative_ne!(f64::NEG_INFINITY, f64::NAN); assert_relative_ne!(f64::NAN, f64::MAX); assert_relative_ne!(f64::MAX, f64::NAN); assert_relative_ne!(f64::NAN, -f64::MAX); assert_relative_ne!(-f64::MAX, f64::NAN); assert_relative_ne!(f64::NAN, f64::MIN_POSITIVE); assert_relative_ne!(f64::MIN_POSITIVE, f64::NAN); assert_relative_ne!(f64::NAN, -f64::MIN_POSITIVE); assert_relative_ne!(-f64::MIN_POSITIVE, f64::NAN); } #[test] fn test_opposite_signs() { assert_relative_ne!(1.000000001f64, -1.0f64); assert_relative_ne!(-1.0f64, 1.000000001f64); assert_relative_ne!(-1.000000001f64, 1.0f64); assert_relative_ne!(1.0f64, -1.000000001f64); assert_relative_eq!(10.0 * f64::MIN_POSITIVE, 10.0 * -f64::MIN_POSITIVE); } #[test] fn test_close_to_zero() { assert_relative_eq!(f64::MIN_POSITIVE, f64::MIN_POSITIVE); assert_relative_eq!(f64::MIN_POSITIVE, -f64::MIN_POSITIVE); assert_relative_eq!(-f64::MIN_POSITIVE, f64::MIN_POSITIVE); assert_relative_eq!(f64::MIN_POSITIVE, 0.0f64); assert_relative_eq!(0.0f64, f64::MIN_POSITIVE); assert_relative_eq!(-f64::MIN_POSITIVE, 0.0f64); assert_relative_eq!(0.0f64, -f64::MIN_POSITIVE); assert_relative_ne!(0.000000000000001f64, -f64::MIN_POSITIVE); assert_relative_ne!(0.000000000000001f64, f64::MIN_POSITIVE); assert_relative_ne!(f64::MIN_POSITIVE, 0.000000000000001f64); assert_relative_ne!(-f64::MIN_POSITIVE, 0.000000000000001f64); } } mod test_ref { mod test_f32 { #[test] fn test_basic() { assert_relative_eq!(&1.0f32, &1.0f32); assert_relative_ne!(&1.0f32, &2.0f32); } } mod test_f64 { #[test] fn test_basic() { assert_relative_eq!(&1.0f64, &1.0f64); assert_relative_ne!(&1.0f64, &2.0f64); } } } mod test_slice { mod test_f32 { #[test] fn test_basic() { assert_relative_eq!([1.0f32, 2.0f32][..], [1.0f32, 2.0f32][..]); assert_relative_ne!([1.0f32, 2.0f32][..], [2.0f32, 1.0f32][..]); } } mod test_f64 { #[test] fn test_basic() { assert_relative_eq!([1.0f64, 2.0f64][..], [1.0f64, 2.0f64][..]); assert_relative_ne!([1.0f64, 2.0f64][..], [2.0f64, 1.0f64][..]); } } } #[cfg(feature = "num-complex")] mod test_complex { extern crate num_complex; pub use self::num_complex::Complex; mod test_f32 { use super::Complex; #[test] fn test_basic() { assert_relative_eq!(Complex::new(1.0f32, 2.0f32), Complex::new(1.0f32, 2.0f32)); assert_relative_ne!(Complex::new(1.0f32, 2.0f32), Complex::new(2.0f32, 1.0f32)); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_relative_eq!(Complex::new(1.0f32, 2.0f32), Complex::new(2.0f32, 1.0f32)); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_relative_ne!(Complex::new(1.0f32, 2.0f32), Complex::new(1.0f32, 2.0f32)); } } mod test_f64 { use super::Complex; #[test] fn test_basic() { assert_relative_eq!(Complex::new(1.0f64, 2.0f64), Complex::new(1.0f64, 2.0f64)); assert_relative_ne!(Complex::new(1.0f64, 2.0f64), Complex::new(2.0f64, 1.0f64)); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_relative_eq!(Complex::new(1.0f64, 2.0f64), Complex::new(2.0f64, 1.0f64)); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_relative_ne!(Complex::new(1.0f64, 2.0f64), Complex::new(1.0f64, 2.0f64)); } } } approx-0.5.0/tests/ulps_eq.rs000064400000000000000000000336460000000000000143100ustar 00000000000000// Copyright 2015 Brendan Zabarauskas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Test cases derived from: // https://github.com/Pybonacci/puntoflotante.org/blob/master/content/errors/NearlyEqualsTest.java #[macro_use] extern crate approx; mod test_f32 { use std::f32; #[test] fn test_basic() { assert_ulps_eq!(1.0f32, 1.0f32); assert_ulps_ne!(1.0f32, 2.0f32); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_ulps_eq!(1.0f32, 2.0f32); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_ulps_ne!(1.0f32, 1.0f32); } #[test] fn test_big() { assert_ulps_eq!(100000000.0f32, 100000001.0f32); assert_ulps_eq!(100000001.0f32, 100000000.0f32); assert_ulps_ne!(10000.0f32, 10001.0f32); assert_ulps_ne!(10001.0f32, 10000.0f32); } #[test] fn test_big_neg() { assert_ulps_eq!(-100000000.0f32, -100000001.0f32); assert_ulps_eq!(-100000001.0f32, -100000000.0f32); assert_ulps_ne!(-10000.0f32, -10001.0f32); assert_ulps_ne!(-10001.0f32, -10000.0f32); } #[test] fn test_mid() { assert_ulps_eq!(1.0000001f32, 1.0000002f32); assert_ulps_eq!(1.0000002f32, 1.0000001f32); assert_ulps_ne!(1.000001f32, 1.000002f32); assert_ulps_ne!(1.000002f32, 1.000001f32); } #[test] fn test_mid_neg() { assert_ulps_eq!(-1.0000001f32, -1.0000002f32); assert_ulps_eq!(-1.0000002f32, -1.0000001f32); assert_ulps_ne!(-1.000001f32, -1.000002f32); assert_ulps_ne!(-1.000002f32, -1.000001f32); } #[test] fn test_small() { assert_ulps_eq!(0.000010001f32, 0.000010002f32); assert_ulps_eq!(0.000010002f32, 0.000010001f32); assert_ulps_ne!(0.000001002f32, 0.0000001001f32); assert_ulps_ne!(0.000001001f32, 0.0000001002f32); } #[test] fn test_small_neg() { assert_ulps_eq!(-0.000010001f32, -0.000010002f32); assert_ulps_eq!(-0.000010002f32, -0.000010001f32); assert_ulps_ne!(-0.000001002f32, -0.0000001001f32); assert_ulps_ne!(-0.000001001f32, -0.0000001002f32); } #[test] fn test_zero() { assert_ulps_eq!(0.0f32, 0.0f32); assert_ulps_eq!(0.0f32, -0.0f32); assert_ulps_eq!(-0.0f32, -0.0f32); assert_ulps_ne!(0.000001f32, 0.0f32); assert_ulps_ne!(0.0f32, 0.000001f32); assert_ulps_ne!(-0.000001f32, 0.0f32); assert_ulps_ne!(0.0f32, -0.000001f32); } #[test] fn test_epsilon() { assert_ulps_eq!(0.0f32, 1e-40f32, epsilon = 1e-40f32); assert_ulps_eq!(1e-40f32, 0.0f32, epsilon = 1e-40f32); assert_ulps_eq!(0.0f32, -1e-40f32, epsilon = 1e-40f32); assert_ulps_eq!(-1e-40f32, 0.0f32, epsilon = 1e-40f32); assert_ulps_ne!(1e-40f32, 0.0f32, epsilon = 1e-41f32); assert_ulps_ne!(0.0f32, 1e-40f32, epsilon = 1e-41f32); assert_ulps_ne!(-1e-40f32, 0.0f32, epsilon = 1e-41f32); assert_ulps_ne!(0.0f32, -1e-40f32, epsilon = 1e-41f32); } #[test] fn test_max() { assert_ulps_eq!(f32::MAX, f32::MAX); assert_ulps_ne!(f32::MAX, -f32::MAX); assert_ulps_ne!(-f32::MAX, f32::MAX); assert_ulps_ne!(f32::MAX, f32::MAX / 2.0); assert_ulps_ne!(f32::MAX, -f32::MAX / 2.0); assert_ulps_ne!(-f32::MAX, f32::MAX / 2.0); } #[test] fn test_infinity() { assert_ulps_eq!(f32::INFINITY, f32::INFINITY); assert_ulps_eq!(f32::NEG_INFINITY, f32::NEG_INFINITY); assert_ulps_ne!(f32::NEG_INFINITY, f32::INFINITY); assert_ulps_eq!(f32::INFINITY, f32::MAX); assert_ulps_eq!(f32::NEG_INFINITY, -f32::MAX); } #[test] fn test_nan() { assert_ulps_ne!(f32::NAN, f32::NAN); assert_ulps_ne!(f32::NAN, 0.0); assert_ulps_ne!(-0.0, f32::NAN); assert_ulps_ne!(f32::NAN, -0.0); assert_ulps_ne!(0.0, f32::NAN); assert_ulps_ne!(f32::NAN, f32::INFINITY); assert_ulps_ne!(f32::INFINITY, f32::NAN); assert_ulps_ne!(f32::NAN, f32::NEG_INFINITY); assert_ulps_ne!(f32::NEG_INFINITY, f32::NAN); assert_ulps_ne!(f32::NAN, f32::MAX); assert_ulps_ne!(f32::MAX, f32::NAN); assert_ulps_ne!(f32::NAN, -f32::MAX); assert_ulps_ne!(-f32::MAX, f32::NAN); assert_ulps_ne!(f32::NAN, f32::MIN_POSITIVE); assert_ulps_ne!(f32::MIN_POSITIVE, f32::NAN); assert_ulps_ne!(f32::NAN, -f32::MIN_POSITIVE); assert_ulps_ne!(-f32::MIN_POSITIVE, f32::NAN); } #[test] fn test_opposite_signs() { assert_ulps_ne!(1.000000001f32, -1.0f32); assert_ulps_ne!(-1.0f32, 1.000000001f32); assert_ulps_ne!(-1.000000001f32, 1.0f32); assert_ulps_ne!(1.0f32, -1.000000001f32); assert_ulps_eq!(10.0 * f32::MIN_POSITIVE, 10.0 * -f32::MIN_POSITIVE); } #[test] fn test_close_to_zero() { assert_ulps_eq!(f32::MIN_POSITIVE, f32::MIN_POSITIVE); assert_ulps_eq!(f32::MIN_POSITIVE, -f32::MIN_POSITIVE); assert_ulps_eq!(-f32::MIN_POSITIVE, f32::MIN_POSITIVE); assert_ulps_eq!(f32::MIN_POSITIVE, 0.0f32); assert_ulps_eq!(0.0f32, f32::MIN_POSITIVE); assert_ulps_eq!(-f32::MIN_POSITIVE, 0.0f32); assert_ulps_eq!(0.0f32, -f32::MIN_POSITIVE); assert_ulps_ne!(0.000001f32, -f32::MIN_POSITIVE); assert_ulps_ne!(0.000001f32, f32::MIN_POSITIVE); assert_ulps_ne!(f32::MIN_POSITIVE, 0.000001f32); assert_ulps_ne!(-f32::MIN_POSITIVE, 0.000001f32); } } #[cfg(test)] mod test_f64 { use std::f64; #[test] fn test_basic() { assert_ulps_eq!(1.0f64, 1.0f64); assert_ulps_ne!(1.0f64, 2.0f64); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_ulps_eq!(1.0f64, 2.0f64); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_ulps_ne!(1.0f64, 1.0f64); } #[test] fn test_big() { assert_ulps_eq!(10000000000000000.0f64, 10000000000000001.0f64); assert_ulps_eq!(10000000000000001.0f64, 10000000000000000.0f64); assert_ulps_ne!(1000000000000000.0f64, 1000000000000001.0f64); assert_ulps_ne!(1000000000000001.0f64, 1000000000000000.0f64); } #[test] fn test_big_neg() { assert_ulps_eq!(-10000000000000000.0f64, -10000000000000001.0f64); assert_ulps_eq!(-10000000000000001.0f64, -10000000000000000.0f64); assert_ulps_ne!(-1000000000000000.0f64, -1000000000000001.0f64); assert_ulps_ne!(-1000000000000001.0f64, -1000000000000000.0f64); } #[test] fn test_mid() { assert_ulps_eq!(1.0000000000000001f64, 1.0000000000000002f64); assert_ulps_eq!(1.0000000000000002f64, 1.0000000000000001f64); assert_ulps_ne!(1.000000000000001f64, 1.0000000000000022f64); assert_ulps_ne!(1.0000000000000022f64, 1.000000000000001f64); } #[test] fn test_mid_neg() { assert_ulps_eq!(-1.0000000000000001f64, -1.0000000000000002f64); assert_ulps_eq!(-1.0000000000000002f64, -1.0000000000000001f64); assert_ulps_ne!(-1.000000000000001f64, -1.0000000000000022f64); assert_ulps_ne!(-1.0000000000000022f64, -1.000000000000001f64); } #[test] fn test_small() { assert_ulps_eq!(0.0000000100000001f64, 0.0000000100000002f64); assert_ulps_eq!(0.0000000100000002f64, 0.0000000100000001f64); assert_ulps_ne!(0.0000000100000001f64, 0.0000000010000002f64); assert_ulps_ne!(0.0000000100000002f64, 0.0000000010000001f64); } #[test] fn test_small_neg() { assert_ulps_eq!(-0.0000000100000001f64, -0.0000000100000002f64); assert_ulps_eq!(-0.0000000100000002f64, -0.0000000100000001f64); assert_ulps_ne!(-0.0000000100000001f64, -0.0000000010000002f64); assert_ulps_ne!(-0.0000000100000002f64, -0.0000000010000001f64); } #[test] fn test_zero() { assert_ulps_eq!(0.0f64, 0.0f64); assert_ulps_eq!(0.0f64, -0.0f64); assert_ulps_eq!(-0.0f64, -0.0f64); assert_ulps_ne!(0.000000000000001f64, 0.0f64); assert_ulps_ne!(0.0f64, 0.000000000000001f64); assert_ulps_ne!(-0.000000000000001f64, 0.0f64); assert_ulps_ne!(0.0f64, -0.000000000000001f64); } #[test] fn test_epsilon() { assert_ulps_eq!(0.0f64, 1e-40f64, epsilon = 1e-40f64); assert_ulps_eq!(1e-40f64, 0.0f64, epsilon = 1e-40f64); assert_ulps_eq!(0.0f64, -1e-40f64, epsilon = 1e-40f64); assert_ulps_eq!(-1e-40f64, 0.0f64, epsilon = 1e-40f64); assert_ulps_ne!(1e-40f64, 0.0f64, epsilon = 1e-41f64); assert_ulps_ne!(0.0f64, 1e-40f64, epsilon = 1e-41f64); assert_ulps_ne!(-1e-40f64, 0.0f64, epsilon = 1e-41f64); assert_ulps_ne!(0.0f64, -1e-40f64, epsilon = 1e-41f64); } #[test] fn test_max() { assert_ulps_eq!(f64::MAX, f64::MAX); assert_ulps_ne!(f64::MAX, -f64::MAX); assert_ulps_ne!(-f64::MAX, f64::MAX); assert_ulps_ne!(f64::MAX, f64::MAX / 2.0); assert_ulps_ne!(f64::MAX, -f64::MAX / 2.0); assert_ulps_ne!(-f64::MAX, f64::MAX / 2.0); } #[test] fn test_infinity() { assert_ulps_eq!(f64::INFINITY, f64::INFINITY); assert_ulps_eq!(f64::NEG_INFINITY, f64::NEG_INFINITY); assert_ulps_ne!(f64::NEG_INFINITY, f64::INFINITY); assert_ulps_eq!(f64::INFINITY, f64::MAX); assert_ulps_eq!(f64::NEG_INFINITY, -f64::MAX); } #[test] fn test_nan() { assert_ulps_ne!(f64::NAN, f64::NAN); assert_ulps_ne!(f64::NAN, 0.0); assert_ulps_ne!(-0.0, f64::NAN); assert_ulps_ne!(f64::NAN, -0.0); assert_ulps_ne!(0.0, f64::NAN); assert_ulps_ne!(f64::NAN, f64::INFINITY); assert_ulps_ne!(f64::INFINITY, f64::NAN); assert_ulps_ne!(f64::NAN, f64::NEG_INFINITY); assert_ulps_ne!(f64::NEG_INFINITY, f64::NAN); assert_ulps_ne!(f64::NAN, f64::MAX); assert_ulps_ne!(f64::MAX, f64::NAN); assert_ulps_ne!(f64::NAN, -f64::MAX); assert_ulps_ne!(-f64::MAX, f64::NAN); assert_ulps_ne!(f64::NAN, f64::MIN_POSITIVE); assert_ulps_ne!(f64::MIN_POSITIVE, f64::NAN); assert_ulps_ne!(f64::NAN, -f64::MIN_POSITIVE); assert_ulps_ne!(-f64::MIN_POSITIVE, f64::NAN); } #[test] fn test_opposite_signs() { assert_ulps_ne!(1.000000001f64, -1.0f64); assert_ulps_ne!(-1.0f64, 1.000000001f64); assert_ulps_ne!(-1.000000001f64, 1.0f64); assert_ulps_ne!(1.0f64, -1.000000001f64); assert_ulps_eq!(10.0 * f64::MIN_POSITIVE, 10.0 * -f64::MIN_POSITIVE); } #[test] fn test_close_to_zero() { assert_ulps_eq!(f64::MIN_POSITIVE, f64::MIN_POSITIVE); assert_ulps_eq!(f64::MIN_POSITIVE, -f64::MIN_POSITIVE); assert_ulps_eq!(-f64::MIN_POSITIVE, f64::MIN_POSITIVE); assert_ulps_eq!(f64::MIN_POSITIVE, 0.0f64); assert_ulps_eq!(0.0f64, f64::MIN_POSITIVE); assert_ulps_eq!(-f64::MIN_POSITIVE, 0.0f64); assert_ulps_eq!(0.0f64, -f64::MIN_POSITIVE); assert_ulps_ne!(0.000000000000001f64, -f64::MIN_POSITIVE); assert_ulps_ne!(0.000000000000001f64, f64::MIN_POSITIVE); assert_ulps_ne!(f64::MIN_POSITIVE, 0.000000000000001f64); assert_ulps_ne!(-f64::MIN_POSITIVE, 0.000000000000001f64); } } mod test_ref { mod test_f32 { #[test] fn test_basic() { assert_ulps_eq!(&1.0f32, &1.0f32); assert_ulps_ne!(&1.0f32, &2.0f32); } } mod test_f64 { #[test] fn test_basic() { assert_ulps_eq!(&1.0f64, &1.0f64); assert_ulps_ne!(&1.0f64, &2.0f64); } } } mod test_slice { mod test_f32 { #[test] fn test_basic() { assert_ulps_eq!([1.0f32, 2.0f32][..], [1.0f32, 2.0f32][..]); assert_ulps_ne!([1.0f32, 2.0f32][..], [2.0f32, 1.0f32][..]); } } mod test_f64 { #[test] fn test_basic() { assert_ulps_eq!([1.0f64, 2.0f64][..], [1.0f64, 2.0f64][..]); assert_ulps_ne!([1.0f64, 2.0f64][..], [2.0f64, 1.0f64][..]); } } } #[cfg(feature = "num-complex")] mod test_complex { extern crate num_complex; pub use self::num_complex::Complex; mod test_f32 { use super::Complex; #[test] fn test_basic() { assert_ulps_eq!(Complex::new(1.0f32, 2.0f32), Complex::new(1.0f32, 2.0f32)); assert_ulps_ne!(Complex::new(1.0f32, 2.0f32), Complex::new(2.0f32, 1.0f32)); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_ulps_eq!(Complex::new(1.0f32, 2.0f32), Complex::new(2.0f32, 1.0f32)); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_ulps_ne!(Complex::new(1.0f32, 2.0f32), Complex::new(1.0f32, 2.0f32)); } } mod test_f64 { use super::Complex; #[test] fn test_basic() { assert_ulps_eq!(Complex::new(1.0f64, 2.0f64), Complex::new(1.0f64, 2.0f64)); assert_ulps_ne!(Complex::new(1.0f64, 2.0f64), Complex::new(2.0f64, 1.0f64)); } #[test] #[should_panic] fn test_basic_panic_eq() { assert_ulps_eq!(Complex::new(1.0f64, 2.0f64), Complex::new(2.0f64, 1.0f64)); } #[test] #[should_panic] fn test_basic_panic_ne() { assert_ulps_ne!(Complex::new(1.0f64, 2.0f64), Complex::new(1.0f64, 2.0f64)); } } }