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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! Infinity for types without infinite values
//!
//! Infinitable introduces the notion of "infinity" and "negative infinity"
//! to numeric types, such as integers, that do not have infinite values.
//!
//! A representation of infinity is useful for graph algorithms such as
//! Dijkstra's algorithm, as well as for representing a graph with an
//! adjacency matrix.
//!
//! # Basic Usage
//!
//! ```
//! use infinitable::*;
//!
//! let finite = Finite(5);
//! let infinity = Infinity;
//! let negative_infinity = NegativeInfinity;
//!
//! assert!(finite < infinity);
//! assert!(finite > negative_infinity);
//! ```

#![cfg_attr(not(test), no_std)]

#[cfg(test)]
extern crate core;

extern crate num_traits;

use core::cmp::Ordering;
use core::fmt;
use core::fmt::{Display, Formatter};
use core::ops::{Add, Div, Mul, Neg, Sub};
use num_traits::Zero;

/// An "infinitable" value, one that can be either finite or infinite
///
/// # Versioning
///
/// Available since 1.0.0. Variants are re-exported since 1.3.0.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Infinitable<T> {
    /// A finite value `T`
    Finite(T),
    /// Positive infinity, which compares greater than all other values
    Infinity,
    /// Negative infinity, which compares less than all other values
    NegativeInfinity,
}

pub use Infinitable::{Finite, Infinity, NegativeInfinity};

impl<T> Infinitable<T> {
    /// Returns `true` if the value is [`Finite`].
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// let finite = Finite(5);
    /// assert!(finite.is_finite());
    /// let infinite: Infinitable<i32> = Infinity;
    /// assert!(!infinite.is_finite());
    /// ```
    ///
    /// # Versioning
    ///
    /// Available since 1.0.0.
    #[must_use]
    pub fn is_finite(&self) -> bool {
        match self {
            Finite(_) => true,
            _ => false,
        }
    }

    /// Converts from an `Infinitable<T>` to an [`Option<T>`].
    ///
    /// Converts `self` into an [`Option<T>`] possibly containing
    /// a finite value, consuming `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// let finite = Finite(5);
    /// assert_eq!(Some(5), finite.finite());
    /// let infinite: Infinitable<i32> = Infinity;
    /// assert_eq!(None, infinite.finite());
    /// ```
    ///
    /// # Versioning
    ///
    /// Available since 1.1.0.
    #[must_use]
    pub fn finite(self) -> Option<T> {
        match self {
            Finite(x) => Some(x),
            _ => None,
        }
    }

    /// Converts from [`Option<T>`] to [`Finite`] or [`Infinity`].
    ///
    /// <code>[Some]\(T)</code> is converted to <code>[Finite]\(T)</code>,
    /// and [`None`] is converted to [`Infinity`].
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// let finite = Finite(5);
    /// assert_eq!(finite, Infinitable::finite_or_infinity(Some(5)));
    /// let infinite: Infinitable<i32> = Infinity;
    /// assert_eq!(infinite, Infinitable::finite_or_infinity(None));
    /// ```
    ///
    /// # Versioning
    ///
    /// Available since 1.3.0.
    #[must_use]
    pub fn finite_or_infinity(option: Option<T>) -> Infinitable<T> {
        match option {
            Some(x) => Finite(x),
            None => Infinity,
        }
    }

    /// Converts from [`Option<T>`] to [`Finite`] or [`NegativeInfinity`].
    ///
    /// <code>[Some]\(T)</code> is converted to <code>[Finite]\(T)</code>,
    /// and [`None`] is converted to [`NegativeInfinity`].
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// let finite = Finite(5);
    /// assert_eq!(finite, Infinitable::finite_or_negative_infinity(Some(5)));
    /// let infinite: Infinitable<i32> = NegativeInfinity;
    /// assert_eq!(infinite, Infinitable::finite_or_negative_infinity(None));
    /// ```
    ///
    /// # Versioning
    ///
    /// Available since 1.3.0.
    #[must_use]
    pub fn finite_or_negative_infinity(option: Option<T>) -> Infinitable<T> {
        match option {
            Some(x) => Finite(x),
            None => NegativeInfinity,
        }
    }
}

impl<T> From<T> for Infinitable<T> {
    /// Converts from a value `T` to [`Finite`] containing the underlying value.
    ///
    /// Note that there is no special handling for pre-existing infinite values.
    /// Consider using [`from_f32`] or [`from_f64`] for floating-point numbers.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// let finite = Infinitable::from(5);
    /// assert_eq!(Finite(5), finite);
    ///
    /// // Warning: There is no special handling for pre-existing infinite values
    /// let fp_infinity = Infinitable::from(f32::INFINITY);
    /// assert_eq!(Finite(f32::INFINITY), fp_infinity);
    /// assert_ne!(Infinity, fp_infinity);
    /// ```
    ///
    /// # Versioning
    ///
    /// Available since 1.2.0.
    fn from(value: T) -> Infinitable<T> {
        Finite(value)
    }
}

/// Partial order, where the underlying type `T` implements a partial order.
///
/// [`NegativeInfinity`] compares less than all other values,
/// and [`Infinity`] compares greater than all other values.
///
/// # Examples
///
/// ```
/// use infinitable::*;
/// use std::cmp::Ordering;
///
/// let finite = Finite(5);
/// let infinity = Infinity;
/// let negative_infinity = NegativeInfinity;
///
/// assert_eq!(Some(Ordering::Less), finite.partial_cmp(&infinity));
/// assert_eq!(Some(Ordering::Greater), finite.partial_cmp(&negative_infinity));
/// ```
///
/// # Versioning
///
/// Available since 1.0.0.
impl<T> PartialOrd for Infinitable<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match cmp_initial(self, other) {
            CmpInitialResult::Infinite(o) => Some(o),
            CmpInitialResult::Finite(x, y) => x.partial_cmp(y),
        }
    }
}

/// Total order, where the underlying type `T` implements a total order.
///
/// [`NegativeInfinity`] compares less than all other values,
/// and [`Infinity`] compares greater than all other values.
///
/// # Examples
///
/// ```
/// use infinitable::*;
/// use std::cmp::Ordering;
///
/// let finite = Finite(5);
/// let infinity = Infinity;
/// let negative_infinity = NegativeInfinity;
///
/// assert_eq!(Ordering::Less, finite.cmp(&infinity));
/// assert_eq!(Ordering::Greater, finite.cmp(&negative_infinity));
/// ```
///
/// # Versioning
///
/// Available since 1.0.0.
impl<T> Ord for Infinitable<T>
where
    T: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        match cmp_initial(self, other) {
            CmpInitialResult::Infinite(o) => o,
            CmpInitialResult::Finite(x, y) => x.cmp(y),
        }
    }
}

enum CmpInitialResult<'a, T> {
    Infinite(Ordering),
    Finite(&'a T, &'a T),
}

fn cmp_initial<'a, T>(x: &'a Infinitable<T>, y: &'a Infinitable<T>) -> CmpInitialResult<'a, T> {
    match (x, y) {
        (Infinity, Infinity) | (NegativeInfinity, NegativeInfinity) => {
            CmpInitialResult::Infinite(Ordering::Equal)
        }
        (Infinity, _) | (_, NegativeInfinity) => CmpInitialResult::Infinite(Ordering::Greater),
        (NegativeInfinity, _) | (_, Infinity) => CmpInitialResult::Infinite(Ordering::Less),
        (Finite(xf), Finite(yf)) => CmpInitialResult::Finite(xf, yf),
    }
}

impl<T> Add for Infinitable<T>
where
    T: Add,
{
    type Output = Infinitable<T::Output>;

    /// Adds two values.
    ///
    /// The addition operation follows these rules:
    ///
    /// | self               | rhs                | result                |
    /// |--------------------|--------------------|-----------------------|
    /// | `Finite`           | `Finite`           | `Finite` (add values) |
    /// | `Finite`           | `Infinity`         | `Infinity`            |
    /// | `Finite`           | `NegativeInfinity` | `NegativeInfinity`    |
    /// | `Infinity`         | `Finite`           | `Infinity`            |
    /// | `Infinity`         | `Infinity`         | `Infinity`            |
    /// | `Infinity`         | `NegativeInfinity` | Undefined (panic)     |
    /// | `NegativeInfinity` | `Finite`           | `NegativeInfinity`    |
    /// | `NegativeInfinity` | `Infinity`         | Undefined (panic)     |
    /// | `NegativeInfinity` | `NegativeInfinity` | `NegativeInfinity`    |
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// assert_eq!(Finite(5), Finite(2) + Finite(3));
    /// assert_eq!(Infinity, Finite(1) + Infinity);
    /// assert_eq!(NegativeInfinity, NegativeInfinity + Finite(1));
    /// ```
    ///
    /// The addition operation panics with `Infinity` and `NegativeInfinity`:
    ///
    /// ```should_panic
    /// use infinitable::*;
    ///
    /// let infinity: Infinitable<i32> = Infinity;
    /// let negative_infinity: Infinitable<i32> = NegativeInfinity;
    /// let _ = infinity + negative_infinity;
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the operands consist of `Infinity` and `NegativeInfinity`.
    fn add(self, rhs: Infinitable<T>) -> Infinitable<T::Output> {
        match (self, rhs) {
            (Infinity, NegativeInfinity) | (NegativeInfinity, Infinity) => {
                panic!("Cannot add infinity and negative infinity")
            }
            (Finite(lf), Finite(rf)) => Finite(lf.add(rf)),
            (Infinity, _) | (_, Infinity) => Infinity,
            (NegativeInfinity, _) | (_, NegativeInfinity) => NegativeInfinity,
        }
    }
}

impl<T> Sub for Infinitable<T>
where
    T: Sub,
{
    type Output = Infinitable<T::Output>;

    /// Subtracts two values.
    ///
    /// The subtraction operation follows these rules:
    ///
    /// | self               | rhs                | result                     |
    /// |--------------------|--------------------|----------------------------|
    /// | `Finite`           | `Finite`           | `Finite` (subtract values) |
    /// | `Finite`           | `Infinity`         | `NegativeInfinity`         |
    /// | `Finite`           | `NegativeInfinity` | `Infinity`                 |
    /// | `Infinity`         | `Finite`           | `Infinity`                 |
    /// | `Infinity`         | `Infinity`         | Undefined (panic)          |
    /// | `Infinity`         | `NegativeInfinity` | `Infinity`                 |
    /// | `NegativeInfinity` | `Finite`           | `NegativeInfinity`         |
    /// | `NegativeInfinity` | `Infinity`         | `NegativeInfinity`         |
    /// | `NegativeInfinity` | `NegativeInfinity` | Undefined (panic)          |
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// assert_eq!(Finite(3), Finite(5) - Finite(2));
    /// assert_eq!(Infinity, Infinity - Finite(1));
    /// assert_eq!(Infinity, Finite(1) - NegativeInfinity);
    /// assert_eq!(NegativeInfinity, NegativeInfinity - Finite(1));
    /// assert_eq!(NegativeInfinity, Finite(1) - Infinity);
    /// ```
    ///
    /// The subraction operation panics when an infinite value is subtracted
    /// from itself:
    ///
    /// ```should_panic
    /// use infinitable::*;
    ///
    /// let infinity: Infinitable<i32> = Infinity;
    /// let _ = infinity - infinity;
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the operands are both `Infinity` or both `NegativeInfinity`.
    fn sub(self, rhs: Infinitable<T>) -> Infinitable<T::Output> {
        match (self, rhs) {
            (Infinity, Infinity) | (NegativeInfinity, NegativeInfinity) => {
                panic!("Cannot subtract infinite value from itself")
            }
            (Finite(lf), Finite(rf)) => Finite(lf.sub(rf)),
            (Infinity, _) | (_, NegativeInfinity) => Infinity,
            (NegativeInfinity, _) | (_, Infinity) => NegativeInfinity,
        }
    }
}

impl<T> Mul for Infinitable<T>
where
    T: Mul + Zero + PartialOrd,
{
    type Output = Infinitable<<T as Mul>::Output>;

    /// Multiplies two values.
    ///
    /// The multiplication operation follows these rules:
    ///
    /// | self               | rhs                | result                     |
    /// |--------------------|--------------------|----------------------------|
    /// | `Finite`           | `Finite`           | `Finite` (multiply values) |
    /// | `Finite` (> 0)     | `Infinity`         | `Infinity`                 |
    /// | `Finite` (~ 0)     | `Infinity`         | Undefined (panic)          |
    /// | `Finite` (< 0)     | `Infinity`         | `NegativeInfinity`         |
    /// | `Finite` (> 0)     | `NegativeInfinity` | `NegativeInfinity`         |
    /// | `Finite` (~ 0)     | `NegativeInfinity` | Undefined (panic)          |
    /// | `Finite` (< 0)     | `NegativeInfinity` | `Infinity`                 |
    /// | `Infinity`         | `Finite` (> 0)     | `Infinity`                 |
    /// | `Infinity`         | `Finite` (~ 0)     | Undefined (panic)          |
    /// | `Infinity`         | `Finite` (< 0)     | `NegativeInfinity`         |
    /// | `Infinity`         | `Infinity`         | `Infinity`                 |
    /// | `Infinity`         | `NegativeInfinity` | `NegativeInfinity`         |
    /// | `NegativeInfinity` | `Finite` (> 0)     | `NegativeInfinity`         |
    /// | `NegativeInfinity` | `Finite` (~ 0)     | Undefined (panic)          |
    /// | `NegativeInfinity` | `Finite` (< 0)     | `Infinity`                 |
    /// | `NegativeInfinity` | `Infinity`         | `NegativeInfinity`         |
    /// | `NegativeInfinity` | `NegativeInfinity` | `Infinity`                 |
    ///
    /// (In the table, "~ 0" refers to a value that is either equal to or
    /// unordered with zero.)
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// assert_eq!(Finite(6), Finite(2) * Finite(3));
    /// assert_eq!(Infinity, Infinity * Finite(2));
    /// assert_eq!(Infinity, Finite(-1) * NegativeInfinity);
    /// assert_eq!(NegativeInfinity, NegativeInfinity * Finite(2));
    /// assert_eq!(NegativeInfinity, Finite(-1) * Infinity);
    /// ```
    ///
    /// The multiplication operation panics when an infinite value is multiplied
    /// with zero:
    ///
    /// ```should_panic
    /// use infinitable::*;
    ///
    /// let infinity: Infinitable<i32> = Infinity;
    /// let _ = infinity * Finite(0);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if one of the operands is `Infinity` or `NegativeInfinity` and
    /// the other is a `Finite` value with an underlying value equal to or
    /// unordered with zero.
    fn mul(self, rhs: Infinitable<T>) -> Infinitable<<T as Mul>::Output> {
        match (self, rhs) {
            (Infinity, Infinity) | (NegativeInfinity, NegativeInfinity) => Infinity,
            (Infinity, NegativeInfinity) | (NegativeInfinity, Infinity) => NegativeInfinity,
            (Infinity, Finite(x)) | (Finite(x), Infinity) => match x.partial_cmp(&T::zero()) {
                Some(Ordering::Greater) => Infinity,
                Some(Ordering::Less) => NegativeInfinity,
                _ => panic!("Cannot multiply infinite value and zero or unordered value"),
            },
            (NegativeInfinity, Finite(x)) | (Finite(x), NegativeInfinity) => {
                match x.partial_cmp(&T::zero()) {
                    Some(Ordering::Greater) => NegativeInfinity,
                    Some(Ordering::Less) => Infinity,
                    _ => panic!("Cannot multiply infinite value and zero or unordered value"),
                }
            }
            (Finite(lf), Finite(rf)) => Finite(lf.mul(rf)),
        }
    }
}

impl<T> Div for Infinitable<T>
where
    T: Div + Zero + PartialOrd,
    <T as Div>::Output: Zero,
{
    type Output = Infinitable<<T as Div>::Output>;

    /// Divides two values.
    ///
    /// The division operation follows these rules:
    ///
    /// | self               | rhs                | result                     |
    /// |--------------------|--------------------|----------------------------|
    /// | `Finite` (> 0)     | `Finite` (~ 0)     | `Infinity`                 |
    /// | `Finite` (~ 0)     | `Finite` (~ 0)     | Undefined (panic)          |
    /// | `Finite` (< 0)     | `Finite` (~ 0)     | `NegativeInfinity`         |
    /// | `Finite`           | `Finite` (<> 0)    | `Finite` (divide values)   |
    /// | `Finite`           | `Infinity`         | Zero                       |
    /// | `Finite`           | `NegativeInfinity` | Zero                       |
    /// | `Infinity`         | `Finite` (> 0)     | `Infinity`                 |
    /// | `Infinity`         | `Finite` (~ 0)     | `Infinity`                 |
    /// | `Infinity`         | `Finite` (< 0)     | `NegativeInfinity`         |
    /// | `Infinity`         | `Infinity`         | Undefined (panic)          |
    /// | `Infinity`         | `NegativeInfinity` | Undefined (panic)          |
    /// | `NegativeInfinity` | `Finite` (> 0)     | `NegativeInfinity`         |
    /// | `NegativeInfinity` | `Finite` (~ 0)     | `NegativeInfinity`         |
    /// | `NegativeInfinity` | `Finite` (< 0)     | `Infinity`                 |
    /// | `NegativeInfinity` | `Infinity`         | Undefined (panic)          |
    /// | `NegativeInfinity` | `NegativeInfinity` | Undefined (panic)          |
    ///
    /// (In the table, "~ 0" refers to a value that is either equal to or
    /// unordered with zero, and "<> 0" refers to a value that is either
    /// greater than or less than zero.)
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// assert_eq!(Finite(3), Finite(6) / Finite(2));
    /// assert_eq!(Infinity, Infinity / Finite(1));
    /// assert_eq!(Infinity, NegativeInfinity / Finite(-2));
    /// assert_eq!(NegativeInfinity, NegativeInfinity / Finite(1));
    /// assert_eq!(NegativeInfinity, Infinity / Finite(-2));
    /// assert_eq!(Finite(0), Finite(1) / Infinity);
    /// assert_eq!(Finite(0), Finite(1) / NegativeInfinity);
    /// ```
    ///
    /// The division operation panics when an infinite value is divided by
    /// another infinite value, or when zero is divided by itself:
    ///
    /// ```should_panic
    /// use infinitable::*;
    ///
    /// let infinity: Infinitable<i32> = Infinity;
    /// let _ = infinity / infinity;
    /// ```
    ///
    /// ```should_panic
    /// use infinitable::*;
    ///
    /// let _ = Finite(0) / Finite(0);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if both operands are either `Infinity` or `NegativeInfinity`, or
    /// both operands are `Finite` with an underlying value equal to or
    /// unordered with zero.
    fn div(self, rhs: Infinitable<T>) -> Infinitable<<T as Div>::Output> {
        match (self, rhs) {
            (Infinity, Infinity)
            | (NegativeInfinity, NegativeInfinity)
            | (Infinity, NegativeInfinity)
            | (NegativeInfinity, Infinity) => panic!("Cannot divide two infinite values"),
            (Infinity, Finite(x)) => match x.partial_cmp(&T::zero()) {
                Some(Ordering::Less) => NegativeInfinity,
                _ => Infinity,
            },
            (NegativeInfinity, Finite(x)) => match x.partial_cmp(&T::zero()) {
                Some(Ordering::Less) => Infinity,
                _ => NegativeInfinity,
            },
            (Finite(_), Infinity) | (Finite(_), NegativeInfinity) => {
                Finite(<T as Div>::Output::zero())
            }
            (Finite(lf), Finite(rf)) => match rf.partial_cmp(&T::zero()) {
                Some(Ordering::Greater) | Some(Ordering::Less) => Finite(lf.div(rf)),
                _ => match lf.partial_cmp(&T::zero()) {
                    Some(Ordering::Greater) => Infinity,
                    Some(Ordering::Less) => NegativeInfinity,
                    _ => panic!("Cannot divide two zeros or unordered values"),
                },
            },
        }
    }
}

impl<T> Neg for Infinitable<T>
where
    T: Neg,
{
    type Output = Infinitable<T::Output>;

    /// Negates the value, when the underlying type `T` supports negation.
    ///
    /// [`Infinity`] is negated to [`NegativeInfinity`] (and vice versa),
    /// and [`Finite`] is negated based on the underlying value.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// let finite = Finite(5);
    /// assert_eq!(Finite(-5), -finite);
    /// let infinity: Infinitable<i32> = Infinity;
    /// assert_eq!(NegativeInfinity, -infinity);
    /// let negative_infinity: Infinitable<i32> = NegativeInfinity;
    /// assert_eq!(Infinity, -negative_infinity);
    /// ```
    ///
    /// # Versioning
    ///
    /// Available since 1.3.0.
    fn neg(self) -> Infinitable<T::Output> {
        match self {
            Finite(x) => Finite(-x),
            Infinity => NegativeInfinity,
            NegativeInfinity => Infinity,
        }
    }
}

impl<T> Display for Infinitable<T>
where
    T: Display,
{
    /// Formats the value, where the underlying type `T` supports formatting.
    ///
    /// [`Infinity`] is formatted to `"inf"`, [`NegativeInfinity`] is formatted
    /// to `"-inf"`, and [`Finite`] is formatted based on the underlying value.
    ///
    /// # Examples
    ///
    /// ```
    /// use infinitable::*;
    ///
    /// let finite = Finite(5);
    /// assert_eq!("5", format!("{}", finite));
    /// let infinity: Infinitable<i32> = Infinity;
    /// assert_eq!("inf", format!("{}", infinity));
    /// let negative_infinity: Infinitable<i32> = NegativeInfinity;
    /// assert_eq!("-inf", format!("{}", negative_infinity));
    /// ```
    ///
    /// # Versioning
    ///
    /// Available since 1.2.0.
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Finite(x) => write!(f, "{}", x),
            Infinity => write!(f, "inf"),
            NegativeInfinity => write!(f, "-inf"),
        }
    }
}

/// Converts from [`f32`] value to an optional [`Infinitable<f32>`],
/// accounting for floating-point infinities and NaN.
///
/// The value is converted as follows:
///
/// | value             | result                                   |
/// |-------------------|------------------------------------------|
/// | Finite value `x`  | <code>[Some]\([Finite]\(x))</code>       |
/// | Positive infinity | <code>[Some]\([Infinity])</code>         |
/// | Negative infinity | <code>[Some]\([NegativeInfinity])</code> |
/// | NaN               | [`None`]                                 |
pub fn from_f32(value: f32) -> Option<Infinitable<f32>> {
    if value.is_finite() {
        Some(Finite(value))
    } else if value.is_nan() {
        None
    } else if value.is_sign_positive() {
        Some(Infinity)
    } else {
        Some(NegativeInfinity)
    }
}

/// Converts from [`f64`] value to an optional [`Infinitable<f64>`],
/// accounting for floating-point infinities and NaN.
///
/// The value is converted as follows:
///
/// | value             | result                                   |
/// |-------------------|------------------------------------------|
/// | Finite value `x`  | <code>[Some]\([Finite]\(x))</code>       |
/// | Positive infinity | <code>[Some]\([Infinity])</code>         |
/// | Negative infinity | <code>[Some]\([NegativeInfinity])</code> |
/// | NaN               | [`None`]                                 |
pub fn from_f64(value: f64) -> Option<Infinitable<f64>> {
    if value.is_finite() {
        Some(Finite(value))
    } else if value.is_nan() {
        None
    } else if value.is_sign_positive() {
        Some(Infinity)
    } else {
        Some(NegativeInfinity)
    }
}