maryada 0.1.2

No-std binary64 real interval arithmetic conforming to IEEE 1788.1
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use maryada::{
    DecoratedInterval, Decoration, Interval, Signal, SignalFlags, TextError, decoration_part,
    interval_to_text, set_dec, subset,
};

#[test]
fn required_text_literal_forms_are_accepted() {
    let mut signals = SignalFlags::NONE;

    assert_eq!(
        Interval::text_to_interval("[1, 2]", &mut signals).bounds(),
        (1.0, 2.0)
    );
    assert_eq!(
        Interval::text_to_interval("[0x1p-1]", &mut signals).bounds(),
        (0.5, 0.5)
    );
    assert_eq!(
        Interval::text_to_interval("[+1.25E+2]", &mut signals).bounds(),
        (125.0, 125.0)
    );
    assert_eq!(
        Interval::text_to_interval("[0X1.Ap+2]", &mut signals).bounds(),
        (6.5, 6.5)
    );
    assert!(Interval::text_to_interval("[-INFINITY,+Inf]", &mut signals).is_entire());
    assert!(Interval::text_to_interval("[,]", &mut signals).is_entire());
    assert_eq!(
        Interval::text_to_interval("[,2]", &mut signals).bounds(),
        (f64::NEG_INFINITY, 2.0)
    );
    assert_eq!(
        Interval::text_to_interval("[1,]", &mut signals).bounds(),
        (1.0, f64::INFINITY)
    );
    assert!(Interval::text_to_interval("[ ]", &mut signals).is_empty());
    assert!(Interval::text_to_interval("[empty]", &mut signals).is_empty());
    assert!(Interval::text_to_interval("[EnTiRe]", &mut signals).is_entire());

    let rational = Interval::text_to_interval("[1/3, 2/3]", &mut signals);
    assert_eq!(
        rational.bounds(),
        (
            f64::from_bits(0x3fd5_5555_5555_5555),
            f64::from_bits(0x3fe5_5555_5555_5556),
        )
    );

    assert_eq!(
        Interval::text_to_interval("[-1/3]", &mut signals).bounds(),
        (
            f64::from_bits(0xbfd5_5555_5555_5556),
            f64::from_bits(0xbfd5_5555_5555_5555),
        )
    );

    let uncertain = Interval::text_to_interval("1.0?", &mut signals);
    assert!(uncertain.contains(1.0));
    assert!(uncertain.inf() < uncertain.sup());

    let decorated = DecoratedInterval::text_to_interval("[1,2]_COM", &mut signals);
    assert_eq!(decoration_part(decorated), Decoration::Com);
    assert!(DecoratedInterval::text_to_interval("[NAI]", &mut signals).is_nai());
    assert!(signals.is_empty());
}

#[test]
fn uncertain_literals_cover_symmetric_directed_unbounded_and_scaled_forms() {
    let symmetric = Interval::text_to_interval("1.0?", &mut ());
    let symmetric_reference = Interval::text_to_interval("[19/20,21/20]", &mut ());
    assert!(subset(symmetric_reference, symmetric));

    let radius = Interval::text_to_interval("1.0?2", &mut ());
    let radius_reference = Interval::text_to_interval("[4/5,6/5]", &mut ());
    assert!(subset(radius_reference, radius));

    let down = Interval::text_to_interval("1.0?d", &mut ());
    assert_eq!(down.sup(), 1.0);
    assert!(down.inf() <= 0.95);

    let up = Interval::text_to_interval("1.0?2U", &mut ());
    assert_eq!(up.inf(), 1.0);
    assert!(up.sup() >= 1.2);

    assert_eq!(
        Interval::text_to_interval("1.0??d", &mut ()).bounds(),
        (f64::NEG_INFINITY, 1.0)
    );
    assert_eq!(
        Interval::text_to_interval("1.0??U", &mut ()).bounds(),
        (1.0, f64::INFINITY)
    );
    assert_eq!(
        Interval::text_to_interval("1.0?2uE3", &mut ()).bounds(),
        (1000.0, 1200.0)
    );
}

#[test]
fn decorated_literals_enforce_permitted_combinations_and_default_decorations() {
    for (literal, expected) in [
        ("[1]_trv", Decoration::Trv),
        ("[1]_def", Decoration::Def),
        ("[1]_dac", Decoration::Dac),
        ("[1]_com", Decoration::Com),
    ] {
        let value = DecoratedInterval::text_to_interval(literal, &mut ());
        assert_eq!(decoration_part(value), expected);
    }

    assert_eq!(
        decoration_part(DecoratedInterval::text_to_interval("[1,2]", &mut ())),
        Decoration::Com
    );
    assert_eq!(
        decoration_part(DecoratedInterval::text_to_interval("[,]", &mut ())),
        Decoration::Dac
    );
    assert_eq!(
        decoration_part(DecoratedInterval::text_to_interval("[empty]", &mut ())),
        Decoration::Trv
    );

    let positive_overflow = DecoratedInterval::text_to_interval("[1e999]_com", &mut ());
    assert_eq!(positive_overflow.bounds(), (f64::MAX, f64::INFINITY));
    assert_eq!(decoration_part(positive_overflow), Decoration::Dac);

    let negative_overflow = DecoratedInterval::text_to_interval("[-1e999]_com", &mut ());
    assert_eq!(negative_overflow.bounds(), (f64::NEG_INFINITY, -f64::MAX));
    assert_eq!(decoration_part(negative_overflow), Decoration::Dac);

    let mut signals = SignalFlags::NONE;
    assert!(DecoratedInterval::text_to_interval("[empty]_com", &mut signals).is_nai());
    assert!(signals.contains(Signal::UndefinedOperation));
    signals.clear();
    assert!(DecoratedInterval::text_to_interval("[,]_com", &mut signals).is_nai());
    assert!(signals.contains(Signal::UndefinedOperation));
}

#[test]
fn invalid_text_signals_and_output_round_trips() {
    let mut signals = SignalFlags::NONE;
    for literal in [
        "not an interval",
        "[.]",
        "[1e]",
        "[0x1]",
        "[0x.p1]",
        "[1/0]",
        "[1/-2]",
        "[+inf,0]",
        "[0,-inf]",
        "[nai]",
        "[2,1]",
        "[1 2]",
        "[1,2,3]",
        "[1;2]",
        "[1,2] trailing",
        "1e2?",
        "1.0 ?",
        "1.0?-1",
        "1.0?x",
        "1.0?2du",
        "1.0?2e",
    ] {
        assert!(
            Interval::text_to_interval(literal, &mut signals).is_empty(),
            "bare constructor accepted {literal:?}"
        );
        assert!(signals.contains(Signal::UndefinedOperation));
        signals.clear();
    }

    for literal in [
        "[1]_ill",
        "[nai]_trv",
        "[1]_com_extra",
        "[empty]_def",
        "[empty]_dac",
        "[empty]_com",
        "[entire]_com",
    ] {
        assert!(
            DecoratedInterval::text_to_interval(literal, &mut signals).is_nai(),
            "decorated constructor accepted {literal:?}"
        );
        assert!(signals.contains(Signal::UndefinedOperation));
        signals.clear();
    }

    let relaxed = Interval::text_to_interval("[0.1,0x1p0]", &mut signals);
    assert!(relaxed.inf() <= 0.1 && relaxed.sup() >= 1.0);
    assert!(signals.is_empty());

    for literal in ["[1.0,0x0p0]", "[2/3,1/3]"] {
        assert!(Interval::text_to_interval(literal, &mut signals).is_empty());
        assert!(signals.contains(Signal::UndefinedOperation));
        signals.clear();
    }

    let value = Interval::new(-1.25, 3.5);
    let mut output = [0; 64];
    let length = interval_to_text(value, None, &mut output).unwrap();
    let text = core::str::from_utf8(&output[..length]).unwrap();
    assert_eq!(Interval::text_to_interval(text, &mut ()), value);

    for value in [Interval::EMPTY, Interval::ENTIRE, Interval::ZERO] {
        let length = interval_to_text(value, Some("invalid"), &mut output).unwrap();
        let text = core::str::from_utf8(&output[..length]).unwrap();
        assert_eq!(Interval::text_to_interval(text, &mut ()), value);
    }

    let length = interval_to_text(DecoratedInterval::NAI, Some("hex"), &mut output).unwrap();
    assert_eq!(&output[..length], b"[nai]");
}

#[test]
fn interchange_encoding_and_validation_match_the_standard_representation() {
    let value = set_dec(Interval::new(-1.0, 3.0), Decoration::Com);
    let expected_be = [
        0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -1
        0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 3
        0x10, // com
    ];
    let expected_le = [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xbf, // -1
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, // 3
        0x10, // com
    ];

    assert_eq!(value.to_be_bytes(), expected_be);
    assert_eq!(value.to_le_bytes(), expected_le);
    assert_eq!(
        DecoratedInterval::from_be_bytes(&expected_be, &mut ()),
        value
    );
    assert_eq!(
        DecoratedInterval::from_le_bytes(&expected_le, &mut ()),
        value
    );

    for (interval, expected) in [
        (
            Interval::EMPTY,
            [
                0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +inf
                0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -inf
            ],
        ),
        (
            Interval::ENTIRE,
            [
                0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -inf
                0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +inf
            ],
        ),
        (
            Interval::ZERO,
            [
                0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -0
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +0
            ],
        ),
    ] {
        assert_eq!(interval.to_be_bytes(), expected);
        assert_eq!(Interval::from_be_bytes(&expected, &mut ()), interval);

        let mut little_endian = expected;
        little_endian[..8].reverse();
        little_endian[8..].reverse();
        assert_eq!(interval.to_le_bytes(), little_endian);
        assert_eq!(Interval::from_le_bytes(&little_endian, &mut ()), interval);
    }

    for (decoration, octet) in [
        (Decoration::Trv, 0x04),
        (Decoration::Def, 0x08),
        (Decoration::Dac, 0x0c),
        (Decoration::Com, 0x10),
    ] {
        let decorated = set_dec(Interval::new(-1.0, 3.0), decoration);
        let encoded = decorated.to_be_bytes();
        assert_eq!(encoded[16], octet);
        assert_eq!(
            DecoratedInterval::from_be_bytes(&encoded, &mut ()),
            decorated
        );
    }

    let nai = DecoratedInterval::NAI.to_be_bytes();
    assert_eq!(nai[..8], 0x7ff8_0000_0000_0000_u64.to_be_bytes());
    assert_eq!(nai[8..16], 0x7ff8_0000_0000_0000_u64.to_be_bytes());
    assert_eq!(nai[16], 0x00);
    assert!(DecoratedInterval::from_be_bytes(&nai, &mut ()).is_nai());

    let invalid_bare = [
        [
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +0 lower
            0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1
        ],
        [
            0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -1
            0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -0 upper
        ],
        [
            0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 2
            0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1
        ],
        [
            0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // NaN
            0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1
        ],
    ];
    let mut signals = SignalFlags::NONE;
    for encoded in invalid_bare {
        assert!(Interval::from_be_bytes(&encoded, &mut signals).is_empty());
        assert!(signals.contains(Signal::InvalidOperand));
        signals.clear();
    }
    assert!(Interval::from_be_bytes(&[0; 15], &mut signals).is_empty());
    assert!(signals.contains(Signal::InvalidOperand));

    let invalid_decorated = [
        [
            0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +inf
            0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -inf
            0x10, // Empty_com
        ],
        [
            0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -inf
            0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +inf
            0x10, // Entire_com
        ],
        [
            0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -1
            0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 3
            0x00, // ill with numeric bounds
        ],
        [
            0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // NaN
            0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // NaN
            0x04, // trv with NaN bounds
        ],
        [
            0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // -1
            0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 3
            0x01, // invalid decoration octet
        ],
    ];
    signals.clear();
    for encoded in invalid_decorated {
        assert!(DecoratedInterval::from_be_bytes(&encoded, &mut signals).is_nai());
        assert!(signals.contains(Signal::InvalidOperand));
        signals.clear();
    }
    assert!(DecoratedInterval::from_be_bytes(&[0; 16], &mut signals).is_nai());
    assert!(signals.contains(Signal::InvalidOperand));
}

#[test]
fn uncertain_literals_cover_long_input_and_checked_arithmetic_boundaries() {
    let long_center =
        Interval::text_to_interval("1234567890123456789012345678901234567890?1", &mut ());
    assert!(long_center.is_bounded());
    assert!(long_center.contains(1.2345678901234568e39));

    let doubled_center_fallback =
        Interval::text_to_interval("170141183460469231731687303715884105727?1", &mut ());
    assert!(doubled_center_fallback.is_bounded());

    for literal in [
        "170141183460469231731687303715884105727??",
        "170141183460469231731687303715884105727??d",
        "170141183460469231731687303715884105727??U",
    ] {
        let value = Interval::text_to_interval(literal, &mut ());
        assert!(!value.is_empty(), "fallback rejected {literal:?}");
    }

    for literal in [
        "1?170141183460469231731687303715884105727",
        "85070591730234615865843651857942052863?85070591730234615865843651857942052863",
    ] {
        assert!(
            Interval::text_to_interval(literal, &mut ()).is_entire(),
            "boundary did not conservatively widen {literal:?}",
        );
    }

    let huge_positive =
        Interval::text_to_interval("1?1e999999999999999999999999999999999999999", &mut ());
    assert!(!huge_positive.is_empty());
    assert_eq!(huge_positive.sup(), f64::INFINITY);
    let huge_negative =
        Interval::text_to_interval("1?1e-999999999999999999999999999999999999999", &mut ());
    assert!(!huge_negative.is_empty());
    assert!(huge_negative.contains(0.0));

    let mut signals = SignalFlags::NONE;
    for literal in [
        "1?9999999999999999999999999999999999999999",
        "1234567890123456789012345678901234567890.0.0?1",
        "1?1e+",
        "1?1e-",
        "1?1e-not-a-number",
    ] {
        assert!(
            Interval::text_to_interval(literal, &mut signals).is_empty(),
            "accepted malformed uncertain literal {literal:?}",
        );
        assert!(
            signals.contains(Signal::UndefinedOperation),
            "missing signal for {literal:?}"
        );
        signals.clear();
    }

    for literal in ["[inf]", "[-inf]", "[+infinity]", "[-INFINITY]"] {
        assert!(Interval::text_to_interval(literal, &mut signals).is_empty());
        assert!(signals.contains(Signal::UndefinedOperation));
        signals.clear();
    }
}

#[test]
fn hexadecimal_output_handles_binary64_edges_decorations_and_small_buffers() {
    let values = [
        Interval::ZERO,
        Interval::new(f64::from_bits(1), f64::from_bits(1)),
        Interval::from(1.625),
        Interval::from(-1.625),
        Interval::from(f64::MAX),
        Interval::new(f64::NEG_INFINITY, -1.0),
        Interval::new(1.0, f64::INFINITY),
        Interval::EMPTY,
        Interval::ENTIRE,
    ];

    let mut output = [0; 128];
    for value in values {
        let length = interval_to_text(value, Some("hex"), &mut output).unwrap();
        let text = core::str::from_utf8(&output[..length]).unwrap();
        assert_eq!(Interval::text_to_interval(text, &mut ()), value, "{text}");
    }

    let length = interval_to_text(Interval::ZERO, None, &mut output).unwrap();
    assert_eq!(&output[..length], b"[-0x0p+0,0x0p+0]");
    let length = interval_to_text(Interval::from(f64::from_bits(1)), None, &mut output).unwrap();
    assert_eq!(&output[..length], b"[0x1p-1074,0x1p-1074]");
    let length = interval_to_text(Interval::from(1.625), None, &mut output).unwrap();
    assert_eq!(&output[..length], b"[0x1.ap+0,0x1.ap+0]");

    for decoration in [
        Decoration::Trv,
        Decoration::Def,
        Decoration::Dac,
        Decoration::Com,
    ] {
        let value = set_dec(Interval::new(-1.625, 2.5), decoration);
        let length = interval_to_text(value, Some("hex"), &mut output).unwrap();
        let text = core::str::from_utf8(&output[..length]).unwrap();
        let parsed = DecoratedInterval::text_to_interval(text, &mut ());
        assert_eq!(parsed, value, "{text}");
        assert_eq!(decoration_part(parsed), decoration);
    }

    let ill_length = interval_to_text(DecoratedInterval::NAI, None, &mut output).unwrap();
    assert_eq!(&output[..ill_length], b"[nai]");

    for (value, capacity) in [
        (Interval::EMPTY, 0usize),
        (Interval::ENTIRE, 4usize),
        (Interval::from(1.625), 8usize),
    ] {
        let error = interval_to_text(value, None, &mut output[..capacity]).unwrap_err();
        assert!(matches!(error, TextError::BufferTooSmall { required } if required > capacity));
    }
}