lib.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //! A collection of numeric types and traits for Rust.
  11. //!
  12. //! This includes new types for big integers, rationals, and complex numbers,
  13. //! new traits for generic programming on numeric properties like `Integer`,
  14. //! and generic range iterators.
  15. //!
  16. //! ## Example
  17. //!
  18. //! This example uses the BigRational type and [Newton's method][newt] to
  19. //! approximate a square root to arbitrary precision:
  20. //!
  21. //! ```
  22. //! extern crate num;
  23. //! # #[cfg(all(feature = "bigint", feature="rational"))]
  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<BigInt> = 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(all(feature = "bigint", feature="rational")))]
  44. //! # mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } }
  45. //! # use 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. #![doc(html_logo_url = "https://rust-num.github.io/num/rust-logo-128x128-blk-v2.png",
  55. html_favicon_url = "https://rust-num.github.io/num/favicon.ico",
  56. html_root_url = "https://rust-num.github.io/num/",
  57. html_playground_url = "http://play.integer32.com/")]
  58. extern crate num_traits;
  59. extern crate num_integer;
  60. extern crate num_iter;
  61. #[cfg(feature = "num-complex")]
  62. extern crate num_complex;
  63. #[cfg(feature = "num-bigint")]
  64. extern crate num_bigint;
  65. #[cfg(feature = "num-rational")]
  66. extern crate num_rational;
  67. #[cfg(feature = "num-bigint")]
  68. pub use num_bigint::{BigInt, BigUint};
  69. #[cfg(feature = "num-rational")]
  70. pub use num_rational::Rational;
  71. #[cfg(all(feature = "num-rational", feature="num-bigint"))]
  72. pub use num_rational::BigRational;
  73. #[cfg(feature = "num-complex")]
  74. pub use num_complex::Complex;
  75. pub use num_integer::Integer;
  76. pub use num_iter::{range, range_inclusive, range_step, range_step_inclusive};
  77. pub use num_traits::{Num, Zero, One, Signed, Unsigned, Bounded,
  78. one, zero, abs, abs_sub, signum,
  79. Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv,
  80. PrimInt, Float, ToPrimitive, FromPrimitive, NumCast, cast,
  81. pow, checked_pow};
  82. #[cfg(feature = "num-bigint")]
  83. pub mod bigint {
  84. pub use num_bigint::*;
  85. }
  86. #[cfg(feature = "num-complex")]
  87. pub mod complex {
  88. pub use num_complex::*;
  89. }
  90. pub mod integer {
  91. pub use num_integer::*;
  92. }
  93. pub mod iter {
  94. pub use num_iter::*;
  95. }
  96. pub mod traits {
  97. pub use num_traits::*;
  98. }
  99. #[cfg(feature = "num-rational")]
  100. pub mod rational {
  101. pub use num_rational::*;
  102. }