num-cmp-0.1.0/.gitignore010064400007650000024000000000221305527722600132760ustar0000000000000000target Cargo.lock num-cmp-0.1.0/Cargo.toml010064400007650000024000000020761305600701000132300ustar0000000000000000# 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "num-cmp" version = "0.1.0" authors = ["Kang Seonghoon "] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Comparison between differently typed numbers" homepage = "https://github.com/lifthrasiir/num-cmp" documentation = "https://docs.rs/num-cmp/" readme = "README.md" keywords = [ "numeric", "comparison", "compare", ] categories = ["algorithms"] license = "MIT/Apache-2.0" repository = "https://github.com/lifthrasiir/num-cmp" [lib] name = "num_cmp" path = "src/lib.rs" [features] i128 = [] num-cmp-0.1.0/Cargo.toml.orig010064400007650000024000000007121305600701000141620ustar0000000000000000[package] name = "num-cmp" version = "0.1.0" authors = ["Kang Seonghoon "] description = "Comparison between differently typed numbers" homepage = "https://github.com/lifthrasiir/num-cmp" documentation = "https://docs.rs/num-cmp/" repository = "https://github.com/lifthrasiir/num-cmp" keywords = ["numeric", "comparison", "compare"] categories = ["algorithms"] readme = "README.md" license = "MIT/Apache-2.0" [features] "i128" = [] num-cmp-0.1.0/LICENSE.txt010064400007650000024000000500401305600737600131340ustar0000000000000000The author of num-cmp, Kang Seonghoon, disclaims copyright to this source code. In place of a legal notice, here is a blessing: May you do good and not evil. May you find forgiveness for yourself and forgive others. May you share freely, never taking more than you give. Alternatively, for jurisdictions where authors cannot disclaim their copyright, this source code is distributed under the terms of CC0 1.0 Universal license [1] as published by Creative Commons. In order to allow the maximal compatibility with Rust projects, this source code is also dual-licensed under the MIT License [2] and Apache 2.0 License [3]. Copyright (c) 2017, Kang Seonghoon. This dual-licensing is disjunctive with the public domain dedication and shall not affect the casual use of this source code. This also means that not every contribution to the MIT/Apache-licensed version of this source code cannot be ported back to the public domain version. The author encourages (but not requires, naturally) that any contribution to this source code is also pushed back separately to the public domain repository. This legal notice and blessing is shamelessly adopted from the SQLite library. Nota Bene: [2] and [3] are same as the Rust Project's own license. [1]: , which is reproduced below: ~~~~ Creative Commons Legal Code CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. ~~~~ [2]: , which is reproduced below: ~~~~ The MIT License (MIT) Copyright (c) 2017, Kang Seonghoon. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ~~~~ [3]: , which is reproduced below: ~~~~ 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. ~~~~ num-cmp-0.1.0/README.md010064400007650000024000000015001305600741200125540ustar0000000000000000# num-cmp The **[`NumCmp`](./trait.NumCmp.html)** trait for comparison between differently typed numbers. ```rust use std::f32; use std::cmp::Ordering; use num_cmp::NumCmp; assert!(NumCmp::num_eq(3u64, 3.0f32)); assert!(NumCmp::num_lt(-4.7f64, -4i8)); assert!(!NumCmp::num_ge(-3i8, 1u16)); // 40_000_000 can be exactly represented in f32, 40_000_001 cannot assert_eq!(NumCmp::num_cmp(40_000_000.0f32, 40_000_000u32), Some(Ordering::Equal)); assert_ne!(NumCmp::num_cmp(40_000_001.0f32, 40_000_001u32), Some(Ordering::Equal)); assert_eq!(NumCmp::num_cmp(f32::NAN, 40_000_002u32), None); ``` The `i128` Cargo feature can be enabled in nightly to get support for `i128` and `u128` types as well, which is being implemented in [Rust issue #35118][issue-35118]. [issue-35118]: https://github.com/rust-lang/rust/issues/35118 num-cmp-0.1.0/src/lib.rs010064400007650000024000000554731305600713200132210ustar0000000000000000//! The **[`NumCmp`](./trait.NumCmp.html)** trait for comparison between differently typed numbers. //! //! ```rust //! use std::f32; //! use std::cmp::Ordering; //! use num_cmp::NumCmp; //! //! assert!(NumCmp::num_eq(3u64, 3.0f32)); //! assert!(NumCmp::num_lt(-4.7f64, -4i8)); //! assert!(!NumCmp::num_ge(-3i8, 1u16)); //! //! // 40_000_000 can be exactly represented in f32, 40_000_001 cannot //! assert_eq!(NumCmp::num_cmp(40_000_000.0f32, 40_000_000u32), Some(Ordering::Equal)); //! assert_ne!(NumCmp::num_cmp(40_000_001.0f32, 40_000_001u32), Some(Ordering::Equal)); //! assert_eq!(NumCmp::num_cmp(f32::NAN, 40_000_002u32), None); //! ``` //! //! The `i128` Cargo feature can be enabled in nightly //! to get support for `i128` and `u128` types as well, //! which is being implemented in [Rust issue #35118][issue-35118]. //! //! [issue-35118]: https://github.com/rust-lang/rust/issues/35118 #![cfg_attr(feature = "i128", feature(i128_type))] #![deny(missing_docs)] use std::cmp::Ordering; /// A trait for comparison between differently typed numbers. /// /// This trait is implemented for every pair of integer and floating-point types available, /// including `isize`, `usize` and also (when the `i128` feature is enabled) `i128` and `u128`. pub trait NumCmp: Copy { // only used for testing #[cfg(test)] fn num_cmp_strategy(self, other: Other) -> &'static str; /// Same to `self.partial_cmp(&other)` /// but can be used for different numeric types for `self` and `other`. fn num_cmp(self, other: Other) -> Option; /// Same to `self == other` but can be used for different numeric types for `self` and `other`. fn num_eq(self, other: Other) -> bool; /// Same to `self != other` but can be used for different numeric types for `self` and `other`. fn num_ne(self, other: Other) -> bool; /// Same to `self < other` but can be used for different numeric types for `self` and `other`. fn num_lt(self, other: Other) -> bool; /// Same to `self > other` but can be used for different numeric types for `self` and `other`. fn num_gt(self, other: Other) -> bool; /// Same to `self <= other` but can be used for different numeric types for `self` and `other`. fn num_le(self, other: Other) -> bool; /// Same to `self >= other` but can be used for different numeric types for `self` and `other`. fn num_ge(self, other: Other) -> bool; } // strategy 1: for the same type T, delegate to normal operators. macro_rules! impl_for_equal_types { ($($ty:ty;)*) => ($( impl NumCmp<$ty> for $ty { #[cfg(test)] fn num_cmp_strategy(self, _other: $ty) -> &'static str { "strategy 1" } #[inline] fn num_cmp(self, other: $ty) -> Option { self.partial_cmp(&other) } #[inline] fn num_eq(self, other: $ty) -> bool { self == other } #[inline] fn num_ne(self, other: $ty) -> bool { self != other } #[inline] fn num_lt(self, other: $ty) -> bool { self < other } #[inline] fn num_gt(self, other: $ty) -> bool { self > other } #[inline] fn num_le(self, other: $ty) -> bool { self <= other } #[inline] fn num_ge(self, other: $ty) -> bool { self >= other } } )*); } // strategy 2: for two types where one of them is isize or usize, // delegate to implementations for the equivalently sized types. macro_rules! impl_for_size_types { ($($size:ty => $nonsize:ty, $other:ty;)*) => ($( impl NumCmp<$other> for $size { #[cfg(test)] fn num_cmp_strategy(self, _other: $other) -> &'static str { "strategy 2, size vs other" } #[inline] fn num_cmp(self, other: $other) -> Option { (self as $nonsize).num_cmp(other) } #[inline] fn num_eq(self, other: $other) -> bool { (self as $nonsize).num_eq(other) } #[inline] fn num_ne(self, other: $other) -> bool { (self as $nonsize).num_ne(other) } #[inline] fn num_lt(self, other: $other) -> bool { (self as $nonsize).num_lt(other) } #[inline] fn num_gt(self, other: $other) -> bool { (self as $nonsize).num_gt(other) } #[inline] fn num_le(self, other: $other) -> bool { (self as $nonsize).num_le(other) } #[inline] fn num_ge(self, other: $other) -> bool { (self as $nonsize).num_ge(other) } } impl NumCmp<$size> for $other { #[cfg(test)] fn num_cmp_strategy(self, _other: $size) -> &'static str { "strategy 2, nonsize vs size" } #[inline] fn num_cmp(self, other: $size) -> Option { self.num_cmp(other as $nonsize) } #[inline] fn num_eq(self, other: $size) -> bool { self.num_eq(other as $nonsize) } #[inline] fn num_ne(self, other: $size) -> bool { self.num_ne(other as $nonsize) } #[inline] fn num_lt(self, other: $size) -> bool { self.num_lt(other as $nonsize) } #[inline] fn num_gt(self, other: $size) -> bool { self.num_gt(other as $nonsize) } #[inline] fn num_le(self, other: $size) -> bool { self.num_le(other as $nonsize) } #[inline] fn num_ge(self, other: $size) -> bool { self.num_ge(other as $nonsize) } } )*); } // strategy 3: for two types T and U, // (without loss of generality) when T can be always exactly casted to U, // compare them by casting T to U. macro_rules! impl_for_nonequal_types_with_casting { ($($big:ty, $small:ty;)*) => ($( impl NumCmp<$small> for $big { #[cfg(test)] fn num_cmp_strategy(self, _other: $small) -> &'static str { "strategy 3, big vs small" } #[inline] fn num_cmp(self, other: $small) -> Option { self.partial_cmp(&(other as $big)) } #[inline] fn num_eq(self, other: $small) -> bool { self == other as $big } #[inline] fn num_ne(self, other: $small) -> bool { self != other as $big } #[inline] fn num_lt(self, other: $small) -> bool { self < other as $big } #[inline] fn num_gt(self, other: $small) -> bool { self > other as $big } #[inline] fn num_le(self, other: $small) -> bool { self <= other as $big } #[inline] fn num_ge(self, other: $small) -> bool { self >= other as $big } } impl NumCmp<$big> for $small { #[cfg(test)] fn num_cmp_strategy(self, _other: $big) -> &'static str { "strategy 3, small vs big" } #[inline] fn num_cmp(self, other: $big) -> Option { (self as $big).partial_cmp(&other) } #[inline] fn num_eq(self, other: $big) -> bool { self as $big == other } #[inline] fn num_ne(self, other: $big) -> bool { self as $big != other } #[inline] fn num_lt(self, other: $big) -> bool { (self as $big) < other } #[inline] fn num_gt(self, other: $big) -> bool { self as $big > other } #[inline] fn num_le(self, other: $big) -> bool { self as $big <= other } #[inline] fn num_ge(self, other: $big) -> bool { self as $big >= other } } )*); } // strategy 4: for unsigned type T and signed type U, // if bit size of T is no less than that of U, // check if both operands are positive before doing the normal comparison in unsigned type. macro_rules! impl_for_nonequal_types_with_different_signedness { ($($unsigned:ty, $signed:ty;)*) => ($( impl NumCmp<$signed> for $unsigned { #[cfg(test)] fn num_cmp_strategy(self, _other: $signed) -> &'static str { "strategy 4, unsigned vs signed" } #[inline] fn num_cmp(self, other: $signed) -> Option { if other < 0 { Some(Ordering::Greater) } else { self.partial_cmp(&(other as $unsigned)) } } #[inline] fn num_eq(self, other: $signed) -> bool { other >= 0 && self == other as $unsigned } #[inline] fn num_ne(self, other: $signed) -> bool { other < 0 || self != other as $unsigned } #[inline] fn num_lt(self, other: $signed) -> bool { other > 0 && self < other as $unsigned } #[inline] fn num_gt(self, other: $signed) -> bool { other < 0 || self > other as $unsigned } #[inline] fn num_le(self, other: $signed) -> bool { other >= 0 && self <= other as $unsigned } #[inline] fn num_ge(self, other: $signed) -> bool { other <= 0 || self >= other as $unsigned } } impl NumCmp<$unsigned> for $signed { #[cfg(test)] fn num_cmp_strategy(self, _other: $unsigned) -> &'static str { "strategy 4, signed vs unsigned" } #[inline] fn num_cmp(self, other: $unsigned) -> Option { if self < 0 { Some(Ordering::Less) } else { (self as $unsigned).partial_cmp(&other) } } #[inline] fn num_eq(self, other: $unsigned) -> bool { self >= 0 && self as $unsigned == other } #[inline] fn num_ne(self, other: $unsigned) -> bool { self < 0 || self as $unsigned != other } #[inline] fn num_lt(self, other: $unsigned) -> bool { self < 0 || (self as $unsigned) < other } #[inline] fn num_gt(self, other: $unsigned) -> bool { self > 0 && self as $unsigned > other } #[inline] fn num_le(self, other: $unsigned) -> bool { self <= 0 || self as $unsigned <= other } #[inline] fn num_ge(self, other: $unsigned) -> bool { self >= 0 && self as $unsigned >= other } } )*); } // strategy 5: for two types T and U, // when T can be casted to U but it may result in precision loss, // first bound the operand in type U to the domain of type T; // when testing equality the bound failure means the inequality, // otherwise we convert to the appropriate value in type T so that the comparison can continue. // // since all integral conversion does not lose precision (but can be out of range), // T is guaranteed to be integral while U is guaranteed to be floating-point. // it is possible that bounds themselves can be overflown (especially when T=u128, U=f32). // // for general comparison we have the following useful observation: // // where `a cmp b` is a general partial ordering operator (like `PartialOrd::partial_cmp`) // and `trunc(x)` is `x` rounded towards zero (i.e. trunc(3.5) = 3, trunc(-3.5) = -3), // if `a` is an integer, `a cmp b` equals to `(a, trunc(b)) cmp (trunc(b), b)`. // (we assume that the tuple is ordered lexicographically.) // // the proof involves an equality `floor(x) <= trunc(x) <= ceil(x)`, // and inequalities `n < x <=> n < ceil(x)` and `x < n <=> floor(x) < n` for integer `n`. // when `a < trunc(b)` it follows that `a < trunc(b) <= ceil(b)`, which implies `a < b`; // when `a > trunc(b)` it follows that `a > trunc(b) >= floor(b)`, which implies `a > b`; // when `a = trunc(b)` any inequality `trunc(b) op b` follows that `a = trunc(b) op b`, // which clearly implies `a op b` as intended. Q.E.D. // // since `a` and `trunc(b)` can be made into the same type after bounds checking, // this gives very uniform and simple way to compare numbers. // we of course rely on the fact that the range of `trunc(a)` is smaller than the domain of `a`. // requires that the float operand is not NaN and in the ($lb, $ub) range macro_rules! trunc_cmp { (int $lhs:expr, $method:ident, float $rhs:expr; ($lb:expr) <= ($intty:ty) < ($ub:expr)) => ({ let rhsint = $rhs.trunc(); debug_assert!($lb <= rhsint && rhsint < $ub); ($lhs, rhsint).$method(&(rhsint as $intty, $rhs)) }); (float $lhs:expr, $method:ident, int $rhs:expr; ($lb:expr) <= ($intty:ty) < ($ub:expr)) => ({ let lhsint = $lhs.trunc(); debug_assert!($lb <= lhsint && lhsint < $ub); (lhsint as $intty, $lhs).$method(&($rhs, lhsint)) }); } macro_rules! impl_for_int_and_float_types_with_bounds_check { ($($float:ty, $int:ty, ($lb:expr) <= _ < ($ub:expr);)*) => ($( impl NumCmp<$int> for $float { #[cfg(test)] fn num_cmp_strategy(self, _other: $int) -> &'static str { "strategy 5, float vs int" } #[inline] fn num_cmp(self, other: $int) -> Option { if self < $lb { Some(Ordering::Less) } else if $ub <= self { Some(Ordering::Greater) } else if self == self { trunc_cmp!(float self, partial_cmp, int other; ($lb) <= ($int) < ($ub)) } else { None } } #[inline] fn num_eq(self, other: $int) -> bool { $lb <= self && self < $ub && trunc_cmp!(float self, eq, int other; ($lb) <= ($int) < ($ub)) } #[inline] fn num_ne(self, other: $int) -> bool { // we cannot blindly apply De Morgan's law; we need to catch NaN early on !($lb <= self && self < $ub) || trunc_cmp!(float self, ne, int other; ($lb) <= ($int) < ($ub)) } #[inline] fn num_lt(self, other: $int) -> bool { self < $ub && (self < $lb || trunc_cmp!(float self, lt, int other; ($lb) <= ($int) < ($ub))) } #[inline] fn num_gt(self, other: $int) -> bool { $lb <= self && ($ub <= self || trunc_cmp!(float self, gt, int other; ($lb) <= ($int) < ($ub))) } #[inline] fn num_le(self, other: $int) -> bool { self < $ub && (self < $lb || trunc_cmp!(float self, le, int other; ($lb) <= ($int) < ($ub))) } #[inline] fn num_ge(self, other: $int) -> bool { $lb <= self && ($ub <= self || trunc_cmp!(float self, ge, int other; ($lb) <= ($int) < ($ub))) } } impl NumCmp<$float> for $int { #[cfg(test)] fn num_cmp_strategy(self, _other: $float) -> &'static str { "strategy 5, int vs float" } #[inline] fn num_cmp(self, other: $float) -> Option { if other < $lb { Some(Ordering::Greater) } else if $ub <= other { Some(Ordering::Less) } else if other == other { trunc_cmp!(int self, partial_cmp, float other; ($lb) <= ($int) < ($ub)) } else { None } } #[inline] fn num_eq(self, other: $float) -> bool { $lb <= other && other < $ub && trunc_cmp!(int self, eq, float other; ($lb) <= ($int) < ($ub)) } #[inline] fn num_ne(self, other: $float) -> bool { // we cannot blindly apply De Morgan's law; we need to catch NaN early on !($lb <= other && other < $ub) || trunc_cmp!(int self, ne, float other; ($lb) <= ($int) < ($ub)) } #[inline] fn num_lt(self, other: $float) -> bool { $lb <= other && ($ub <= other || trunc_cmp!(int self, lt, float other; ($lb) <= ($int) < ($ub))) } #[inline] fn num_gt(self, other: $float) -> bool { other < $ub && (other < $lb || trunc_cmp!(int self, gt, float other; ($lb) <= ($int) < ($ub))) } #[inline] fn num_le(self, other: $float) -> bool { $lb <= other && ($ub <= other || trunc_cmp!(int self, le, float other; ($lb) <= ($int) < ($ub))) } #[inline] fn num_ge(self, other: $float) -> bool { other < $ub && (other < $lb || trunc_cmp!(int self, ge, float other; ($lb) <= ($int) < ($ub))) } } )*); } // actual implementations. // there should be 12 * 12 = 144 implementations for the NumCmp trait // (or 14 * 14 = 196 implementations if the `i128` feature is enabled). impl_for_equal_types! { u8; u16; u32; u64; usize; i8; i16; i32; i64; isize; f32; f64; } #[cfg(feature = "i128")] impl_for_equal_types! { u128; i128; } #[cfg(target_pointer_width = "32")] impl_for_size_types! { usize => u32, u8; isize => i32, u8; usize => u32, u16; isize => i32, u16; usize => u32, u32; isize => i32, u32; usize => u32, u64; isize => i32, u64; usize => u32, i8; isize => i32, i8; usize => u32, i16; isize => i32, i16; usize => u32, i32; isize => i32, i32; usize => u32, i64; isize => i32, i64; usize => u32, f32; isize => i32, f32; usize => u32, f64; isize => i32, f64; } #[cfg(target_pointer_width = "64")] impl_for_size_types! { usize => u64, u8; isize => i64, u8; usize => u64, u16; isize => i64, u16; usize => u64, u32; isize => i64, u32; usize => u64, u64; isize => i64, u64; usize => u64, i8; isize => i64, i8; usize => u64, i16; isize => i64, i16; usize => u64, i32; isize => i64, i32; usize => u64, i64; isize => i64, i64; usize => u64, f32; isize => i64, f32; usize => u64, f64; isize => i64, f64; } #[cfg(all(target_pointer_width = "32", feature = "i128"))] impl_for_size_types! { usize => u32, u128; isize => i32, u128; usize => u32, i128; isize => i32, i128; } #[cfg(all(target_pointer_width = "64", feature = "i128"))] impl_for_size_types! { usize => u64, u128; isize => i64, u128; usize => u64, i128; isize => i64, i128; } impl_for_nonequal_types_with_casting! { // uN, uM for N > M u64, u8; u32, u8; u16, u8; u64, u16; u32, u16; u64, u32; // iN, iM for N > M i64, i8; i32, i8; i16, i8; i64, i16; i32, i16; i64, i32; // iN, uM for N > M i64, u8; i32, u8; i16, u8; i64, u16; i32, u16; i64, u32; // fN, fM for N > M f64, f32; // f32, uM for 24 >= M, since f32 can exactly represent all integers (-2^24,2^24) // f64, uM for 53 >= M, since f64 can exactly represent all integers (-2^53,2^53) f32, u8; f32, u16; f64, u8; f64, u16; f64, u32; // f32, iM for 24 >= M // f64, iM for 53 >= M // since iM's range [-2^(M-1),2^(M-1)) includes -2^(M-1), bounds do not change f32, i8; f32, i16; f64, i8; f64, i16; f64, i32; } #[cfg(feature = "i128")] impl_for_nonequal_types_with_casting! { u128, u8; u128, u16; u128, u32; u128, u64; i128, u8; i128, u16; i128, u32; i128, u64; i128, i8; i128, i16; i128, i32; i128, i64; } impl_for_nonequal_types_with_different_signedness! { u64, i8; u32, i8; u16, i8; u8, i8; u64, i16; u32, i16; u16, i16; u64, i32; u32, i32; u64, i64; usize, isize; } #[cfg(feature = "i128")] impl_for_nonequal_types_with_different_signedness! { u128, i8; u128, i16; u128, i32; u128, i64; u128, i128; } const U32_BOUND_IN_F32: f32 = 4294967296.0; const I32_BOUND_IN_F32: f32 = 2147483648.0; const U64_BOUND_IN_F32: f32 = 18446744073709551616.0; const U64_BOUND_IN_F64: f64 = 18446744073709551616.0; const I64_BOUND_IN_F32: f32 = 9223372036854775808.0; const I64_BOUND_IN_F64: f64 = 9223372036854775808.0; impl_for_int_and_float_types_with_bounds_check! { // f32, uM for 24 < M // f64, uM for 53 < M f32, u32, (0.0) <= _ < (U32_BOUND_IN_F32); f32, u64, (0.0) <= _ < (U64_BOUND_IN_F32); f64, u64, (0.0) <= _ < (U64_BOUND_IN_F64); // f32, iM for 24 < M // f64, iM for 53 < M f32, i32, (-I32_BOUND_IN_F32) <= _ < (I32_BOUND_IN_F32); f32, i64, (-I64_BOUND_IN_F32) <= _ < (I64_BOUND_IN_F32); f64, i64, (-I64_BOUND_IN_F64) <= _ < (I64_BOUND_IN_F64); } #[cfg(feature = "i128")] const U128_BOUND_IN_F32: f32 = std::f32::INFINITY; #[cfg(feature = "i128")] const U128_BOUND_IN_F64: f64 = 340282366920938463463374607431768211456.0; #[cfg(feature = "i128")] const I128_BOUND_IN_F32: f32 = 170141183460469231731687303715884105728.0; #[cfg(feature = "i128")] const I128_BOUND_IN_F64: f64 = 170141183460469231731687303715884105728.0; #[cfg(feature = "i128")] impl_for_int_and_float_types_with_bounds_check! { f32, u128, (0.0) <= _ < (U128_BOUND_IN_F32); f64, u128, (0.0) <= _ < (U128_BOUND_IN_F64); f32, i128, (-I128_BOUND_IN_F32) <= _ < (I128_BOUND_IN_F32); f64, i128, (-I128_BOUND_IN_F64) <= _ < (I128_BOUND_IN_F64); } #[cfg(test)] mod tests; num-cmp-0.1.0/src/tests.rs010064400007650000024000000421251305600444700136110ustar0000000000000000use super::*; use std::fmt; use std::f32; use std::f64; use std::cmp::Ordering; use std::cmp::Ordering::*; #[derive(Copy, Clone, Debug)] #[allow(non_camel_case_types)] enum N { u8(u8), u16(u16), u32(u32), u64(u64), usize(usize), i8(i8), i16(i16), i32(i32), i64(i64), isize(isize), f32(f32), f64(f64), #[cfg(feature = "i128")] u128(u128), #[cfg(feature = "i128")] i128(i128), } fn assert_cmp>>(lhs: N, rhs: N, expected: T) { #[derive(PartialEq)] struct Result { ord: Option, eq: bool, ne: bool, lt: bool, gt: bool, le: bool, ge: bool, } impl fmt::Debug for Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let Some(ord) = self.ord { write!(f, "", neg(self.eq), neg(self.ne), neg(self.lt), neg(self.gt), neg(self.le), neg(self.gt)) } } let expected: Option = expected.into(); let expected = match expected { Some(Less) => { Result { ord: expected, eq: false, ne: true, lt: true, gt: false, le: true, ge: false } }, Some(Equal) => { Result { ord: expected, eq: true, ne: false, lt: false, gt: false, le: true, ge: true } }, Some(Greater) => { Result { ord: expected, eq: false, ne: true, lt: false, gt: true, le: false, ge: true } }, None => { Result { ord: expected, eq: false, ne: true, lt: false, gt: false, le: false, ge: false } }, }; macro_rules! repeat_arms { ($e:expr; $v:ident => $arm:expr) => { match $e { N::u8($v) => $arm, N::u16($v) => $arm, N::u32($v) => $arm, N::u64($v) => $arm, N::usize($v) => $arm, N::i8($v) => $arm, N::i16($v) => $arm, N::i32($v) => $arm, N::i64($v) => $arm, N::isize($v) => $arm, N::f32($v) => $arm, N::f64($v) => $arm, #[cfg(feature = "i128")] N::u128($v) => $arm, #[cfg(feature = "i128")] N::i128($v) => $arm, } }; } let (strategy, actual) = repeat_arms! { lhs; x => { repeat_arms! { rhs; y => { (x.num_cmp_strategy(y), Result { ord: x.num_cmp(y), eq: x.num_eq(y), ne: x.num_ne(y), lt: x.num_lt(y), gt: x.num_gt(y), le: x.num_le(y), ge: x.num_ge(y) }) } } } }; if expected != actual { panic!("failed to compare {:?} against {:?} ({}); expected {:?}, actual {:?}", lhs, rhs, strategy, expected, actual); } } macro_rules! n { ($e:expr; $($t:ident),*) => (&[$(N::$t($e as $t)),*]); } #[cfg(feature = "i128")] macro_rules! n128 { (($e:expr; $($t:ident),*)) => (&[$(N::$t($e as $t)),*]); (($e:expr; $($t:ident),*); ($($_ignore:tt)*)) => (&[$(N::$t($e as $t)),*]); } #[cfg(not(feature = "i128"))] macro_rules! n128 { (($($_ignore:tt)*)) => (&[]); (($($_ignore:tt)*); ($e:expr; $($t:ident),*)) => (&[$(N::$t($e as $t)),*]); } const F32_SUBNORMAL_MIN: f32 = 1.401298464324817e-45; const F64_SUBNORMAL_MIN: f64 = 4.9406564584124654e-324; // they are used to represent 2^32 when only decimal fp literals can be used #[cfg(not(feature = "i128"))] const F32_32: f32 = 0x1_0000_0000u64 as f32; const F64_32: f64 = 0x1_0000_0000u64 as f64; #[test] fn test_subnormal_min_is_correct() { assert_ne!(F32_SUBNORMAL_MIN, 0.0f32); assert_ne!(F64_SUBNORMAL_MIN, 0.0f64); assert_eq!(F32_SUBNORMAL_MIN / 2.0, 0.0f32); assert_eq!(F64_SUBNORMAL_MIN / 2.0, 0.0f64); } const NUMBERS: &'static [&'static [N]] = &[ // f64 min boundary and infinity n!(f32::NEG_INFINITY; f32), n!(f64::MIN; f64), // f32 min boundary n!((-F64_32 * F64_32 - 0x1000 as f64) * F64_32 * F64_32; f64), // -2^128 - 2^76 n!(-F64_32 * F64_32 * F64_32 * F64_32; f64), // -2^128 n!((-F64_32 * F64_32 + 0x800 as f64) * F64_32 * F64_32; f64), // -2^128 + 2^75 n!(f32::MIN; f32), // -2^128 + 2^104 // i128 max boundary n128!((-(0x8000_0100_0000_0000_0000_0000_0000_0000u128 as f32); f32)), n128!((-(0x8000_0000_0000_0800_0000_0000_0000_0000u128 as f64); f64)), n128!((-0x8000_0000_0000_0000_0000_0000_0000_0000i128; i128, f32)), n128!((-0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_ffffi128; i128)), n128!((-0x7fff_ffff_ffff_fc00_0000_0000_0000_0000i128; i128, f64)), n128!((-0x7fff_ff80_0000_0000_0000_0000_0000_0000i128; i128, f32)), // i64 max boundary n128!((-0x8000_0100_0000_0000i128; i128, f32); (-(0x8000_0100_0000_0000u64 as f32); f32)), n128!((-0x8000_0000_0000_0800i128; i128, f64); (-(0x8000_0000_0000_0800u64 as f64); f64)), n128!((-0x8000_0000_0000_0001i128; i128)), n!(-0x8000_0000_0000_0000i64; i64, f32), n!(-0x7fff_ffff_ffff_ffffi64; i64), n!(-0x7fff_ffff_ffff_fc00i64; i64, f64), n!(-0x7fff_ff80_0000_0000i64; i64, f32), // f64 min exact int boundary n!(-0x20_0000_4000_0000i64; i64, f32), n!(-0x20_0000_0000_0002i64; i64, f64), n!(-0x20_0000_0000_0001i64; i64), n!(-0x20_0000_0000_0000i64; i64, f32), n!(-0x1f_ffff_ffff_ffffi64; i64, f64), n!(-0x1f_ffff_e000_0000i64; i64, f32), // f64 min exact half int boundary n!(-0x10_0000_2000_0000i64; i64, f32), n!(-0x10_0000_0000_0002i64; i64, f64), n!(-0x10_0000_0000_0001i64; i64, f64), n!(-0x10_0000_0000_0000i64; i64, f32), n!(-0xf_ffff_ffff_ffffi64 as f64 - 0.5; f64), n!(-0xf_ffff_ffff_ffffi64; i64, f64), n!(-0xf_ffff_f000_0000i64; i64, f32), // i32 min boundary n!(-0x8000_0100i64; i64, f32), n!(-0x8000_0001i64; i64, f64), n!(-0x8000_0000i64 as f64 - 0.5; f64), n!(-0x8000_0000i64; i64, f32), n!(-0x7fff_ffff as f64 - 0.5; f64), n!(-0x7fff_ffff; i32, f64), n!(-0x7fff_ff80; i32, f32), // f32 max exact int boundary n!(-0x100_0002; i32, f32), n!(-0x100_0001 as f64 - 0.5; f64), n!(-0x100_0001; i32, f64), n!(-0x100_0000 as f64 - 0.5; f64), n!(-0x100_0000; i32, f32), n!(-0xff_ffff as f64 - 0.5; f64), n!(-0xff_ffff; i32, f32), n!(-0xff_fffe as f64 - 0.5; f64), n!(-0xff_fffe; i32, f32), // f32 min exact half int boundary n!(-0x80_0002; i32, f32), n!(-0x80_0001 as f64 - 0.5; f64), n!(-0x80_0001; i32, f32), n!(-0x80_0000 as f64 - 0.5; f64), n!(-0x80_0000; i32, f32), n!(-0x7f_ffff as f64 - 0.5; f32), n!(-0x7f_ffff; i32, f32), n!(-0x7f_fffe as f64 - 0.5; f32), n!(-0x7f_fffe; i32, f32), // i16 min boundary n!(-0x8002; i32, f32), n!(-0x8001 as f32 - 0.5; f32), n!(-0x8001; i32, f32), n!(-0x8000 as f32 - 0.5; f32), n!(-0x8000; i16, f32), n!(-0x7fff as f32 - 0.5; f32), n!(-0x7fff; i16, f32), // i8 min boundary n!(-0x82; i16, f32), n!(-0x81 as f32 - 0.5; f32), n!(-0x81; i16, f32), n!(-0x80 as f32 - 0.5; f32), n!(-0x80; i8, f32), n!(-0x7f as f32 - 0.5; f32), n!(-0x7f; i8, f32), // around zero n!(-2; i8, f32), n!(-1.5; f32), n!(-1; i8, f32), n!(-0.5; f32), n!(-0.1; f32), n!(-f32::MIN_POSITIVE; f32), n!(-F32_SUBNORMAL_MIN; f32), n!(-f64::MIN_POSITIVE; f64), n!(-F64_SUBNORMAL_MIN; f64), &[N::u8(0), N::i8(0), N::f32(0.0), N::f32(-0.0)], // negative zeros should be handled! n!(F64_SUBNORMAL_MIN; f64), n!(f64::MIN_POSITIVE; f64), n!(F32_SUBNORMAL_MIN; f32), n!(f32::MIN_POSITIVE; f32), n!(0.1; f32), n!(0.5; f32), n!(1.0 - f32::EPSILON; f32), n!(1.0 - f32::EPSILON / 2.0; f32), n!(1.0 - f64::EPSILON; f64), n!(1.0 - f64::EPSILON / 2.0; f64), n!(1; u8, i8, f32), n!(1.0 + f64::EPSILON; f64), n!(1.0 + f64::EPSILON * 2.0; f64), n!(1.0 + f32::EPSILON; f32), n!(1.0 + f32::EPSILON * 2.0; f32), n!(1.5; f32), n!(2; u8, i8, f32), // i8 max boundary n!(0x7e; u8, i8, f32), n!(0x7f as f32 - 0.5; f32), n!(0x7f; u8, i8, f32), n!(0x7f as f32 + 0.5; f32), n!(0x80; u8, i16, f32), n!(0x80 as f32 + 0.5; f32), n!(0x81; u8, i16, f32), // u8 max boundary n!(0xfe; u8, i16, f32), n!(0xff as f32 - 0.5; f32), n!(0xff; u8, i16, f32), n!(0xff as f32 + 0.5; f32), n!(0x100; u16, i16, f32), n!(0x100 as f32 + 0.5; f32), n!(0x101; u16, i16, f32), // i16 max boundary n!(0x7ffe; u16, i16, f32), n!(0x7fff as f32 - 0.5; f32), n!(0x7fff; u16, i16, f32), n!(0x7fff as f32 + 0.5; f32), n!(0x8000; u16, i32, f32), n!(0x8000 as f32 + 0.5; f32), n!(0x8001; u16, i32, f32), // u16 max boundary n!(0xfffe; u16, i32, f32), n!(0xffff as f32 - 0.5; f32), n!(0xffff; u16, i32, f32), n!(0xffff as f32 + 0.5; f32), n!(0x1_0000; u32, i32, f32), n!(0x1_0000 as f32 + 0.5; f32), n!(0x1_0001; u32, i32, f32), // f32 max exact half int boundary n!(0x7f_fffe; u32, i32, f32), n!(0x7f_ffff as f64 - 0.5; f32), n!(0x7f_ffff; u32, i32, f32), n!(0x7f_ffff as f64 + 0.5; f32), n!(0x80_0000; u32, i32, f32), n!(0x80_0000 as f64 + 0.5; f64), n!(0x80_0001; u32, i32, f32), n!(0x80_0001 as f64 + 0.5; f64), n!(0x80_0002; u32, i32, f32), // f32 max exact int boundary n!(0xff_fffe; u32, i32, f32), n!(0xff_ffff as f64 - 0.5; f64), n!(0xff_ffff; u32, i32, f32), n!(0xff_ffff as f64 + 0.5; f64), n!(0x100_0000; u32, i32, f32), n!(0x100_0000 as f64 + 0.5; f64), n!(0x100_0001; u32, i32, f64), n!(0x100_0001 as f64 + 0.5; f64), n!(0x100_0002; u32, i32, f32), // i32 max boundary n!(0x7fff_ff80; u32, i32, f32), n!(0x7fff_ffff; u32, i32, f64), n!(0x7fff_ffff as f64 + 0.5; f64), n!(0x8000_0000u64; u32, i64, f32), n!(0x8000_0000u64 as f64 + 0.5; f64), n!(0x8000_0001u64; u32, i64, f64), n!(0x8000_0100u64; u32, i64, f32), // u32 max boundary n!(0xffff_ff00u64; u32, i64, f32), n!(0xffff_ffffu64; u32, i64, f64), n!(0xffff_ffffu64 as f64 + 0.5; f64), n!(0x1_0000_0000u64; u64, i64, f32), n!(0x1_0000_0000u64 as f64 + 0.5; f64), n!(0x1_0000_0001u64; u64, i64, f64), n!(0x1_0000_0200u64; u64, i64, f32), // f64 max exact half int boundary n!(0xf_ffff_f000_0000u64; u64, i64, f32), n!(0xf_ffff_ffff_ffffu64; u64, i64, f64), n!(0xf_ffff_ffff_ffffu64 as f64 + 0.5; f64), n!(0x10_0000_0000_0000u64; u64, i64, f32), n!(0x10_0000_0000_0001u64; u64, i64, f64), n!(0x10_0000_0000_0002u64; u64, i64, f64), n!(0x10_0000_2000_0000u64; u64, i64, f32), // f64 max exact int boundary n!(0x1f_ffff_e000_0000u64; u64, i64, f32), n!(0x1f_ffff_ffff_ffffu64; u64, i64, f64), n!(0x20_0000_0000_0000u64; u64, i64, f32), n!(0x20_0000_0000_0001u64; u64, i64), n!(0x20_0000_0000_0002u64; u64, i64, f64), n!(0x20_0000_4000_0000u64; u64, i64, f32), // i64 max boundary n!(0x7fff_ff80_0000_0000u64; u64, i64, f32), n!(0x7fff_ffff_ffff_fc00u64; u64, i64, f64), n!(0x7fff_ffff_ffff_ffffu64; u64, i64), n128!((0x8000_0000_0000_0000u64; u64, i128, f32); (0x8000_0000_0000_0000u64; u64, f32)), n128!((0x8000_0000_0000_0001u64; u64, i128); (0x8000_0000_0000_0001u64; u64)), n128!((0x8000_0000_0000_0800u64; u64, i128, f64); (0x8000_0000_0000_0800u64; u64, f64)), n128!((0x8000_0100_0000_0000u64; u64, i128, f32); (0x8000_0100_0000_0000u64; u64, f32)), // u64 max boundary (from this point we cannot use literals w/ >64 bits when not supported) n128!((0xffff_ff00_0000_0000u64; u64, i128, f32); (0xffff_ff00_0000_0000u64; u64, f32)), n128!((0xffff_ffff_ffff_f800u64; u64, i128, f64); (0xffff_ffff_ffff_f800u64; u64, f64)), n128!((0xffff_ffff_ffff_ffffu64; u64, i128); (0xffff_ffff_ffff_ffffu64; u64)), n128!((0x1_0000_0000_0000_0000u128; u128, i128, f32); (F32_32 * F32_32; f32)), n128!((0x1_0000_0000_0000_0001u128; u128, i128)), n128!((0x1_0000_0000_0000_1000u128; u128, i128, f64); (F64_32 * F64_32 + 0x1000 as f64; f64)), n128!((0x1_0000_0200_0000_0000u128; u128, i128, f32); (0x1_0000_0200u64 as f32 * F32_32; f32)), // i128 max boundary n128!((0x7fff_ff80_0000_0000_0000_0000_0000_0000u128; u128, i128, f32)), n128!((0x7fff_ffff_ffff_fc00_0000_0000_0000_0000u128; u128, i128, f64)), n128!((0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_ffffu128; u128, i128)), n128!((0x8000_0000_0000_0000_0000_0000_0000_0000u128; u128, f32)), n128!((0x8000_0000_0000_0000_0000_0000_0000_0001u128; u128)), n128!((0x8000_0000_0000_0800_0000_0000_0000_0000u128; u128, f64)), n128!((0x8000_0100_0000_0000_0000_0000_0000_0000u128; u128, f32)), // u128/f32 max boundary n128!((0xffff_ff00_0000_0000_0000_0000_0000_0000u128; u128, f32); (f32::MAX; f32)), n128!((0xffff_ffff_ffff_f800_0000_0000_0000_0000u128; u128, f64); ((F64_32 * F64_32 - 0x800 as f64) * F64_32 * F64_32; f64)), // 2^128 - 2^75 n128!((0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffffu128; u128)), n!(F64_32 * F64_32 * F64_32 * F64_32; f64), // 2^128 n!((F64_32 * F64_32 + 0x1000 as f64) * F64_32 * F64_32; f64), // 2^128 + 2^76 // f64 max boundary and infinity n!(f64::MAX; f64), n!(f32::INFINITY; f32), ]; fn expand_equiv_class(cls: &[N]) -> Vec { let mut ret = Vec::new(); for e in cls { // size extension match *e { N::u8(v) => ret.extend_from_slice(&[N::u8(v), N::u16(v as u16), N::u32(v as u32), N::u64(v as u64)]), N::u16(v) => ret.extend_from_slice(&[N::u16(v), N::u32(v as u32), N::u64(v as u64)]), N::u32(v) => ret.extend_from_slice(&[N::u32(v), N::u64(v as u64)]), N::u64(v) => ret.push(N::u64(v)), N::usize(v) => ret.push(N::usize(v)), N::i8(v) => ret.extend_from_slice(&[N::i8(v), N::i16(v as i16), N::i32(v as i32), N::i64(v as i64)]), N::i16(v) => ret.extend_from_slice(&[N::i16(v), N::i32(v as i32), N::i64(v as i64)]), N::i32(v) => ret.extend_from_slice(&[N::i32(v), N::i64(v as i64)]), N::i64(v) => ret.push(N::i64(v)), N::isize(v) => ret.push(N::isize(v)), N::f32(v) => ret.extend_from_slice(&[N::f32(v), N::f64(v as f64)]), N::f64(v) => ret.push(N::f64(v)), #[cfg(feature = "i128")] _ => {} } // size extension for i128 #[cfg(feature = "i128")] match *e { N::u8(v) => ret.push(N::u128(v as u128)), N::u16(v) => ret.push(N::u128(v as u128)), N::u32(v) => ret.push(N::u128(v as u128)), N::u64(v) => ret.push(N::u128(v as u128)), N::u128(v) => ret.push(N::u128(v)), N::i8(v) => ret.push(N::i128(v as i128)), N::i16(v) => ret.push(N::i128(v as i128)), N::i32(v) => ret.push(N::i128(v as i128)), N::i64(v) => ret.push(N::i128(v as i128)), N::i128(v) => ret.push(N::i128(v)), _ => {} } // insert equivalent usize/isize #[cfg(target_pointer_width = "32")] match *e { N::u8(v) => ret.push(N::usize(v as usize)), N::u16(v) => ret.push(N::usize(v as usize)), N::u32(v) => ret.push(N::usize(v as usize)), N::i8(v) => ret.push(N::isize(v as isize)), N::i16(v) => ret.push(N::isize(v as isize)), N::i32(v) => ret.push(N::isize(v as isize)), _ => {} } #[cfg(target_pointer_width = "64")] match *e { N::u8(v) => ret.push(N::usize(v as usize)), N::u16(v) => ret.push(N::usize(v as usize)), N::u32(v) => ret.push(N::usize(v as usize)), N::u64(v) => ret.push(N::usize(v as usize)), N::i8(v) => ret.push(N::isize(v as isize)), N::i16(v) => ret.push(N::isize(v as isize)), N::i32(v) => ret.push(N::isize(v as isize)), N::i64(v) => ret.push(N::isize(v as isize)), _ => {} } } ret } #[test] fn test_ordering() { let numbers: Vec<_> = NUMBERS.iter().map(|cls| expand_equiv_class(cls)).collect(); // comparison between numbers for icls in 0..numbers.len() { for jcls in 0..numbers.len() { let expected = icls.cmp(&jcls); for &i in &numbers[icls] { for &j in &numbers[jcls] { assert_cmp(i, j, expected); } } } } // comparison between numbers and NaNs for cls in &numbers { for &i in cls { assert_cmp(i, N::f32(f32::NAN), None); assert_cmp(i, N::f64(f64::NAN), None); assert_cmp(N::f32(f32::NAN), i, None); assert_cmp(N::f64(f64::NAN), i, None); } } // comparison between NaNs themselves assert_cmp(N::f32(f32::NAN), N::f32(f32::NAN), None); assert_cmp(N::f32(f32::NAN), N::f64(f64::NAN), None); assert_cmp(N::f64(f64::NAN), N::f32(f32::NAN), None); assert_cmp(N::f64(f64::NAN), N::f64(f64::NAN), None); }