Skip to content

Commit 06a2f77

Browse files
committed
---
yaml --- r: 147786 b: refs/heads/try2 c: b88138a h: refs/heads/master v: v3
1 parent d9a2e58 commit 06a2f77

File tree

6 files changed

+61
-135
lines changed

6 files changed

+61
-135
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 705f472c5509ec3a8fe3448541282eca1386344a
8+
refs/heads/try2: b88138a3ec4bb142fa514a863b62f955bf1c44e3
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libextra/num/rational.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use std::cmp;
1515
use std::from_str::FromStr;
1616
use std::num::{Zero,One,ToStrRadix,FromStrRadix,Round};
17-
use super::bigint::BigInt;
17+
use super::bigint::{BigInt, BigUint, Sign, Plus, Minus};
1818

1919
/// Represents the ratio between 2 numbers.
2020
#[deriving(Clone)]
@@ -107,6 +107,27 @@ impl<T: Clone + Integer + Ord>
107107
}
108108
}
109109

110+
impl Ratio<BigInt> {
111+
/// Converts a float into a rational number
112+
pub fn from_float<T: Float>(f: T) -> Option<BigRational> {
113+
if !f.is_finite() {
114+
return None;
115+
}
116+
let (mantissa, exponent, sign) = f.integer_decode();
117+
let bigint_sign: Sign = if sign == 1 { Plus } else { Minus };
118+
if exponent < 0 {
119+
let one: BigInt = One::one();
120+
let denom: BigInt = one << ((-exponent) as uint);
121+
let numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap();
122+
Some(Ratio::new(BigInt::from_biguint(bigint_sign, numer), denom))
123+
} else {
124+
let mut numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap();
125+
numer = numer << (exponent as uint);
126+
Some(Ratio::from_integer(BigInt::from_biguint(bigint_sign, numer)))
127+
}
128+
}
129+
}
130+
110131
/* Comparisons */
111132

112133
// comparing a/b and c/d is the same as comparing a*d and b*c, so we
@@ -621,4 +642,42 @@ mod test {
621642
test(s);
622643
}
623644
}
645+
646+
#[test]
647+
fn test_from_float() {
648+
fn test<T: Float>(given: T, (numer, denom): (&str, &str)) {
649+
let ratio: BigRational = Ratio::from_float(given).unwrap();
650+
assert_eq!(ratio, Ratio::new(
651+
FromStr::from_str(numer).unwrap(),
652+
FromStr::from_str(denom).unwrap()));
653+
}
654+
655+
// f32
656+
test(3.14159265359f32, ("13176795", "4194304"));
657+
test(2f32.pow(&100.), ("1267650600228229401496703205376", "1"));
658+
test(-2f32.pow(&100.), ("-1267650600228229401496703205376", "1"));
659+
test(1.0 / 2f32.pow(&100.), ("1", "1267650600228229401496703205376"));
660+
test(684729.48391f32, ("1369459", "2"));
661+
test(-8573.5918555f32, ("-4389679", "512"));
662+
663+
// f64
664+
test(3.14159265359f64, ("3537118876014453", "1125899906842624"));
665+
test(2f64.pow(&100.), ("1267650600228229401496703205376", "1"));
666+
test(-2f64.pow(&100.), ("-1267650600228229401496703205376", "1"));
667+
test(684729.48391f64, ("367611342500051", "536870912"));
668+
test(-8573.5918555, ("-4713381968463931", "549755813888"));
669+
test(1.0 / 2f64.pow(&100.), ("1", "1267650600228229401496703205376"));
670+
}
671+
672+
#[test]
673+
fn test_from_float_fail() {
674+
use std::{f32, f64};
675+
676+
assert_eq!(Ratio::from_float(f32::NAN), None);
677+
assert_eq!(Ratio::from_float(f32::INFINITY), None);
678+
assert_eq!(Ratio::from_float(f32::NEG_INFINITY), None);
679+
assert_eq!(Ratio::from_float(f64::NAN), None);
680+
assert_eq!(Ratio::from_float(f64::INFINITY), None);
681+
assert_eq!(Ratio::from_float(f64::NEG_INFINITY), None);
682+
}
624683
}

branches/try2/src/librustc/back/link.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use back::archive::{Archive, METADATA_FILENAME};
1313
use back::rpath;
14-
use back::manifest;
1514
use driver::driver::CrateTranslation;
1615
use driver::session::Session;
1716
use driver::session;
@@ -828,12 +827,6 @@ fn link_binary_output(sess: Session,
828827
}
829828
session::OutputExecutable => {
830829
link_natively(sess, false, obj_filename, &out_filename);
831-
// Windows linker will add an ".exe" extension if there was none
832-
let out_filename = match out_filename.extension() {
833-
Some(_) => out_filename.clone(),
834-
None => out_filename.with_extension(win32::EXE_EXTENSION)
835-
};
836-
manifest::postprocess_executable(sess, &out_filename);
837830
}
838831
session::OutputDylib => {
839832
link_natively(sess, true, obj_filename, &out_filename);

branches/try2/src/librustc/back/manifest.rs

Lines changed: 0 additions & 106 deletions
This file was deleted.

branches/try2/src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ pub mod front {
8888
pub mod back {
8989
pub mod archive;
9090
pub mod link;
91-
pub mod manifest;
9291
pub mod abi;
9392
pub mod arm;
9493
pub mod mips;

branches/try2/src/test/run-pass/setup.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)