1 // Copyright 2015 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.
11 //! Decodes a floating-point value into individual parts and error ranges.
17 use num::dec2flt::rawfp::RawFloat;
19 /// Decoded unsigned finite value, such that:
21 /// - The original value equals to `mant * 2^exp`.
23 /// - Any number from `(mant - minus) * 2^exp` to `(mant + plus) * 2^exp` will
24 /// round to the original value. The range is inclusive only when
25 /// `inclusive` is true.
26 #[derive(Copy, Clone, Debug, PartialEq)]
28 /// The scaled mantissa.
30 /// The lower error range.
32 /// The upper error range.
34 /// The shared exponent in base 2.
36 /// True when the error range is inclusive.
38 /// In IEEE 754, this is true when the original mantissa was even.
42 /// Decoded unsigned value.
43 #[derive(Copy, Clone, Debug, PartialEq)]
44 pub enum FullDecoded {
47 /// Infinities, either positive or negative.
49 /// Zero, either positive or negative.
51 /// Finite numbers with further decoded fields.
55 /// A floating point type which can be `decode`d.
56 pub trait DecodableFloat: RawFloat + Copy {
57 /// The minimum positive normalized value.
58 fn min_pos_norm_value() -> Self;
61 impl DecodableFloat for f32 {
62 fn min_pos_norm_value() -> Self { f32::MIN_POSITIVE }
65 impl DecodableFloat for f64 {
66 fn min_pos_norm_value() -> Self { f64::MIN_POSITIVE }
69 /// Returns a sign (true when negative) and `FullDecoded` value
70 /// from given floating point number.
71 pub fn decode<T: DecodableFloat>(v: T) -> (/*negative?*/ bool, FullDecoded) {
72 let (mant, exp, sign) = v.integer_decode2();
73 let even = (mant & 1) == 0;
74 let decoded = match v.classify() {
75 FpCategory::Nan => FullDecoded::Nan,
76 FpCategory::Infinite => FullDecoded::Infinite,
77 FpCategory::Zero => FullDecoded::Zero,
78 FpCategory::Subnormal => {
79 // neighbors: (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
80 // Float::integer_decode always preserves the exponent,
81 // so the mantissa is scaled for subnormals.
82 FullDecoded::Finite(Decoded { mant: mant, minus: 1, plus: 1,
83 exp: exp, inclusive: even })
85 FpCategory::Normal => {
86 let minnorm = <T as DecodableFloat>::min_pos_norm_value().integer_decode2();
87 if mant == minnorm.0 {
88 // neighbors: (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
89 // where maxmant = minnormmant * 2 - 1
90 FullDecoded::Finite(Decoded { mant: mant << 2, minus: 1, plus: 2,
91 exp: exp - 2, inclusive: even })
93 // neighbors: (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
94 FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 1,
95 exp: exp - 1, inclusive: even })