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.
16 use num::{Float, FpCategory};
18 /// Decoded unsigned finite value, such that:
20 /// - The original value equals to `mant * 2^exp`.
22 /// - Any number from `(mant - minus) * 2^exp` to `(mant + plus) * 2^exp` will
23 /// round to the original value. The range is inclusive only when
24 /// `inclusive` is true.
25 #[derive(Copy, Clone, Debug, PartialEq)]
27 /// The scaled mantissa.
29 /// The lower error range.
31 /// The upper error range.
33 /// The shared exponent in base 2.
35 /// True when the error range is inclusive.
37 /// In IEEE 754, this is true when the original mantissa was even.
41 /// Decoded unsigned value.
42 #[derive(Copy, Clone, Debug, PartialEq)]
43 pub enum FullDecoded {
46 /// Infinities, either positive or negative.
48 /// Zero, either positive or negative.
50 /// Finite numbers with further decoded fields.
54 /// A floating point type which can be `decode`d.
55 pub trait DecodableFloat: Float + Copy {
56 /// The minimum positive normalized value.
57 fn min_pos_norm_value() -> Self;
60 impl DecodableFloat for f32 {
61 fn min_pos_norm_value() -> Self { f32::MIN_POSITIVE }
64 impl DecodableFloat for f64 {
65 fn min_pos_norm_value() -> Self { f64::MIN_POSITIVE }
68 /// Returns a sign (true when negative) and `FullDecoded` value
69 /// from given floating point number.
70 pub fn decode<T: DecodableFloat>(v: T) -> (/*negative?*/ bool, FullDecoded) {
71 let (mant, exp, sign) = v.integer_decode();
72 let even = (mant & 1) == 0;
73 let decoded = match v.classify() {
74 FpCategory::Nan => FullDecoded::Nan,
75 FpCategory::Infinite => FullDecoded::Infinite,
76 FpCategory::Zero => FullDecoded::Zero,
77 FpCategory::Subnormal => {
78 // neighbors: (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
79 // Float::integer_decode always preserves the exponent,
80 // so the mantissa is scaled for subnormals.
81 FullDecoded::Finite(Decoded { mant: mant, minus: 1, plus: 1,
82 exp: exp, inclusive: even })
84 FpCategory::Normal => {
85 let minnorm = <T as DecodableFloat>::min_pos_norm_value().integer_decode();
86 if mant == minnorm.0 {
87 // neighbors: (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
88 // where maxmant = minnormmant * 2 - 1
89 FullDecoded::Finite(Decoded { mant: mant << 2, minus: 1, plus: 2,
90 exp: exp - 2, inclusive: even })
92 // neighbors: (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
93 FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 1,
94 exp: exp - 1, inclusive: even })