├── .github └── workflows │ ├── ci.yaml │ ├── master.yaml │ └── pr.yaml ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── RELEASES.md ├── ci ├── rustup.sh └── test_full.sh └── src └── lib.rs /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: merge_group 3 | 4 | jobs: 5 | 6 | test: 7 | name: Test 8 | runs-on: ubuntu-latest 9 | strategy: 10 | matrix: 11 | rust: [ 12 | 1.60.0, # MSRV 13 | stable, 14 | beta, 15 | nightly 16 | ] 17 | steps: 18 | - uses: actions/checkout@v4 19 | - uses: actions/cache@v4 20 | if: startsWith(matrix.rust, '1') 21 | with: 22 | path: ~/.cargo/registry/index 23 | key: cargo-${{ matrix.rust }}-git-index 24 | - uses: dtolnay/rust-toolchain@master 25 | with: 26 | toolchain: ${{ matrix.rust }} 27 | - run: cargo build 28 | - run: ./ci/test_full.sh 29 | 30 | # try a target that doesn't have std at all, but does have alloc 31 | no_std: 32 | name: No Std 33 | runs-on: ubuntu-latest 34 | steps: 35 | - uses: actions/checkout@v4 36 | - uses: dtolnay/rust-toolchain@stable 37 | with: 38 | target: thumbv6m-none-eabi 39 | - run: cargo build --target thumbv6m-none-eabi --no-default-features --features "alloc libm serde rand" 40 | 41 | fmt: 42 | name: Format 43 | runs-on: ubuntu-latest 44 | steps: 45 | - uses: actions/checkout@v4 46 | - uses: dtolnay/rust-toolchain@1.62.0 47 | with: 48 | components: rustfmt 49 | - run: cargo fmt --all --check 50 | 51 | # One job that "summarizes" the success state of this pipeline. This can then be added to branch 52 | # protection, rather than having to add each job separately. 53 | success: 54 | name: Success 55 | runs-on: ubuntu-latest 56 | needs: [test, no_std, fmt] 57 | # Github branch protection is exceedingly silly and treats "jobs skipped because a dependency 58 | # failed" as success. So we have to do some contortions to ensure the job fails if any of its 59 | # dependencies fails. 60 | if: always() # make sure this is never "skipped" 61 | steps: 62 | # Manually check the status of all dependencies. `if: failure()` does not work. 63 | - name: check if any dependency failed 64 | run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}' 65 | -------------------------------------------------------------------------------- /.github/workflows/master.yaml: -------------------------------------------------------------------------------- 1 | name: master 2 | on: 3 | push: 4 | branches: 5 | - master 6 | schedule: 7 | - cron: '0 0 * * 0' # 00:00 Sunday 8 | 9 | jobs: 10 | 11 | test: 12 | name: Test 13 | runs-on: ubuntu-latest 14 | strategy: 15 | matrix: 16 | rust: [1.60.0, stable] 17 | steps: 18 | - uses: actions/checkout@v4 19 | - uses: actions/cache@v4 20 | if: startsWith(matrix.rust, '1') 21 | with: 22 | path: ~/.cargo/registry/index 23 | key: cargo-${{ matrix.rust }}-git-index 24 | - uses: dtolnay/rust-toolchain@master 25 | with: 26 | toolchain: ${{ matrix.rust }} 27 | - run: cargo build 28 | - run: ./ci/test_full.sh 29 | -------------------------------------------------------------------------------- /.github/workflows/pr.yaml: -------------------------------------------------------------------------------- 1 | name: PR 2 | on: 3 | pull_request: 4 | 5 | jobs: 6 | 7 | test: 8 | name: Test 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | rust: [1.60.0, stable] 13 | steps: 14 | - uses: actions/checkout@v4 15 | - uses: actions/cache@v4 16 | if: startsWith(matrix.rust, '1') 17 | with: 18 | path: ~/.cargo/registry/index 19 | key: cargo-${{ matrix.rust }}-git-index 20 | - uses: dtolnay/rust-toolchain@master 21 | with: 22 | toolchain: ${{ matrix.rust }} 23 | - run: cargo build 24 | - run: ./ci/test_full.sh 25 | 26 | fmt: 27 | name: Format 28 | runs-on: ubuntu-latest 29 | steps: 30 | - uses: actions/checkout@v4 31 | - uses: dtolnay/rust-toolchain@1.62.0 32 | with: 33 | components: rustfmt 34 | - run: cargo fmt --all --check 35 | 36 | # One job that "summarizes" the success state of this pipeline. This can then be added to branch 37 | # protection, rather than having to add each job separately. 38 | success: 39 | name: Success 40 | runs-on: ubuntu-latest 41 | needs: [test, fmt] 42 | # Github branch protection is exceedingly silly and treats "jobs skipped because a dependency 43 | # failed" as success. So we have to do some contortions to ensure the job fails if any of its 44 | # dependencies fails. 45 | if: always() # make sure this is never "skipped" 46 | steps: 47 | # Manually check the status of all dependencies. `if: failure()` does not work. 48 | - name: check if any dependency failed 49 | run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}' 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Cargo.lock 2 | target 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["The Rust Project Developers"] 3 | description = "A collection of numeric types and traits for Rust, including bigint,\ncomplex, rational, range iterators, generic integers, and more!\n" 4 | documentation = "https://docs.rs/num" 5 | homepage = "https://github.com/rust-num/num" 6 | keywords = ["mathematics", "numerics", "bignum"] 7 | categories = [ "algorithms", "data-structures", "science", "no-std" ] 8 | license = "MIT OR Apache-2.0" 9 | repository = "https://github.com/rust-num/num" 10 | name = "num" 11 | version = "0.4.3" 12 | readme = "README.md" 13 | exclude = ["/ci/*", "/.github/*"] 14 | edition = "2021" 15 | rust-version = "1.60" 16 | 17 | [package.metadata.docs.rs] 18 | features = ["std", "serde", "rand"] 19 | 20 | [dependencies] 21 | 22 | [dependencies.num-bigint] 23 | optional = true 24 | version = "0.4.5" 25 | default-features = false 26 | 27 | [dependencies.num-complex] 28 | version = "0.4.6" 29 | default-features = false 30 | 31 | [dependencies.num-integer] 32 | version = "0.1.46" 33 | default-features = false 34 | features = ["i128"] 35 | 36 | [dependencies.num-iter] 37 | version = "0.1.45" 38 | default-features = false 39 | features = ["i128"] 40 | 41 | [dependencies.num-rational] 42 | version = "0.4.2" 43 | default-features = false 44 | 45 | [dependencies.num-traits] 46 | version = "0.2.19" 47 | default-features = false 48 | features = ["i128"] 49 | 50 | [dev-dependencies] 51 | 52 | [features] 53 | default = ["std"] 54 | 55 | num-bigint = ["dep:num-bigint"] 56 | 57 | std = [ 58 | "dep:num-bigint", "num-bigint/std", 59 | "num-complex/std", 60 | "num-integer/std", 61 | "num-iter/std", 62 | "num-rational/std", "num-rational/num-bigint-std", 63 | "num-traits/std", 64 | ] 65 | 66 | alloc = ["dep:num-bigint", "num-rational/num-bigint"] 67 | 68 | libm = [ 69 | "num-complex/libm", 70 | "num-traits/libm", 71 | ] 72 | 73 | rand = [ 74 | "num-bigint/rand", 75 | "num-complex/rand", 76 | ] 77 | 78 | serde = [ 79 | "num-bigint/serde", 80 | "num-complex/serde", 81 | "num-rational/serde", 82 | ] 83 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 The Rust Project Developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # num 2 | 3 | [![crate](https://img.shields.io/crates/v/num.svg)](https://crates.io/crates/num) 4 | [![documentation](https://docs.rs/num/badge.svg)](https://docs.rs/num) 5 | [![minimum rustc 1.60](https://img.shields.io/badge/rustc-1.60+-red.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) 6 | [![build status](https://github.com/rust-num/num/workflows/master/badge.svg)](https://github.com/rust-num/num/actions) 7 | 8 | A collection of numeric types and traits for Rust. 9 | 10 | This includes new types for big integers, rationals (aka fractions), and complex numbers, 11 | new traits for generic programming on numeric properties like `Integer`, 12 | and generic range iterators. 13 | 14 | `num` is a meta-crate, re-exporting items from these sub-crates: 15 | 16 | | Repository | Crate | Documentation | 17 | | ---------- | ----- | ------------- | 18 | | [`num-bigint`] | [![crate][bigint-cb]][bigint-c] | [![documentation][bigint-db]][bigint-d] 19 | | [`num-complex`] | [![crate][complex-cb]][complex-c] | [![documentation][complex-db]][complex-d] 20 | | [`num-integer`] | [![crate][integer-cb]][integer-c] | [![documentation][integer-db]][integer-d] 21 | | [`num-iter`] | [![crate][iter-cb]][iter-c] | [![documentation][iter-db]][iter-d] 22 | | [`num-rational`] | [![crate][rational-cb]][rational-c] | [![documentation][rational-db]][rational-d] 23 | | [`num-traits`] | [![crate][traits-cb]][traits-c] | [![documentation][traits-db]][traits-d] 24 | | ([`num-derive`]) | [![crate][derive-cb]][derive-c] | [![documentation][derive-db]][derive-d] 25 | 26 | Note: `num-derive` is listed here for reference, but it's not directly included 27 | in `num`. This is a `proc-macro` crate for deriving some of `num`'s traits. 28 | 29 | ## Usage 30 | 31 | Add this to your `Cargo.toml`: 32 | 33 | ```toml 34 | [dependencies] 35 | num = "0.4" 36 | ``` 37 | 38 | ## Features 39 | 40 | This crate can be used without the standard library (`#![no_std]`) by disabling 41 | the default `std` feature. Use this in `Cargo.toml`: 42 | 43 | ```toml 44 | [dependencies.num] 45 | version = "0.4" 46 | default-features = false 47 | ``` 48 | 49 | The `num-bigint` crate requires the `std` feature, or the `alloc` feature may 50 | be used instead with Rust 1.36 and later. Other sub-crates may also have 51 | limited functionality when used without `std`. 52 | 53 | The `libm` feature uses pure-Rust floating point implementations in `no_std` 54 | builds, enabling the `Float` trait and related `Complex` methods. 55 | 56 | The `rand` feature enables randomization traits in `num-bigint` and 57 | `num-complex`. 58 | 59 | The `serde` feature enables serialization for types in `num-bigint`, 60 | `num-complex`, and `num-rational`. 61 | 62 | The `num` meta-crate no longer supports features to toggle the inclusion of 63 | the individual sub-crates. If you need such control, you are recommended to 64 | directly depend on your required crates instead. 65 | 66 | ## Releases 67 | 68 | Release notes are available in [RELEASES.md](RELEASES.md). 69 | 70 | ## Compatibility 71 | 72 | The `num` crate as a whole is tested for rustc 1.60 and greater. 73 | 74 | The `num-traits`, `num-integer`, and `num-iter` crates are individually tested 75 | for rustc 1.8 and greater, if you require such older compatibility. 76 | 77 | ## License 78 | 79 | Licensed under either of 80 | 81 | * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) 82 | * [MIT license](http://opensource.org/licenses/MIT) 83 | 84 | at your option. 85 | 86 | ### Contribution 87 | 88 | Unless you explicitly state otherwise, any contribution intentionally submitted 89 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be 90 | dual licensed as above, without any additional terms or conditions. 91 | 92 | 93 | [`num-bigint`]: https://github.com/rust-num/num-bigint 94 | [bigint-c]: https://crates.io/crates/num-bigint 95 | [bigint-cb]: https://img.shields.io/crates/v/num-bigint.svg 96 | [bigint-d]: https://docs.rs/num-bigint/ 97 | [bigint-db]: https://docs.rs/num-bigint/badge.svg 98 | 99 | [`num-complex`]: https://github.com/rust-num/num-complex 100 | [complex-c]: https://crates.io/crates/num-complex 101 | [complex-cb]: https://img.shields.io/crates/v/num-complex.svg 102 | [complex-d]: https://docs.rs/num-complex/ 103 | [complex-db]: https://docs.rs/num-complex/badge.svg 104 | 105 | [`num-derive`]: https://github.com/rust-num/num-derive 106 | [derive-c]: https://crates.io/crates/num-derive 107 | [derive-cb]: https://img.shields.io/crates/v/num-derive.svg 108 | [derive-d]: https://docs.rs/num-derive/ 109 | [derive-db]: https://docs.rs/num-derive/badge.svg 110 | 111 | [`num-integer`]: https://github.com/rust-num/num-integer 112 | [integer-c]: https://crates.io/crates/num-integer 113 | [integer-cb]: https://img.shields.io/crates/v/num-integer.svg 114 | [integer-d]: https://docs.rs/num-integer/ 115 | [integer-db]: https://docs.rs/num-integer/badge.svg 116 | 117 | [`num-iter`]: https://github.com/rust-num/num-iter 118 | [iter-c]: https://crates.io/crates/num-iter 119 | [iter-cb]: https://img.shields.io/crates/v/num-iter.svg 120 | [iter-d]: https://docs.rs/num-iter/ 121 | [iter-db]: https://docs.rs/num-iter/badge.svg 122 | 123 | [`num-rational`]: https://github.com/rust-num/num-rational 124 | [rational-c]: https://crates.io/crates/num-rational 125 | [rational-cb]: https://img.shields.io/crates/v/num-rational.svg 126 | [rational-d]: https://docs.rs/num-rational/ 127 | [rational-db]: https://docs.rs/num-rational/badge.svg 128 | 129 | [`num-traits`]: https://github.com/rust-num/num-traits 130 | [traits-c]: https://crates.io/crates/num-traits 131 | [traits-cb]: https://img.shields.io/crates/v/num-traits.svg 132 | [traits-d]: https://docs.rs/num-traits/ 133 | [traits-db]: https://docs.rs/num-traits/badge.svg 134 | -------------------------------------------------------------------------------- /RELEASES.md: -------------------------------------------------------------------------------- 1 | # Release 0.4.3 (2024-05-08) 2 | 3 | - Upgrade to 2021 edition, **MSRV 1.60**. 4 | - Updated all sub-crates to their latest versions. 5 | 6 | # Release 0.4.2 (2024-04-12) 7 | 8 | - Updated all sub-crates to their latest versions. 9 | 10 | # Release 0.4.1 (2023-07-11) 11 | 12 | - Updated all sub-crates to their latest versions. 13 | 14 | # Release 0.4.0 (2021-03-05) 15 | 16 | - Updated `num-bigint`, `num-complex`, and `num-rational` to 0.4.0. 17 | - Updated to `rand` 0.8 in `num-bigint` and `num-complex`. 18 | - `Rational` is deprecated in favor of explicit `Rational32` or `Rational64`. 19 | - As with prior release bumps, all items exported from `num-integer`, 20 | `num-iter`, and `num-traits` are still semver-compatible with those exported 21 | by earlier version of `num`. 22 | 23 | # Release 0.3.1 (2020-11-03) 24 | 25 | - Updated all sub-crates to their latest versions. 26 | - Clarify the license specification as "MIT OR Apache-2.0". 27 | 28 | # Release 0.3.0 (2020-06-13) 29 | 30 | All items exported from `num-integer`, `num-iter`, and `num-traits` are still 31 | semver-compatible with those exported by `num` 0.1 and 0.2. If you have these 32 | as public dependencies in your own crates, it is not a breaking change to move 33 | to `num` 0.3. However, this is not true of `num-bigint`, `num-complex`, or 34 | `num-rational`, as those exported items are distinct in this release. 35 | 36 | ### Enhancements 37 | 38 | - Updates to `num-integer`, `num-iter`, and `num-traits` are still compatible 39 | with `num` 0.1 and 0.2. 40 | - The "alloc" feature enables `bigint` without `std` on Rust 1.36+. 41 | - The "libm" feature enables `Float` without `std` in `traits` and `complex`. 42 | - Please see the release notes of the individual sub-crates for details. 43 | 44 | ### Breaking Changes 45 | 46 | - `num` now requires rustc 1.31 or greater. 47 | - The "i128" opt-in feature was removed, now always available. 48 | - `rand` support has been updated to 0.7, requiring Rust 1.32. 49 | 50 | **Contributors**: @cuviper 51 | 52 | # Release 0.2.1 (2019-01-09) 53 | 54 | - Updated all sub-crates to their latest versions. 55 | 56 | **Contributors**: @cuviper, @ignatenkobrain, @jimbo1qaz 57 | 58 | # Release 0.2.0 (2018-06-29) 59 | 60 | All items exported from `num-integer`, `num-iter`, and `num-traits` are still 61 | semver-compatible with those exported by `num` 0.1. If you have these as public 62 | dependencies in your own crates, it is not a breaking change to move to `num` 63 | 0.2. However, this is not true of `num-bigint`, `num-complex`, or 64 | `num-rational`, as those exported items are distinct in this release. 65 | 66 | A few common changes are listed below, but most of the development happens in 67 | the individual sub-crates. Please consult their release notes for more details 68 | about recent changes: 69 | [`num-bigint`](https://github.com/rust-num/num-bigint/blob/master/RELEASES.md), 70 | [`num-complex`](https://github.com/rust-num/num-complex/blob/master/RELEASES.md), 71 | [`num-integer`](https://github.com/rust-num/num-integer/blob/master/RELEASES.md), 72 | [`num-iter`](https://github.com/rust-num/num-iter/blob/master/RELEASES.md), 73 | [`num-rational`](https://github.com/rust-num/num-rational/blob/master/RELEASES.md), 74 | and [`num-traits`](https://github.com/rust-num/num-traits/blob/master/RELEASES.md). 75 | 76 | ### Enhancements 77 | 78 | - Updates to `num-integer`, `num-iter`, and `num-traits` are still compatible 79 | with `num` 0.1. 80 | - 128-bit integers are supported with Rust 1.26 and later. 81 | - `BigInt`, `BigUint`, `Complex`, and `Ratio` all implement `Sum` and `Product`. 82 | 83 | ### Breaking Changes 84 | 85 | - `num` now requires rustc 1.15 or greater. 86 | - `num-bigint`, `num-complex`, and `num-rational` have all been updated to 0.2. 87 | - It's no longer possible to toggle individual `num-*` sub-crates using cargo 88 | features. If you need that control, please use those crates directly. 89 | - There is now a `std` feature, enabled by default, along with the implication 90 | that building *without* this feature makes this a `#![no_std]` crate. 91 | `num::bigint` is not available without `std`, and the other sub-crates may 92 | have limited functionality. 93 | - The `serde` dependency has been updated to 1.0, still disabled by default. 94 | The `rustc-serialize` crate is no longer supported by `num`. 95 | - The `rand` dependency has been updated to 0.5, now disabled by default. This 96 | requires rustc 1.22 or greater for `rand`'s own requirement. 97 | 98 | **Contributors**: @CAD97, @cuviper, and the many sub-crate contributors! 99 | 100 | # Release 0.1.42 (2018-02-08) 101 | 102 | - [All of the num sub-crates now have their own source repositories][num-356]. 103 | - Updated num sub-crates to their latest versions. 104 | 105 | **Contributors**: @cuviper 106 | 107 | [num-356]: https://github.com/rust-num/num/pull/356 108 | 109 | 110 | # Prior releases 111 | 112 | No prior release notes were kept. Thanks all the same to the many 113 | contributors that have made this crate what it is! 114 | -------------------------------------------------------------------------------- /ci/rustup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Use rustup to locally run the same suite of tests as .github/workflows/ 3 | # (You should first install/update all of the versions below.) 4 | 5 | set -ex 6 | 7 | ci=$(dirname $0) 8 | for version in 1.60.0 stable beta nightly; do 9 | rustup run "$version" "$ci/test_full.sh" 10 | done 11 | -------------------------------------------------------------------------------- /ci/test_full.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | CRATE=num 6 | MSRV=1.60 7 | 8 | get_rust_version() { 9 | local array=($(rustc --version)); 10 | echo "${array[1]}"; 11 | return 0; 12 | } 13 | RUST_VERSION=$(get_rust_version) 14 | 15 | check_version() { 16 | IFS=. read -ra rust <<< "$RUST_VERSION" 17 | IFS=. read -ra want <<< "$1" 18 | [[ "${rust[0]}" -gt "${want[0]}" || 19 | ( "${rust[0]}" -eq "${want[0]}" && 20 | "${rust[1]}" -ge "${want[1]}" ) 21 | ]] 22 | } 23 | 24 | echo "Testing $CRATE on rustc $RUST_VERSION" 25 | if ! check_version $MSRV ; then 26 | echo "The minimum for $CRATE is rustc $MSRV" 27 | exit 1 28 | fi 29 | 30 | STD_FEATURES=(libm serde rand) 31 | ALLOC_FEATURES=(libm serde rand) 32 | NO_STD_FEATURES=(libm) 33 | echo "Testing supported features: ${STD_FEATURES[*]}" 34 | echo " alloc supported features: ${ALLOC_FEATURES[*]}" 35 | echo " no_std supported features: ${NO_STD_FEATURES[*]}" 36 | 37 | cargo generate-lockfile 38 | 39 | set -x 40 | 41 | # test the default with std 42 | cargo build 43 | cargo test 44 | 45 | # test each isolated feature with std 46 | for feature in ${STD_FEATURES[*]}; do 47 | cargo build --no-default-features --features="std $feature" 48 | cargo test --no-default-features --features="std $feature" 49 | done 50 | 51 | # test all supported features with std 52 | cargo build --no-default-features --features="std ${STD_FEATURES[*]}" 53 | cargo test --no-default-features --features="std ${STD_FEATURES[*]}" 54 | 55 | 56 | # test minimal `no_std` 57 | cargo build --no-default-features 58 | cargo test --no-default-features 59 | 60 | # test each isolated feature without std 61 | for feature in ${NO_STD_FEATURES[*]}; do 62 | cargo build --no-default-features --features="$feature" 63 | cargo test --no-default-features --features="$feature" 64 | done 65 | 66 | # test all supported features without std 67 | cargo build --no-default-features --features="${NO_STD_FEATURES[*]}" 68 | cargo test --no-default-features --features="${NO_STD_FEATURES[*]}" 69 | 70 | 71 | # test minimal with alloc 72 | cargo build --no-default-features --features="alloc" 73 | cargo test --no-default-features --features="alloc" 74 | 75 | # test each isolated feature with alloc 76 | for feature in ${ALLOC_FEATURES[*]}; do 77 | cargo build --no-default-features --features="alloc $feature" 78 | cargo test --no-default-features --features="alloc $feature" 79 | done 80 | 81 | # test all supported features with alloc 82 | cargo build --no-default-features --features="alloc ${ALLOC_FEATURES[*]}" 83 | cargo test --no-default-features --features="alloc ${ALLOC_FEATURES[*]}" 84 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2014-2016 The Rust Project Developers. See the COPYRIGHT 2 | // file at the top-level directory of this distribution and at 3 | // http://rust-lang.org/COPYRIGHT. 4 | // 5 | // Licensed under the Apache License, Version 2.0 or the MIT license 7 | // , at your 8 | // option. This file may not be copied, modified, or distributed 9 | // except according to those terms. 10 | 11 | //! A collection of numeric types and traits for Rust. 12 | //! 13 | //! This includes new types for big integers, rationals, and complex numbers, 14 | //! new traits for generic programming on numeric properties like `Integer`, 15 | //! and generic range iterators. 16 | //! 17 | //! ## Example 18 | //! 19 | //! This example uses the BigRational type and [Newton's method][newt] to 20 | //! approximate a square root to arbitrary precision: 21 | //! 22 | //! ``` 23 | //! # #[cfg(any(feature = "alloc", feature = "std"))] 24 | //! # mod test { 25 | //! 26 | //! use num::FromPrimitive; 27 | //! use num::bigint::BigInt; 28 | //! use num::rational::{Ratio, BigRational}; 29 | //! 30 | //! # pub 31 | //! fn approx_sqrt(number: u64, iterations: usize) -> BigRational { 32 | //! let start: Ratio = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap()); 33 | //! let mut approx = start.clone(); 34 | //! 35 | //! for _ in 0..iterations { 36 | //! approx = (&approx + (&start / &approx)) / 37 | //! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap()); 38 | //! } 39 | //! 40 | //! approx 41 | //! } 42 | //! # } 43 | //! # #[cfg(not(any(feature = "alloc", feature = "std")))] 44 | //! # mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } } 45 | //! # use crate::test::approx_sqrt; 46 | //! 47 | //! fn main() { 48 | //! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416 49 | //! } 50 | //! 51 | //! ``` 52 | //! 53 | //! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method 54 | //! 55 | //! ## Compatibility 56 | //! 57 | //! The `num` crate is tested for rustc 1.60 and greater. 58 | 59 | #![doc(html_root_url = "https://docs.rs/num/0.4")] 60 | #![no_std] 61 | 62 | #[cfg(any(feature = "alloc", feature = "std"))] 63 | pub use num_bigint::{BigInt, BigUint}; 64 | 65 | pub use num_complex::Complex; 66 | 67 | #[cfg(any(feature = "alloc", feature = "std"))] 68 | pub use num_rational::BigRational; 69 | #[allow(deprecated)] 70 | pub use num_rational::Rational; 71 | pub use num_rational::{Rational32, Rational64}; 72 | 73 | pub use num_integer::Integer; 74 | 75 | pub use num_iter::{range, range_inclusive, range_step, range_step_inclusive}; 76 | 77 | #[cfg(any(feature = "libm", feature = "std"))] 78 | pub use num_traits::Float; 79 | pub use num_traits::{ 80 | abs, abs_sub, cast, checked_pow, clamp, one, pow, signum, zero, Bounded, CheckedAdd, 81 | CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, NumCast, One, PrimInt, Saturating, 82 | Signed, ToPrimitive, Unsigned, Zero, 83 | }; 84 | 85 | #[cfg(any(feature = "alloc", feature = "std"))] 86 | pub mod bigint { 87 | pub use num_bigint::*; 88 | } 89 | 90 | pub mod complex { 91 | pub use num_complex::*; 92 | } 93 | 94 | pub mod integer { 95 | pub use num_integer::*; 96 | } 97 | 98 | pub mod iter { 99 | pub use num_iter::*; 100 | } 101 | 102 | pub mod traits { 103 | pub use num_traits::*; 104 | } 105 | 106 | pub mod rational { 107 | pub use num_rational::*; 108 | } 109 | --------------------------------------------------------------------------------