lib.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2014 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. //! Simple numerics.
  11. //!
  12. //! This crate contains arbitrary-sized integer, rational, and complex types.
  13. //!
  14. //! ## Example
  15. //!
  16. //! This example uses the BigRational type and [Newton's method][newt] to
  17. //! approximate a square root to arbitrary precision:
  18. //!
  19. //! ```
  20. //! extern crate num;
  21. //!
  22. //! use num::bigint::BigInt;
  23. //! use num::rational::{Ratio, BigRational};
  24. //!
  25. //! fn approx_sqrt(number: u64, iterations: uint) -> BigRational {
  26. //! let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
  27. //! let mut approx = start.clone();
  28. //!
  29. //! for _ in range(0, iterations) {
  30. //! approx = (approx + (start / approx)) /
  31. //! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
  32. //! }
  33. //!
  34. //! approx
  35. //! }
  36. //!
  37. //! fn main() {
  38. //! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
  39. //! }
  40. //! ```
  41. //!
  42. //! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
  43. #![feature(macro_rules)]
  44. #![feature(default_type_params)]
  45. #![crate_name = "num"]
  46. #![experimental]
  47. #![crate_type = "rlib"]
  48. #![crate_type = "dylib"]
  49. #![license = "MIT/ASL2"]
  50. #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
  51. html_favicon_url = "http://www.rust-lang.org/favicon.ico",
  52. html_root_url = "http://doc.rust-lang.org/master/",
  53. html_playground_url = "http://play.rust-lang.org/")]
  54. #![allow(deprecated)] // from_str_radix
  55. extern crate rand;
  56. pub use bigint::{BigInt, BigUint};
  57. pub use rational::{Rational, BigRational};
  58. pub use complex::Complex;
  59. pub use integer::Integer;
  60. pub mod bigint;
  61. pub mod complex;
  62. pub mod integer;
  63. pub mod rational;