1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#![allow(clippy::approx_constant)]

use crate::{const_interval, interval::*};
use hexf::*;

impl Interval {
    pub const EMPTY: Self = {
        #[repr(C)]
        union Rep {
            f: [f64; 2],
            i: Interval,
        }

        unsafe {
            Rep {
                f: [f64::NAN, f64::NAN],
            }
            .i
        }
    };

    pub const ENTIRE: Self = const_interval!(f64::NEG_INFINITY, f64::INFINITY);

    pub const E: Self = const_interval!(
        hexf64!("0x2.b7e151628aed2p0"),
        hexf64!("0x2.b7e151628aed4p0")
    );
    pub const FRAC_1_PI: Self = const_interval!(
        hexf64!("0x5.17cc1b7272208p-4"),
        hexf64!("0x5.17cc1b727220cp-4")
    );
    pub const FRAC_1_SQRT_2: Self = const_interval!(
        hexf64!("0xb.504f333f9de60p-4"),
        hexf64!("0xb.504f333f9de68p-4")
    );
    pub const FRAC_2_PI: Self = const_interval!(
        hexf64!("0xa.2f9836e4e4410p-4"),
        hexf64!("0xa.2f9836e4e4418p-4")
    );
    pub const FRAC_2_SQRT_PI: Self = const_interval!(
        hexf64!("0x1.20dd750429b6dp0"),
        hexf64!("0x1.20dd750429b6ep0")
    );
    pub const FRAC_PI_2: Self = const_interval!(
        hexf64!("0x1.921fb54442d18p0"),
        hexf64!("0x1.921fb54442d19p0")
    );
    pub const FRAC_PI_3: Self = const_interval!(
        hexf64!("0x1.0c152382d7365p0"),
        hexf64!("0x1.0c152382d7366p0")
    );
    pub const FRAC_PI_4: Self = const_interval!(
        hexf64!("0xc.90fdaa22168c0p-4"),
        hexf64!("0xc.90fdaa22168c8p-4")
    );
    pub const FRAC_PI_6: Self = const_interval!(
        hexf64!("0x8.60a91c16b9b28p-4"),
        hexf64!("0x8.60a91c16b9b30p-4")
    );
    pub const FRAC_PI_8: Self = const_interval!(
        hexf64!("0x6.487ed5110b460p-4"),
        hexf64!("0x6.487ed5110b464p-4")
    );
    pub const LN_10: Self = const_interval!(
        hexf64!("0x2.4d763776aaa2ap0"),
        hexf64!("0x2.4d763776aaa2cp0")
    );
    pub const LN_2: Self = const_interval!(
        hexf64!("0xb.17217f7d1cf78p-4"),
        hexf64!("0xb.17217f7d1cf80p-4")
    );
    pub const LOG10_2: Self = const_interval!(
        hexf64!("0x4.d104d427de7f8p-4"),
        hexf64!("0x4.d104d427de7fcp-4")
    );
    pub const LOG10_E: Self = const_interval!(
        hexf64!("0x6.f2dec549b9438p-4"),
        hexf64!("0x6.f2dec549b943cp-4")
    );
    pub const LOG2_10: Self = const_interval!(
        hexf64!("0x3.5269e12f346e2p0"),
        hexf64!("0x3.5269e12f346e4p0")
    );
    pub const LOG2_E: Self = const_interval!(
        hexf64!("0x1.71547652b82fep0"),
        hexf64!("0x1.71547652b82ffp0")
    );
    pub const PI: Self = const_interval!(
        hexf64!("0x3.243f6a8885a30p0"),
        hexf64!("0x3.243f6a8885a32p0")
    );
    pub const SQRT_2: Self = const_interval!(
        hexf64!("0x1.6a09e667f3bccp0"),
        hexf64!("0x1.6a09e667f3bcdp0")
    );
    pub const TAU: Self = const_interval!(
        hexf64!("0x6.487ed5110b460p0"),
        hexf64!("0x6.487ed5110b464p0")
    );
}

macro_rules! def_com {
    ($c:ident) => {
        pub const $c: Self = Self::new_unchecked(Interval::$c, Decoration::Com);
    };
}

impl DecoratedInterval {
    pub const EMPTY: Self = Self::new_unchecked(Interval::EMPTY, Decoration::Trv);
    pub const ENTIRE: Self = Self::new_unchecked(Interval::ENTIRE, Decoration::Dac);
    pub const NAI: Self = Self::new_unchecked(Interval::EMPTY, Decoration::Ill);

    def_com!(E);
    def_com!(FRAC_1_PI);
    def_com!(FRAC_1_SQRT_2);
    def_com!(FRAC_2_PI);
    def_com!(FRAC_2_SQRT_PI);
    def_com!(FRAC_PI_2);
    def_com!(FRAC_PI_3);
    def_com!(FRAC_PI_4);
    def_com!(FRAC_PI_6);
    def_com!(FRAC_PI_8);
    def_com!(LN_10);
    def_com!(LN_2);
    def_com!(LOG10_2);
    def_com!(LOG10_E);
    def_com!(LOG2_10);
    def_com!(LOG2_E);
    def_com!(PI);
    def_com!(SQRT_2);
    def_com!(TAU);
}

#[cfg(test)]
mod tests {
    use super::*;
    type I = Interval;
    type DI = DecoratedInterval;

    // This only works with positive numbers.
    fn succ(x: f64) -> f64 {
        f64::from_bits(x.to_bits() + 1)
    }

    macro_rules! check {
        ($c:ident) => {
            check!($c, std::f64::consts::$c);
        };

        ($c:ident, $f:expr) => {
            assert!({
                let a = I::$c.inf();
                let b = I::$c.sup();
                b == succ(a) && (a == $f || $f == b)
            });
            assert_eq!(DI::$c, DI::new(I::$c));
        };
    }

    #[test]
    fn constants() {
        check!(E);
        check!(FRAC_1_PI);
        check!(FRAC_1_SQRT_2);
        check!(FRAC_2_PI);
        check!(FRAC_2_SQRT_PI);
        check!(FRAC_PI_2);
        check!(FRAC_PI_3);
        check!(FRAC_PI_4);
        check!(FRAC_PI_6);
        check!(FRAC_PI_8);
        check!(LN_10);
        check!(LN_2);
        check!(LOG10_2);
        check!(LOG10_E);
        check!(LOG2_10);
        check!(LOG2_E);
        check!(PI);
        check!(SQRT_2);
        check!(TAU);
    }
}