Skip to content

Commit 298b6de

Browse files
committed
Update readme
1 parent 6140db3 commit 298b6de

File tree

3 files changed

+104
-5
lines changed

3 files changed

+104
-5
lines changed

src/etc/test-float-parse/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ Some tests generate strings, others generate bit patterns. For those that
1717
generate bit patterns, we need to use float -> dec conversions so that also
1818
gets tested.
1919

20-
TODO
20+
todo

src/etc/test-float-parse/src/validate.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ pub struct Constants {
3838
neg_inf_cutoff: BigRational,
3939
/// The powers of two for all relevant integers.
4040
powers_of_two: BTreeMap<i32, BigRational>,
41-
/// Half of each power of two. ULP = "unit in last position", so these are one bit
42-
/// more precise than what is representable by the float (i.e. in between representable
43-
/// values).
41+
/// Half of each power of two. ULP = "unit in last position".
42+
///
43+
/// This is a mapping from integers to half of the precision that can be had at that
44+
/// exponent. That is, these values are in between values that can be represented with a
45+
/// certain exponent, assuming an integer significand (always true for float representations).
4446
half_ulp: BTreeMap<i32, BigRational>,
4547
/// Handy to have around so we don't need to reallocate for it
4648
two: BigInt,
@@ -187,7 +189,7 @@ impl<F: Float> FloatRes<F> {
187189
}
188190

189191
let one_ulp = consts.half_ulp.get(&(exp + 1)).unwrap();
190-
assert_eq!(one_ulp, &(half_ulp * &consts.two));
192+
assert_eq!(one_ulp, &(half_ulp * &consts.two), "ULP values are incorrect");
191193

192194
let relative_error = error / one_ulp;
193195

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use num::ToPrimitive;
2+
3+
use super::*;
4+
5+
#[test]
6+
fn test_parse_rational() {
7+
assert_eq!(Rational::parse("1234").expect_finite(), BigRational::new(1234.into(), 1.into()));
8+
assert_eq!(
9+
Rational::parse("-1234").expect_finite(),
10+
BigRational::new((-1234).into(), 1.into())
11+
);
12+
assert_eq!(Rational::parse("1e+6").expect_finite(), BigRational::new(1000000.into(), 1.into()));
13+
assert_eq!(Rational::parse("1e-6").expect_finite(), BigRational::new(1.into(), 1000000.into()));
14+
assert_eq!(
15+
Rational::parse("10.4e6").expect_finite(),
16+
BigRational::new(10400000.into(), 1.into())
17+
);
18+
assert_eq!(
19+
Rational::parse("10.4e+6").expect_finite(),
20+
BigRational::new(10400000.into(), 1.into())
21+
);
22+
assert_eq!(
23+
Rational::parse("10.4e-6").expect_finite(),
24+
BigRational::new(13.into(), 1250000.into())
25+
);
26+
assert_eq!(
27+
Rational::parse("10.4243566462342456234124").expect_finite(),
28+
BigRational::new(104243566462342456234124_i128.into(), 10000000000000000000000_i128.into())
29+
);
30+
assert_eq!(Rational::parse("inf"), Rational::Inf);
31+
assert_eq!(Rational::parse("+inf"), Rational::Inf);
32+
assert_eq!(Rational::parse("-inf"), Rational::NegInf);
33+
assert_eq!(Rational::parse("NaN"), Rational::Nan);
34+
}
35+
36+
#[test]
37+
fn test_decode() {
38+
assert_eq!(decode(0f32, false), FloatRes::Zero);
39+
assert_eq!(decode(f32::INFINITY, false), FloatRes::Inf);
40+
assert_eq!(decode(f32::NEG_INFINITY, false), FloatRes::NegInf);
41+
assert_eq!(decode(1.0f32, false).normalize(), FloatRes::Real { sig: 1, exp: 0 });
42+
assert_eq!(decode(-1.0f32, false).normalize(), FloatRes::Real { sig: -1, exp: 0 });
43+
assert_eq!(decode(100.0f32, false).normalize(), FloatRes::Real { sig: 100, exp: 0 });
44+
assert_eq!(decode(100.5f32, false).normalize(), FloatRes::Real { sig: 201, exp: -1 });
45+
assert_eq!(decode(-4.004f32, false).normalize(), FloatRes::Real { sig: -8396997, exp: -21 });
46+
assert_eq!(decode(0.0004f32, false).normalize(), FloatRes::Real { sig: 13743895, exp: -35 });
47+
assert_eq!(
48+
decode(f32::from_bits(0x1), false).normalize(),
49+
FloatRes::Real { sig: 1, exp: -149 }
50+
);
51+
}
52+
53+
#[test]
54+
fn test_validate() {
55+
validate::<f32>("0", false).unwrap();
56+
validate::<f32>("-0", false).unwrap();
57+
validate::<f32>("1", false).unwrap();
58+
validate::<f32>("-1", false).unwrap();
59+
validate::<f32>("1.1", false).unwrap();
60+
validate::<f32>("-1.1", false).unwrap();
61+
validate::<f32>("1e10", false).unwrap();
62+
validate::<f32>("1e1000", false).unwrap();
63+
validate::<f32>("-1e1000", false).unwrap();
64+
validate::<f32>("1e-1000", false).unwrap();
65+
validate::<f32>("-1e-1000", false).unwrap();
66+
}
67+
68+
#[test]
69+
fn test_check() {
70+
// Most of the arbitrary values come from checking against <http://weitz.de/ieee/>.
71+
let r = &BigRational::from_float(10.0).unwrap();
72+
FloatRes::<f32>::validate_real(r.clone(), 10, 0).unwrap();
73+
FloatRes::<f32>::validate_real(r.clone(), 10, -1).unwrap_err();
74+
FloatRes::<f32>::validate_real(r.clone(), 10, 1).unwrap_err();
75+
76+
let r = &BigRational::from_float(0.25).unwrap();
77+
FloatRes::<f32>::validate_real(r.clone(), 1, -2).unwrap();
78+
FloatRes::<f32>::validate_real(r.clone(), 2, -2).unwrap_err();
79+
80+
let r = &BigRational::from_float(1234.5678).unwrap();
81+
FloatRes::<f32>::validate_real(r.clone(), 0b100110100101001000101011, -13).unwrap();
82+
FloatRes::<f32>::validate_real(r.clone(), 0b100110100101001000101010, -13).unwrap_err();
83+
FloatRes::<f32>::validate_real(r.clone(), 0b100110100101001000101100, -13).unwrap_err();
84+
85+
let r = &BigRational::from_float(-1234.5678).unwrap();
86+
FloatRes::<f32>::validate_real(r.clone(), -0b100110100101001000101011, -13).unwrap();
87+
FloatRes::<f32>::validate_real(r.clone(), -0b100110100101001000101010, -13).unwrap_err();
88+
FloatRes::<f32>::validate_real(r.clone(), -0b100110100101001000101100, -13).unwrap_err();
89+
}
90+
91+
#[test]
92+
fn check_constants() {
93+
assert_eq!(f32::constants().max.to_f32().unwrap(), f32::MAX);
94+
assert_eq!(f32::constants().min_subnormal.to_f32().unwrap(), f32::from_bits(0x1));
95+
assert_eq!(f64::constants().max.to_f64().unwrap(), f64::MAX);
96+
assert_eq!(f64::constants().min_subnormal.to_f64().unwrap(), f64::from_bits(0x1));
97+
}

0 commit comments

Comments
 (0)