1 // Copyright 2013-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.
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.
17 use std::from_str::FromStr;
18 use std::num::{Zero, One, ToStrRadix, FromStrRadix};
19 use bigint::{BigInt, BigUint, Sign, Plus, Minus};
21 /// Represents the ratio between 2 numbers.
29 /// Alias for a `Ratio` of machine-sized integers.
30 pub type Rational = Ratio<int>;
31 pub type Rational32 = Ratio<i32>;
32 pub type Rational64 = Ratio<i64>;
34 /// Alias for arbitrary precision rationals.
35 pub type BigRational = Ratio<BigInt>;
37 impl<T: Clone + Integer + Ord>
39 /// Create a ratio representing the integer `t`.
41 pub fn from_integer(t: T) -> Ratio<T> {
42 Ratio::new_raw(t, One::one())
45 /// Create a ratio without checking for `denom == 0` or reducing.
47 pub fn new_raw(numer: T, denom: T) -> Ratio<T> {
48 Ratio { numer: numer, denom: denom }
51 /// Create a new Ratio. Fails if `denom == 0`.
53 pub fn new(numer: T, denom: T) -> Ratio<T> {
54 if denom == Zero::zero() {
55 fail!("denominator == 0");
57 let mut ret = Ratio::new_raw(numer, denom);
62 /// Convert to an integer.
64 pub fn to_integer(&self) -> T {
68 /// Gets an immutable reference to the numerator.
70 pub fn numer<'a>(&'a self) -> &'a T {
74 /// Gets an immutable reference to the denominator.
76 pub fn denom<'a>(&'a self) -> &'a T {
80 /// Return true if the rational number is an integer (denominator is 1).
82 pub fn is_integer(&self) -> bool {
83 self.denom == One::one()
86 /// Put self into lowest terms, with denom > 0.
87 fn reduce(&mut self) {
88 let g : T = self.numer.gcd(&self.denom);
90 // FIXME(#5992): assignment operator overloads
92 self.numer = self.numer / g;
93 // FIXME(#5992): assignment operator overloads
95 self.denom = self.denom / g;
97 // keep denom positive!
98 if self.denom < Zero::zero() {
99 self.numer = -self.numer;
100 self.denom = -self.denom;
104 /// Return a `reduce`d copy of self.
105 pub fn reduced(&self) -> Ratio<T> {
106 let mut ret = self.clone();
111 /// Return the reciprocal
113 pub fn recip(&self) -> Ratio<T> {
114 Ratio::new_raw(self.denom.clone(), self.numer.clone())
117 pub fn floor(&self) -> Ratio<T> {
118 if *self < Zero::zero() {
119 Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
121 Ratio::from_integer(self.numer / self.denom)
125 pub fn ceil(&self) -> Ratio<T> {
126 if *self < Zero::zero() {
127 Ratio::from_integer(self.numer / self.denom)
129 Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
134 pub fn round(&self) -> Ratio<T> {
135 if *self < Zero::zero() {
136 Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
138 Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
143 pub fn trunc(&self) -> Ratio<T> {
144 Ratio::from_integer(self.numer / self.denom)
147 pub fn fract(&self) -> Ratio<T> {
148 Ratio::new_raw(self.numer % self.denom, self.denom.clone())
153 /// Converts a float into a rational number
154 pub fn from_float<T: Float>(f: T) -> Option<BigRational> {
158 let (mantissa, exponent, sign) = f.integer_decode();
159 let bigint_sign: Sign = if sign == 1 { Plus } else { Minus };
161 let one: BigInt = One::one();
162 let denom: BigInt = one << ((-exponent) as uint);
163 let numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap();
164 Some(Ratio::new(BigInt::from_biguint(bigint_sign, numer), denom))
166 let mut numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap();
167 numer = numer << (exponent as uint);
168 Some(Ratio::from_integer(BigInt::from_biguint(bigint_sign, numer)))
175 // comparing a/b and c/d is the same as comparing a*d and b*c, so we
176 // abstract that pattern. The following macro takes a trait and either
177 // a comma-separated list of "method name -> return value" or just
178 // "method name" (return value is bool in that case)
179 macro_rules! cmp_impl {
180 (impl $imp:ident, $($method:ident),+) => {
181 cmp_impl!(impl $imp, $($method -> bool),+)
183 // return something other than a Ratio<T>
184 (impl $imp:ident, $($method:ident -> $res:ty),*) => {
185 impl<T: Mul<T,T> + $imp> $imp for Ratio<T> {
188 fn $method(&self, other: &Ratio<T>) -> $res {
189 (self.numer * other.denom). $method (&(self.denom*other.numer))
195 cmp_impl!(impl Eq, eq, ne)
196 cmp_impl!(impl Ord, lt, gt, le, ge)
197 cmp_impl!(impl TotalEq, )
198 cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering)
201 // a/b * c/d = (a*c)/(b*d)
202 impl<T: Clone + Integer + Ord>
203 Mul<Ratio<T>,Ratio<T>> for Ratio<T> {
205 fn mul(&self, rhs: &Ratio<T>) -> Ratio<T> {
206 Ratio::new(self.numer * rhs.numer, self.denom * rhs.denom)
210 // (a/b) / (c/d) = (a*d)/(b*c)
211 impl<T: Clone + Integer + Ord>
212 Div<Ratio<T>,Ratio<T>> for Ratio<T> {
214 fn div(&self, rhs: &Ratio<T>) -> Ratio<T> {
215 Ratio::new(self.numer * rhs.denom, self.denom * rhs.numer)
219 // Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern
220 macro_rules! arith_impl {
221 (impl $imp:ident, $method:ident) => {
222 impl<T: Clone + Integer + Ord>
223 $imp<Ratio<T>,Ratio<T>> for Ratio<T> {
225 fn $method(&self, rhs: &Ratio<T>) -> Ratio<T> {
226 Ratio::new((self.numer * rhs.denom).$method(&(self.denom * rhs.numer)),
227 self.denom * rhs.denom)
233 // a/b + c/d = (a*d + b*c)/(b*d
234 arith_impl!(impl Add, add)
236 // a/b - c/d = (a*d - b*c)/(b*d)
237 arith_impl!(impl Sub, sub)
239 // a/b % c/d = (a*d % b*c)/(b*d)
240 arith_impl!(impl Rem, rem)
242 impl<T: Clone + Integer + Ord>
243 Neg<Ratio<T>> for Ratio<T> {
245 fn neg(&self) -> Ratio<T> {
246 Ratio::new_raw(-self.numer, self.denom.clone())
251 impl<T: Clone + Integer + Ord>
254 fn zero() -> Ratio<T> {
255 Ratio::new_raw(Zero::zero(), One::one())
259 fn is_zero(&self) -> bool {
260 *self == Zero::zero()
264 impl<T: Clone + Integer + Ord>
267 fn one() -> Ratio<T> {
268 Ratio::new_raw(One::one(), One::one())
272 impl<T: Clone + Integer + Ord>
275 /* String conversions */
276 impl<T: fmt::Show> fmt::Show for Ratio<T> {
277 /// Renders as `numer/denom`.
278 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
279 write!(f.buf, "{}/{}", self.numer, self.denom)
282 impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
283 /// Renders as `numer/denom` where the numbers are in base `radix`.
284 fn to_str_radix(&self, radix: uint) -> ~str {
285 format!("{}/{}", self.numer.to_str_radix(radix), self.denom.to_str_radix(radix))
289 impl<T: FromStr + Clone + Integer + Ord>
290 FromStr for Ratio<T> {
291 /// Parses `numer/denom`.
292 fn from_str(s: &str) -> Option<Ratio<T>> {
293 let split: Vec<&str> = s.splitn('/', 1).collect();
297 let a_option: Option<T> = FromStr::from_str(*split.get(0));
298 a_option.and_then(|a| {
299 let b_option: Option<T> = FromStr::from_str(*split.get(1));
300 b_option.and_then(|b| {
301 Some(Ratio::new(a.clone(), b.clone()))
306 impl<T: FromStrRadix + Clone + Integer + Ord>
307 FromStrRadix for Ratio<T> {
308 /// Parses `numer/denom` where the numbers are in base `radix`.
309 fn from_str_radix(s: &str, radix: uint) -> Option<Ratio<T>> {
310 let split: Vec<&str> = s.splitn('/', 1).collect();
314 let a_option: Option<T> = FromStrRadix::from_str_radix(
317 a_option.and_then(|a| {
318 let b_option: Option<T> =
319 FromStrRadix::from_str_radix(*split.get(1), radix);
320 b_option.and_then(|b| {
321 Some(Ratio::new(a.clone(), b.clone()))
331 use super::{Ratio, Rational, BigRational};
332 use std::num::{Zero, One, FromStrRadix, FromPrimitive, ToStrRadix};
333 use std::from_str::FromStr;
335 pub static _0 : Rational = Ratio { numer: 0, denom: 1};
336 pub static _1 : Rational = Ratio { numer: 1, denom: 1};
337 pub static _2: Rational = Ratio { numer: 2, denom: 1};
338 pub static _1_2: Rational = Ratio { numer: 1, denom: 2};
339 pub static _3_2: Rational = Ratio { numer: 3, denom: 2};
340 pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2};
342 pub fn to_big(n: Rational) -> BigRational {
344 FromPrimitive::from_int(n.numer).unwrap(),
345 FromPrimitive::from_int(n.denom).unwrap()
350 fn test_test_constants() {
351 // check our constants are what Ratio::new etc. would make.
352 assert_eq!(_0, Zero::zero());
353 assert_eq!(_1, One::one());
354 assert_eq!(_2, Ratio::from_integer(2));
355 assert_eq!(_1_2, Ratio::new(1,2));
356 assert_eq!(_3_2, Ratio::new(3,2));
357 assert_eq!(_neg1_2, Ratio::new(-1,2));
361 fn test_new_reduce() {
362 let one22 = Ratio::new(2i,2);
364 assert_eq!(one22, One::one());
369 let _a = Ratio::new(1,0);
375 assert!(_0 == _0 && _1 == _1);
376 assert!(_0 != _1 && _1 != _0);
377 assert!(_0 < _1 && !(_1 < _0));
378 assert!(_1 > _0 && !(_0 > _1));
380 assert!(_0 <= _0 && _1 <= _1);
381 assert!(_0 <= _1 && !(_1 <= _0));
383 assert!(_0 >= _0 && _1 >= _1);
384 assert!(_1 >= _0 && !(_0 >= _1));
389 fn test_to_integer() {
390 assert_eq!(_0.to_integer(), 0);
391 assert_eq!(_1.to_integer(), 1);
392 assert_eq!(_2.to_integer(), 2);
393 assert_eq!(_1_2.to_integer(), 0);
394 assert_eq!(_3_2.to_integer(), 1);
395 assert_eq!(_neg1_2.to_integer(), 0);
401 assert_eq!(_0.numer(), &0);
402 assert_eq!(_1.numer(), &1);
403 assert_eq!(_2.numer(), &2);
404 assert_eq!(_1_2.numer(), &1);
405 assert_eq!(_3_2.numer(), &3);
406 assert_eq!(_neg1_2.numer(), &(-1));
410 assert_eq!(_0.denom(), &1);
411 assert_eq!(_1.denom(), &1);
412 assert_eq!(_2.denom(), &1);
413 assert_eq!(_1_2.denom(), &2);
414 assert_eq!(_3_2.denom(), &2);
415 assert_eq!(_neg1_2.denom(), &2);
420 fn test_is_integer() {
421 assert!(_0.is_integer());
422 assert!(_1.is_integer());
423 assert!(_2.is_integer());
424 assert!(!_1_2.is_integer());
425 assert!(!_3_2.is_integer());
426 assert!(!_neg1_2.is_integer());
431 use super::{_0, _1, _2, _1_2, _3_2, _neg1_2, to_big};
432 use super::super::{Ratio, Rational};
436 fn test(a: Rational, b: Rational, c: Rational) {
437 assert_eq!(a + b, c);
438 assert_eq!(to_big(a) + to_big(b), to_big(c));
441 test(_1, _1_2, _3_2);
443 test(_1_2, _3_2, _2);
444 test(_1_2, _neg1_2, _0);
449 fn test(a: Rational, b: Rational, c: Rational) {
450 assert_eq!(a - b, c);
451 assert_eq!(to_big(a) - to_big(b), to_big(c))
454 test(_1, _1_2, _1_2);
455 test(_3_2, _1_2, _1);
456 test(_1, _neg1_2, _3_2);
461 fn test(a: Rational, b: Rational, c: Rational) {
462 assert_eq!(a * b, c);
463 assert_eq!(to_big(a) * to_big(b), to_big(c))
466 test(_1, _1_2, _1_2);
467 test(_1_2, _3_2, Ratio::new(3,4));
468 test(_1_2, _neg1_2, Ratio::new(-1, 4));
473 fn test(a: Rational, b: Rational, c: Rational) {
474 assert_eq!(a / b, c);
475 assert_eq!(to_big(a) / to_big(b), to_big(c))
479 test(_3_2, _1_2, _1 + _2);
480 test(_1, _neg1_2, _neg1_2 + _neg1_2 + _neg1_2 + _neg1_2);
485 fn test(a: Rational, b: Rational, c: Rational) {
486 assert_eq!(a % b, c);
487 assert_eq!(to_big(a) % to_big(b), to_big(c))
490 test(_3_2, _1, _1_2);
491 test(_2, _neg1_2, _0);
492 test(_1_2, _2, _1_2);
497 fn test(a: Rational, b: Rational) {
499 assert_eq!(-to_big(a), to_big(b))
508 assert_eq!(_0 + _0, _0);
509 assert_eq!(_0 * _0, _0);
510 assert_eq!(_0 * _1, _0);
511 assert_eq!(_0 / _neg1_2, _0);
512 assert_eq!(_0 - _0, _0);
523 assert_eq!(_1_2.ceil(), _1);
524 assert_eq!(_1_2.floor(), _0);
525 assert_eq!(_1_2.round(), _1);
526 assert_eq!(_1_2.trunc(), _0);
528 assert_eq!(_neg1_2.ceil(), _0);
529 assert_eq!(_neg1_2.floor(), -_1);
530 assert_eq!(_neg1_2.round(), -_1);
531 assert_eq!(_neg1_2.trunc(), _0);
533 assert_eq!(_1.ceil(), _1);
534 assert_eq!(_1.floor(), _1);
535 assert_eq!(_1.round(), _1);
536 assert_eq!(_1.trunc(), _1);
541 assert_eq!(_1.fract(), _0);
542 assert_eq!(_neg1_2.fract(), _neg1_2);
543 assert_eq!(_1_2.fract(), _1_2);
544 assert_eq!(_3_2.fract(), _1_2);
549 assert_eq!(_1 * _1.recip(), _1);
550 assert_eq!(_2 * _2.recip(), _1);
551 assert_eq!(_1_2 * _1_2.recip(), _1);
552 assert_eq!(_3_2 * _3_2.recip(), _1);
553 assert_eq!(_neg1_2 * _neg1_2.recip(), _1);
557 fn test_to_from_str() {
558 fn test(r: Rational, s: StrBuf) {
559 assert_eq!(FromStr::from_str(s.as_slice()), Some(r));
560 assert_eq!(r.to_str().to_strbuf(), s);
562 test(_1, "1/1".to_strbuf());
563 test(_0, "0/1".to_strbuf());
564 test(_1_2, "1/2".to_strbuf());
565 test(_3_2, "3/2".to_strbuf());
566 test(_2, "2/1".to_strbuf());
567 test(_neg1_2, "-1/2".to_strbuf());
570 fn test_from_str_fail() {
572 let rational: Option<Rational> = FromStr::from_str(s);
573 assert_eq!(rational, None);
576 let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1"];
577 for &s in xs.iter() {
583 fn test_to_from_str_radix() {
584 fn test(r: Rational, s: StrBuf, n: uint) {
585 assert_eq!(FromStrRadix::from_str_radix(s.to_owned(), n),
587 assert_eq!(r.to_str_radix(n).to_strbuf(), s);
589 fn test3(r: Rational, s: StrBuf) { test(r, s, 3) }
590 fn test16(r: Rational, s: StrBuf) { test(r, s, 16) }
592 test3(_1, "1/1".to_strbuf());
593 test3(_0, "0/1".to_strbuf());
594 test3(_1_2, "1/2".to_strbuf());
595 test3(_3_2, "10/2".to_strbuf());
596 test3(_2, "2/1".to_strbuf());
597 test3(_neg1_2, "-1/2".to_strbuf());
598 test3(_neg1_2 / _2, "-1/11".to_strbuf());
600 test16(_1, "1/1".to_strbuf());
601 test16(_0, "0/1".to_strbuf());
602 test16(_1_2, "1/2".to_strbuf());
603 test16(_3_2, "3/2".to_strbuf());
604 test16(_2, "2/1".to_strbuf());
605 test16(_neg1_2, "-1/2".to_strbuf());
606 test16(_neg1_2 / _2, "-1/4".to_strbuf());
607 test16(Ratio::new(13,15), "d/f".to_strbuf());
608 test16(_1_2*_1_2*_1_2*_1_2, "1/10".to_strbuf());
612 fn test_from_str_radix_fail() {
614 let radix: Option<Rational> = FromStrRadix::from_str_radix(s, 3);
615 assert_eq!(radix, None);
618 let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1", "3/2"];
619 for &s in xs.iter() {
625 fn test_from_float() {
626 fn test<T: Float>(given: T, (numer, denom): (&str, &str)) {
627 let ratio: BigRational = Ratio::from_float(given).unwrap();
628 assert_eq!(ratio, Ratio::new(
629 FromStr::from_str(numer).unwrap(),
630 FromStr::from_str(denom).unwrap()));
634 test(3.14159265359f32, ("13176795", "4194304"));
635 test(2f32.powf(100.), ("1267650600228229401496703205376", "1"));
636 test(-2f32.powf(100.), ("-1267650600228229401496703205376", "1"));
637 test(1.0 / 2f32.powf(100.), ("1", "1267650600228229401496703205376"));
638 test(684729.48391f32, ("1369459", "2"));
639 test(-8573.5918555f32, ("-4389679", "512"));
642 test(3.14159265359f64, ("3537118876014453", "1125899906842624"));
643 test(2f64.powf(100.), ("1267650600228229401496703205376", "1"));
644 test(-2f64.powf(100.), ("-1267650600228229401496703205376", "1"));
645 test(684729.48391f64, ("367611342500051", "536870912"));
646 test(-8573.5918555, ("-4713381968463931", "549755813888"));
647 test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376"));
651 fn test_from_float_fail() {
654 assert_eq!(Ratio::from_float(f32::NAN), None);
655 assert_eq!(Ratio::from_float(f32::INFINITY), None);
656 assert_eq!(Ratio::from_float(f32::NEG_INFINITY), None);
657 assert_eq!(Ratio::from_float(f64::NAN), None);
658 assert_eq!(Ratio::from_float(f64::INFINITY), None);
659 assert_eq!(Ratio::from_float(f64::NEG_INFINITY), None);