Skip to content

Commit ca24abd

Browse files
committed
auto merge of rust-lang#15716 : treeman/rust/bigint-doc, r=alexcrichton
Also use `///` for documentation instead of `/**`. End comments with `.`.
2 parents c4b1077 + 428c7bc commit ca24abd

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

src/libnum/bigint.rs

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,53 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/*!
12-
13-
A Big integer (signed version: `BigInt`, unsigned version: `BigUint`).
14-
15-
A `BigUint` is represented as an array of `BigDigit`s.
16-
A `BigInt` is a combination of `BigUint` and `Sign`.
17-
*/
11+
//! A Big integer (signed version: `BigInt`, unsigned version: `BigUint`).
12+
//!
13+
//! A `BigUint` is represented as an array of `BigDigit`s.
14+
//! A `BigInt` is a combination of `BigUint` and `Sign`.
15+
//!
16+
//! Common numerical operations are overloaded, so we can treat them
17+
//! the same way we treat other numbers.
18+
//!
19+
//! ## Example
20+
//!
21+
//! ```rust
22+
//! use num::bigint::BigUint;
23+
//! use std::num::{Zero, One};
24+
//! use std::mem::replace;
25+
//!
26+
//! // Calculate large fibonacci numbers.
27+
//! fn fib(n: uint) -> BigUint {
28+
//! let mut f0: BigUint = Zero::zero();
29+
//! let mut f1: BigUint = One::one();
30+
//! for _ in range(0, n) {
31+
//! let f2 = f0 + f1;
32+
//! // This is a low cost way of swapping f0 with f1 and f1 with f2.
33+
//! f0 = replace(&mut f1, f2);
34+
//! }
35+
//! f0
36+
//! }
37+
//!
38+
//! // This is a very large number.
39+
//! println!("fib(1000) = {}", fib(1000));
40+
//! ```
41+
//!
42+
//! It's easy to generate large random numbers:
43+
//!
44+
//! ```rust
45+
//! use num::bigint::{ToBigInt, RandBigInt};
46+
//! use std::rand;
47+
//!
48+
//! let mut rng = rand::task_rng();
49+
//! let a = rng.gen_bigint(1000u);
50+
//!
51+
//! let low = -10000i.to_bigint().unwrap();
52+
//! let high = 10000i.to_bigint().unwrap();
53+
//! let b = rng.gen_bigint_range(&low, &high);
54+
//!
55+
//! // Probably an even larger number.
56+
//! println!("{}", a * b);
57+
//! ```
1858
1959
use Integer;
2060
use rand::Rng;
@@ -28,15 +68,11 @@ use std::num::{Zero, One, ToStrRadix, FromStrRadix};
2868
use std::string::String;
2969
use std::{uint, i64, u64};
3070

31-
/**
32-
A `BigDigit` is a `BigUint`'s composing element.
33-
*/
71+
/// A `BigDigit` is a `BigUint`'s composing element.
3472
pub type BigDigit = u32;
3573

36-
/**
37-
A `DoubleBigDigit` is the internal type used to do the computations. Its
38-
size is the double of the size of `BigDigit`.
39-
*/
74+
/// A `DoubleBigDigit` is the internal type used to do the computations. Its
75+
/// size is the double of the size of `BigDigit`.
4076
pub type DoubleBigDigit = u64;
4177

4278
pub static ZERO_BIG_DIGIT: BigDigit = 0;
@@ -70,12 +106,10 @@ pub mod BigDigit {
70106
}
71107
}
72108

73-
/**
74-
A big unsigned integer type.
75-
76-
A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
77-
`(a + b * BigDigit::base + c * BigDigit::base^2)`.
78-
*/
109+
/// A big unsigned integer type.
110+
///
111+
/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
112+
/// `(a + b * BigDigit::base + c * BigDigit::base^2)`.
79113
#[deriving(Clone)]
80114
pub struct BigUint {
81115
data: Vec<BigDigit>
@@ -460,11 +494,9 @@ impl Integer for BigUint {
460494
}
461495
}
462496

463-
/**
464-
* Calculates the Greatest Common Divisor (GCD) of the number and `other`
465-
*
466-
* The result is always positive
467-
*/
497+
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`.
498+
///
499+
/// The result is always positive.
468500
#[inline]
469501
fn gcd(&self, other: &BigUint) -> BigUint {
470502
// Use Euclid's algorithm
@@ -478,17 +510,15 @@ impl Integer for BigUint {
478510
return n;
479511
}
480512

481-
/**
482-
* Calculates the Lowest Common Multiple (LCM) of the number and `other`
483-
*/
513+
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
484514
#[inline]
485515
fn lcm(&self, other: &BigUint) -> BigUint { ((*self * *other) / self.gcd(other)) }
486516

487-
/// Returns `true` if the number can be divided by `other` without leaving a remainder
517+
/// Returns `true` if the number can be divided by `other` without leaving a remainder.
488518
#[inline]
489519
fn divides(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
490520

491-
/// Returns `true` if the number is divisible by `2`
521+
/// Returns `true` if the number is divisible by `2`.
492522
#[inline]
493523
fn is_even(&self) -> bool {
494524
// Considering only the last digit.
@@ -498,7 +528,7 @@ impl Integer for BigUint {
498528
}
499529
}
500530

501-
/// Returns `true` if the number is not divisible by `2`
531+
/// Returns `true` if the number is not divisible by `2`.
502532
#[inline]
503533
fn is_odd(&self) -> bool { !self.is_even() }
504534
}
@@ -1068,33 +1098,29 @@ impl Integer for BigInt {
10681098
}
10691099
}
10701100

1071-
/**
1072-
* Calculates the Greatest Common Divisor (GCD) of the number and `other`
1073-
*
1074-
* The result is always positive
1075-
*/
1101+
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`.
1102+
///
1103+
/// The result is always positive.
10761104
#[inline]
10771105
fn gcd(&self, other: &BigInt) -> BigInt {
10781106
BigInt::from_biguint(Plus, self.data.gcd(&other.data))
10791107
}
10801108

1081-
/**
1082-
* Calculates the Lowest Common Multiple (LCM) of the number and `other`
1083-
*/
1109+
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
10841110
#[inline]
10851111
fn lcm(&self, other: &BigInt) -> BigInt {
10861112
BigInt::from_biguint(Plus, self.data.lcm(&other.data))
10871113
}
10881114

1089-
/// Returns `true` if the number can be divided by `other` without leaving a remainder
1115+
/// Returns `true` if the number can be divided by `other` without leaving a remainder.
10901116
#[inline]
10911117
fn divides(&self, other: &BigInt) -> bool { self.data.divides(&other.data) }
10921118

1093-
/// Returns `true` if the number is divisible by `2`
1119+
/// Returns `true` if the number is divisible by `2`.
10941120
#[inline]
10951121
fn is_even(&self) -> bool { self.data.is_even() }
10961122

1097-
/// Returns `true` if the number is not divisible by `2`
1123+
/// Returns `true` if the number is not divisible by `2`.
10981124
#[inline]
10991125
fn is_odd(&self) -> bool { self.data.is_odd() }
11001126
}

0 commit comments

Comments
 (0)