balanced-ternary 2.1.0

A library to manipulate balanced ternary values.
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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Sub};

#[cfg(feature = "ternary-string")]
use crate::Ternary;

/// ## Module: Balanced Ternary `Digit`
///
/// This module defines the `Digit` type for the balanced ternary numeral system,
/// along with its associated operations and functionality.
///
/// Represents a digit in the balanced ternary numeral system.
///
/// A digit can have one of three values:
/// - `Neg` (-1): Represents the value -1 in the balanced ternary system.
/// - `Zero` (0): Represents the value 0 in the balanced ternary system.
/// - `Pos` (+1): Represents the value +1 in the balanced ternary system.
///
/// Provides utility functions for converting to/from characters, integers, and negation.
///
/// ### Key Features
///
/// - **`Digit` Type**: Represents a digit in the balanced ternary numeral system.
///     - Possible values: `Neg` (-1), `Zero` (0), `Pos` (+1).
///     - Provides utility functions for converting between characters, integers, and other formats.
/// - **Arithmetic Operators**: Implements arithmetic operations for digits, including:
///     - Negation (`Neg`) and Bitwise Not (`Not`).
///     - Addition (`Add`) and Subtraction (`Sub`).
///     - Multiplication (`Mul`) and Division (`Div`), with safe handling of divisors (division by zero panics).
/// - **Logical Operators**: Supports bitwise logical operations (AND, OR, XOR) based on ternary logic rules.
/// - **Custom Methods**: Additional utility methods implementing balanced ternary logic principles.
///
/// ### Supported Use Cases
///
/// - Arithmetic in balanced ternary numeral systems.
/// - Logic operations in custom numeral systems.
/// - Conversion between balanced ternary representation and more common formats like integers and characters.
///
/// ## `Digit` type arithmetical and logical operations
///
/// - `Neg` and `Not` for `Digit`: Negates the digit value, adhering to balanced ternary rules.
/// - `Add<Digit>` for `Digit`: Adds two `Digit` values and returns a `Digit`.
/// - `Sub<Digit>` for `Digit`: Subtracts one `Digit` from another and returns a `Digit`.
/// - `Mul<Digit>` for `Digit`: Multiplies two `Digit` values and returns a `Digit`.
/// - `Div<Digit>` for `Digit`: Divides one `Digit` by another and returns a `Digit`. Division by zero panics.
///
/// ### Logical Operations for `Digit`
///
/// The `Digit` type supports bitwise logical operations, which are implemented according to logical rules applicable to balanced ternary digits.
///
/// ### Digits operations
/// 
/// ![Digit operations](https://raw.githubusercontent.com/Trehinos/balanced-ternary/refs/heads/master/digit-operations.png)
///
/// `/`, `*`, `&`, `|` and `^` should not be used with `Ternary::each_{with,zip}()`.
/// Instead, use these operators from `Ternary` directly.
///
/// Do so to `add` and `sub` ternaries, too.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Digit {
    /// Represents -1
    Neg,
    /// Represents 0
    Zero,
    /// Represents +1
    Pos,
}

impl Digit {

    /// Converts the `Digit` into a character representation.
    ///
    /// - Returns:
    ///     - `Θ` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `1` for `Digit::Pos`
    pub const fn to_char_theta(&self) -> char {
        match self {
            Digit::Neg => 'Θ',
            Digit::Zero => '0',
            Digit::Pos => '1',
        }
    }


    /// Converts the `Digit` into a character representation.
    ///
    /// - Returns:
    ///     - `Z` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `1` for `Digit::Pos`
    pub const fn to_char_z(&self) -> char {
        match self {
            Digit::Neg => 'Z',
            Digit::Zero => '0',
            Digit::Pos => '1',
        }
    }

    /// Converts the `Digit` into a character representation.
    ///
    /// - Returns:
    ///     - `T` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `1` for `Digit::Pos`
    pub const fn to_char_t(&self) -> char {
        match self {
            Digit::Neg => 'T',
            Digit::Zero => '0',
            Digit::Pos => '1',
        }
    }

    /// Creates a `Digit` from a character representation.
    ///
    /// - Accepts:
    ///     - `Θ` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `1` for `Digit::Pos`
    /// - Panics if the input character is invalid.
    pub const fn from_char_theta(c: char) -> Digit {
        match c { 
            'Θ' => Digit::Neg,
            '0' => Digit::Zero,
            '1' => Digit::Pos,
            _ => panic!("Invalid value. Expected 'Θ', '0', or '1'."),
        }
    }

    /// Creates a `Digit` from a character representation.
    ///
    /// - Accepts:
    ///     - `Z` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `1` for `Digit::Pos`
    /// - Panics if the input character is invalid.
    pub const fn from_char_z(c: char) -> Digit {
        match c {
            'Z' => Digit::Neg,
            '0' => Digit::Zero,
            '1' => Digit::Pos,
            _ => panic!("Invalid value. Expected 'Z', '0', or '1'."),
        }
    }

    /// Creates a `Digit` from a character representation.
    ///
    /// - Accepts:
    ///     - `T` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `1` for `Digit::Pos`
    /// - Panics if the input character is invalid.
    pub const fn from_char_t(c: char) -> Digit {
        match c {
            'T' => Digit::Neg,
            '0' => Digit::Zero,
            '1' => Digit::Pos,
            _ => panic!("Invalid value. Expected 'T', '0', or '1'."),
        }
    }
    
    /// Converts the `Digit` into its character representation.
    ///
    /// - Returns:
    ///     - `-` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `+` for `Digit::Pos`
    pub const fn to_char(&self) -> char {
        match self {
            Digit::Neg => '-',
            Digit::Zero => '0',
            Digit::Pos => '+',
        }
    }

    /// Creates a `Digit` from its character representation.
    ///
    /// - Accepts:
    ///     - `-` for `Digit::Neg`
    ///     - `0` for `Digit::Zero`
    ///     - `+` for `Digit::Pos`
    /// - Panics if the input character is invalid.
    pub const fn from_char(c: char) -> Digit {
        match c {
            '-' => Digit::Neg,
            '0' => Digit::Zero,
            '+' => Digit::Pos,
            _ => panic!("Invalid value. A Ternary must be either -, 0 or +."),
        }
    }

    /// Converts the `Digit` into its integer representation.
    ///
    /// - Returns:
    ///     - -1 for `Digit::Neg`
    ///     - 0 for `Digit::Zero`
    ///     - 1 for `Digit::Pos`
    pub const fn to_i8(&self) -> i8 {
        match self {
            Digit::Neg => -1,
            Digit::Zero => 0,
            Digit::Pos => 1,
        }
    }

    /// Creates a `Digit` from its integer representation.
    ///
    /// - Accepts:
    ///     - -1 for `Digit::Neg`
    ///     - 0 for `Digit::Zero`
    ///     - 1 for `Digit::Pos`
    /// - Panics if the input integer is invalid.
    pub const fn from_i8(i: i8) -> Digit {
        match i {
            -1 => Digit::Neg,
            0 => Digit::Zero,
            1 => Digit::Pos,
            _ => panic!("Invalid value. A Ternary must be either -1, 0 or +1."),
        }
    }

    /// Returns the corresponding possible value of the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Neg` for `Digit::Neg`
    ///     - `Digit::Pos` for `Digit::Zero`
    ///     - `Digit::Pos` for `Digit::Pos`
    pub const fn possibly(self) -> Self {
        match self {
            Digit::Neg => Digit::Neg,
            Digit::Zero => Digit::Pos,
            Digit::Pos => Digit::Pos,
        }
    }

    /// Determines the condition of necessity for the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Neg` for `Digit::Neg`
    ///     - `Digit::Neg` for `Digit::Zero`
    ///     - `Digit::Pos` for `Digit::Pos`
    ///
    /// This method is used to calculate necessity as part
    /// of balanced ternary logic systems.
    pub const fn necessary(self) -> Self {
        match self {
            Digit::Neg => Digit::Neg,
            Digit::Zero => Digit::Neg,
            Digit::Pos => Digit::Pos,
        }
    }

    /// Determines the condition of contingency for the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Neg` for `Digit::Neg`
    ///     - `Digit::Pos` for `Digit::Zero`
    ///     - `Digit::Neg` for `Digit::Pos`
    ///
    /// This method represents contingency in balanced ternary logic,
    /// which defines the specific alternation of `Digit` values.
    pub const fn contingently(self) -> Self {
        match self {
            Digit::Neg => Digit::Neg,
            Digit::Zero => Digit::Pos,
            Digit::Pos => Digit::Neg,
        }
    }

    /// Returns the absolute positive value of the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Pos` for `Digit::Neg`
    ///     - `Digit::Zero` for `Digit::Zero`
    ///     - `Digit::Pos` for `Digit::Pos`
    pub const fn absolute_positive(self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => Digit::Zero,
            Digit::Pos => Digit::Pos,
        }
    }

    /// Determines the strictly positive condition for the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Zero` for `Digit::Neg`
    ///     - `Digit::Zero` for `Digit::Zero`
    ///     - `Digit::Pos` for `Digit::Pos`
    ///
    /// This method is used to calculate strictly positive states
    /// in association with ternary logic.
    pub const fn positive(self) -> Self {
        match self {
            Digit::Neg => Digit::Zero,
            Digit::Zero => Digit::Zero,
            Digit::Pos => Digit::Pos,
        }
    }

    /// Determines the condition of non-negativity for the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Zero` for `Digit::Neg`
    ///     - `Digit::Pos` for `Digit::Zero`
    ///     - `Digit::Pos` for `Digit::Pos`
    ///
    /// This method is used to filter out negative conditions
    /// in computations with balanced ternary representations.
    pub const fn not_negative(self) -> Self {
        match self {
            Digit::Neg => Digit::Zero,
            Digit::Zero => Digit::Pos,
            Digit::Pos => Digit::Pos,
        }
    }

    /// Determines the condition of non-positivity for the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Neg` for `Digit::Neg`
    ///     - `Digit::Neg` for `Digit::Zero`
    ///     - `Digit::Zero` for `Digit::Pos`
    ///
    /// This method complements the `positive` condition and captures
    /// states that are not strictly positive.
    pub const fn not_positive(self) -> Self {
        match self {
            Digit::Neg => Digit::Neg,
            Digit::Zero => Digit::Neg,
            Digit::Pos => Digit::Zero,
        }
    }

    /// Determines the strictly negative condition for the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Neg` for `Digit::Neg`
    ///     - `Digit::Zero` for `Digit::Zero`
    ///     - `Digit::Zero` for `Digit::Pos`
    ///
    /// This method calculates strictly negative states
    /// in association with ternary logic.
    pub const fn negative(self) -> Self {
        match self {
            Digit::Neg => Digit::Neg,
            Digit::Zero => Digit::Zero,
            Digit::Pos => Digit::Zero,
        }
    }

    /// Returns the absolute negative value of the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Neg` for `Digit::Neg`
    ///     - `Digit::Zero` for `Digit::Zero`
    ///     - `Digit::Neg` for `Digit::Pos`
    pub const fn absolute_negative(self) -> Self {
        match self {
            Digit::Neg => Digit::Neg,
            Digit::Zero => Digit::Zero,
            Digit::Pos => Digit::Neg,
        }
    }

    /// Performs Kleene implication with the current `Digit` as `self` and another `Digit`.
    ///
    /// - `self`: The antecedent of the implication.
    /// - `other`: The consequent of the implication.
    ///
    /// - Returns:
    ///     - `Digit::Pos` when `self` is `Digit::Neg`.
    ///     - The positive condition of `other` when `self` is `Digit::Zero`.
    ///     - `other` when `self` is `Digit::Pos`.
    ///
    /// Implements Kleene ternary implication logic, which includes
    /// determining the logical result based on antecedent and consequent.
    pub const fn k3_imply(self, other: Self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => other.positive(),
            Digit::Pos => other,
        }
    }

    /// Apply a ternary equivalence operation for the current `Digit` and another `Digit`.
    ///
    /// - `self`: The first operand of the equivalence operation.
    /// - `other`: The second operand of the equivalence operation.
    ///
    /// - Returns:
    ///     - The negation of `other` when `self` is `Digit::Neg`.
    ///     - `Digit::Zero` when `self` is `Digit::Zero`.
    ///     - `other` when `self` is `Digit::Pos`.
    ///
    /// This method implements a ternary logic equivalence, which captures the relationship between
    /// two balanced ternary `Digit`s based on their logical equivalence.
    pub const fn k3_equiv(self, other: Self) -> Self {
        match self {
            Digit::Neg => match other {
                Digit::Neg => Digit::Pos,
                Digit::Zero => Digit::Zero,
                Digit::Pos => Digit::Neg,
            },
            Digit::Zero => Digit::Zero,
            Digit::Pos => other,
        }
    }

    /// Performs a ternary AND operation for the current `Digit` and another `Digit`.
    ///
    /// - `self`: The first operand of the AND operation.
    /// - `other`: The second operand of the AND operation.
    ///
    /// - Returns:
    ///     - `Digit::Neg` if `self` is `Digit::Neg` and `other` is not `Digit::Zero`.
    ///     - `Digit::Zero` if either `self` or `other` is `Digit::Zero`.
    ///     - `other` if `self` is `Digit::Pos`.
    ///
    /// This method implements Bochvar's internal three-valued logic in balanced ternary AND operation,
    /// which evaluates the logical conjunction of two `Digit`s in the ternary logic system.
    pub const fn bi3_and(self, other: Self) -> Self {
        match self {
            Digit::Neg => other.absolute_negative(),
            Digit::Zero => Digit::Zero,
            Digit::Pos => other,
        }
    }

    /// Performs a ternary OR operation for the current `Digit` and another `Digit`.
    ///
    /// - `self`: The first operand of the OR operation.
    /// - `other`: The second operand of the OR operation.
    ///
    /// - Returns:
    ///     - `other` if `self` is `Digit::Neg`.
    ///     - `Digit::Zero` if `self` is `Digit::Zero`.
    ///     - `Digit::Pos` if `self` is `Digit::Pos` and `other` is not `Digit::Zero`.
    ///
    /// This method implements Bochvar's three-valued internal ternary logic for the OR operation,
    /// determining the logical disjunction of two balanced ternary `Digit`s.
    pub const fn bi3_or(self, other: Self) -> Self {
        match self {
            Digit::Neg => other,
            Digit::Zero => Digit::Zero,
            Digit::Pos => other.absolute_positive(),
        }
    }

    /// Performs Bochvar's internal three-valued implication with the current `Digit` as `self`
    /// and another `Digit` as the consequent.
    ///
    /// - `self`: The antecedent of the implication.
    /// - `other`: The consequent of the implication.
    ///
    /// - Returns:
    ///     - `Digit::Zero` if `self` is `Digit::Neg` and `other` is `Digit::Zero`.
    ///     - `Digit::Pos` if `self` is `Digit::Neg` and `other` is not `Digit::Zero`.
    ///     - `Digit::Zero` if `self` is `Digit::Zero`.
    ///     - `other` if `self` is `Digit::Pos`.
    ///
    /// This method implements Bochvar's internal implication logic, which evaluates
    /// the logical consequence, between two balanced ternary `Digit`s in a manner
    /// consistent with three-valued logic principles.
    pub const fn bi3_imply(self, other: Self) -> Self {
        match self {
            Digit::Neg => other.absolute_positive(),
            Digit::Zero => Digit::Zero,
            Digit::Pos => other,
        }
    }

    /// Performs Łukasiewicz implication with the current `Digit` as `self` and another `Digit`.
    ///
    /// - `self`: The antecedent of the implication.
    /// - `other`: The consequent of the implication.
    ///
    /// - Returns:
    ///     - `Digit::Pos` when `self` is `Digit::Neg`.
    ///     - The non-negative condition of `other` when `self` is `Digit::Zero`.
    ///     - `other` when `self` is `Digit::Pos`.
    ///
    /// Implements Łukasiewicz ternary implication logic, which
    /// evaluates an alternative approach for implication compared to Kleene logic.
    pub const fn l3_imply(self, other: Self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => other.not_negative(),
            Digit::Pos => other,
        }
    }

    /// Performs RM3 implication with the current `Digit` as `self` and another `Digit`.
    ///
    /// - `self`: The antecedent of the implication.
    /// - `other`: The consequent of the implication.
    ///
    /// - Returns:
    ///     - `Digit::Pos` when `self` is `Digit::Neg`.
    ///     - `other` when `self` is `Digit::Zero`.
    ///     - The necessary condition of `other` when `self` is `Digit::Pos`.
    ///
    /// Implements RM3 ternary implication logic, which defines a unique
    /// perspective for implication operations in balanced ternary systems.
    pub const fn rm3_imply(self, other: Self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => other,
            Digit::Pos => other.necessary(),
        }
    }

    /// Performs the paraconsistent-logic implication with the current `Digit` as `self` and another `Digit`.
    ///
    /// - `self`: The antecedent of the implication.
    /// - `other`: The consequent of the implication.
    ///
    /// - Returns:
    ///     - `Digit::Pos` when `self` is `Digit::Neg`.
    ///     - `other` otherwise.
    pub const fn para_imply(self, other: Self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            _ => other,
        }
    }

    /// Performs HT implication with the current `Digit` as `self` and another `Digit`.
    ///
    /// - `self`: The antecedent of the implication.
    /// - `other`: The consequent of the implication.
    ///
    /// - Returns:
    ///     - `Digit::Pos` when `self` is `Digit::Neg`.
    ///     - The possibility condition of `other` when `self` is `Digit::Zero`.
    ///     - `other` when `self` is `Digit::Pos`.
    ///
    /// This method computes HT ternary implication based on heuristic logic.
    pub const fn ht_imply(self, other: Self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => other.possibly(),
            Digit::Pos => other,
        }
    }

    /// Performs HT logical negation of the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Pos` when `self` is `Digit::Neg`.
    ///     - `Digit::Neg` when `self` is `Digit::Zero` or `Digit::Pos`.
    ///
    /// This method evaluates the HT negation result using heuristic ternary logic.
    pub const fn ht_not(self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => Digit::Neg,
            Digit::Pos => Digit::Neg,
        }
    }

    /// Converts the `Digit` to a `bool` in HT logic.
    ///
    /// - Returns:
    ///     - `true` when `self` is `Digit::Pos`.
    ///     - `false` when `self` is `Digit::Neg`.
    ///
    /// - Panics:
    ///     - Panics if `self` is `Digit::Zero`, as `Digit::Zero` cannot be directly
    ///       converted to a boolean value.
    ///       > To ensure `Pos` or `Neg` value, use one of :
    ///       > * [Digit::possibly]
    ///       > * [Digit::necessary]
    ///       > * [Digit::contingently]
    ///       > * [Digit::ht_not]
    ///
    pub const fn ht_bool(self) -> bool {
        match self {
            Digit::Neg => false,
            Digit::Zero => panic!(
                "Cannot convert a Digit::Zero to a bool. \
                 Use Digit::possibly()->to_bool() or Digit::necessary()->to_bool() instead."
            ),
            Digit::Pos => true,
        }
    }

    /// Performs Post's negation of the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Zero` when `self` is `Digit::Neg`.
    ///     - `Digit::Pos` when `self` is `Digit::Zero`.
    ///     - `Digit::Neg` when `self` is `Digit::Pos`.
    ///
    /// This method evaluates the negation based on Post's logic in ternary systems,
    /// which differs from standard negation logic.
    pub const fn post(self) -> Self {
        match self {
            Digit::Neg => Digit::Zero,
            Digit::Zero => Digit::Pos,
            Digit::Pos => Digit::Neg,
        }
    }

    /// Performs the inverse operation from the Post's negation of the current `Digit`.
    ///
    /// - Returns:
    ///     - `Digit::Pos` when `self` is `Digit::Neg`.
    ///     - `Digit::Neg` when `self` is `Digit::Zero`.
    ///     - `Digit::Zero` when `self` is `Digit::Pos`.
    pub const fn pre(self) -> Self {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => Digit::Neg,
            Digit::Pos => Digit::Zero,
        }
    }

    /// This method maps this `Digit` value to its corresponding unbalanced ternary
    /// integer representation.
    ///
    /// - Returns:
    ///     - `0` for `Digit::Neg`.
    ///     - `1` for `Digit::Zero`.
    ///     - `2` for `Digit::Pos`.
    ///
    pub const fn to_unbalanced(self) -> u8 {
        match self {
            Digit::Neg => 0,
            Digit::Zero => 1,
            Digit::Pos => 2,
        }
    }

    /// Creates a `Digit` from an unbalanced ternary integer representation.
    ///
    /// # Arguments:
    /// - `u`: An unsigned 8-bit integer representing an unbalanced ternary value.
    ///
    /// # Returns:
    /// - `Digit::Neg` for `0`.
    /// - `Digit::Zero` for `1`.
    /// - `Digit::Pos` for `2`.
    ///
    /// # Panics:
    /// - Panics if the provided value is not `0`, `1`, or `2`, as these are the
    ///   only valid representations of unbalanced ternary values.
    pub const fn from_unbalanced(u: u8) -> Digit {
        match u {
            0 => Digit::Neg,
            1 => Digit::Zero,
            2 => Digit::Pos,
            _ => panic!("Invalid value. A unbalanced ternary value must be either 0, 1 or 2."),
        }
    }

    /// Increments the `Digit` value and returns a `Ternary` result.
    ///
    /// - The rules for incrementing are based on ternary arithmetic:
    ///   - For `Digit::Neg`:
    ///     - Incrementing results in `Digit::Zero` (`Ternary::parse("0")`).
    ///   - For `Digit::Zero`:
    ///     - Incrementing results in `Digit::Pos` (`Ternary::parse("+")`).
    ///   - For `Digit::Pos`:
    ///     - Incrementing results in "overflow" (`Ternary::parse("+-")`).
    ///
    /// - Returns:
    ///   - A `Ternary` instance representing the result of the increment operation.
    #[cfg(feature = "ternary-string")]
    pub fn inc(self) -> Ternary {
        match self {
            Digit::Neg => Ternary::parse("0"),
            Digit::Zero => Ternary::parse("+"),
            Digit::Pos => Ternary::parse("+-"),
        }
    }

    /// Decrements the `Digit` value and returns a `Ternary` result.
    ///
    /// - The rules for decrementing are based on ternary arithmetic:
    ///   - For `Digit::Neg`:
    ///     - Decrementing results in "underflow" (`Ternary::parse("-+")`).
    ///   - For `Digit::Zero`:
    ///     - Decrementing results in `Digit::Neg` (`Ternary::parse("-")`).
    ///   - For `Digit::Pos`:
    ///     - Decrementing results in `Digit::Zero` (`Ternary::parse("0")`).
    ///
    /// - Returns:
    ///   - A `Ternary` instance representing the result of the decrement operation.
    #[cfg(feature = "ternary-string")]
    pub fn dec(self) -> Ternary {
        match self {
            Digit::Neg => Ternary::parse("-+"),
            Digit::Zero => Ternary::parse("-"),
            Digit::Pos => Ternary::parse("0"),
        }
    }
}

impl Neg for Digit {
    type Output = Self;

    /// Returns the negation of the `Digit`.
    ///
    /// - `Digit::Neg` becomes `Digit::Pos`
    /// - `Digit::Pos` becomes `Digit::Neg`
    /// - `Digit::Zero` remains `Digit::Zero`
    fn neg(self) -> Self::Output {
        match self {
            Digit::Neg => Digit::Pos,
            Digit::Zero => Digit::Zero,
            Digit::Pos => Digit::Neg,
        }
    }
}

impl Not for Digit {
    type Output = Self;
    fn not(self) -> Self::Output {
        -self
    }
}

impl Add<Digit> for Digit {
    type Output = Digit;

    /// Adds two `Digit` values together and returns a `Digit` result.
    ///
    /// - Returns:
    ///   - A `Ternary` instance that holds the result of the addition.
    ///
    /// - Panics:
    ///   - This method does not panic under any circumstances.
    fn add(self, other: Digit) -> Self::Output {
        match self {
            Digit::Neg => other.pre(),
            Digit::Zero => other,
            Digit::Pos => other.post(),
        }
    }
}

impl Sub<Digit> for Digit {
    type Output = Digit;

    /// Subtracts two `Digit` values and returns a `Digit` result.
    ///
    /// - Returns:
    ///   - A `Ternary` instance that holds the result of the subtraction.
    ///
    /// - Panics:
    ///   - This method does not panic under any circumstances.
    fn sub(self, other: Digit) -> Self::Output {
        match self {
            Digit::Neg => other.post(),
            Digit::Zero => -other,
            Digit::Pos => other.pre(),
        }
    }
}

impl Mul<Digit> for Digit {
    type Output = Digit;

    /// Multiplies two `Digit` values together and returns the product as a `Digit`.
    ///
    /// - The rules for multiplication in this implementation are straightforward:
    ///   - `Digit::Neg` multiplied by:
    ///     - `Digit::Neg` results in `Digit::Pos`.
    ///     - `Digit::Zero` results in `Digit::Zero`.
    ///     - `Digit::Pos` results in `Digit::Neg`.
    ///   - `Digit::Zero` multiplied by any `Digit` always results in `Digit::Zero`.
    ///   - `Digit::Pos` multiplied by:
    ///     - `Digit::Neg` results in `Digit::Neg`.
    ///     - `Digit::Zero` results in `Digit::Zero`.
    ///     - `Digit::Pos` results in `Digit::Pos`.
    ///
    /// - Returns:
    ///   - A `Digit` instance representing the result of the multiplication.
    fn mul(self, other: Digit) -> Self::Output {
        match self {
            Digit::Neg => -other,
            Digit::Zero => Digit::Zero,
            Digit::Pos => other,
        }
    }
}

impl Div<Digit> for Digit {
    type Output = Digit;

    /// Divides one `Digit` value by another and returns the result as a `Digit`.
    ///
    /// # Rules for division:
    /// - For `Digit::Neg`:
    ///   - Dividing `Digit::Neg` by `Digit::Neg` results in `Digit::Pos`.
    ///   - Dividing `Digit::Neg` by `Digit::Zero` will panic with "Cannot divide by zero."
    ///   - Dividing `Digit::Neg` by `Digit::Pos` results in `Digit::Neg`.
    /// - For `Digit::Zero`:
    ///   - Dividing `Digit::Zero` by `Digit::Neg` results in `Digit::Zero`.
    ///   - Dividing `Digit::Zero` by `Digit::Zero` will panic with "Cannot divide by zero."
    ///   - Dividing `Digit::Zero` by `Digit::Pos` results in `Digit::Zero`.
    /// - For `Digit::Pos`:
    ///   - Dividing `Digit::Pos` by `Digit::Neg` results in `Digit::Neg`.
    ///   - Dividing `Digit::Pos` by `Digit::Zero` will panic with "Cannot divide by zero."
    ///   - Dividing `Digit::Pos` by `Digit::Pos` results in `Digit::Pos`.
    ///
    /// # Returns:
    /// - A `Digit` value representing the result of the division.
    ///
    /// # Panics:
    /// - Panics with "Cannot divide by zero." if the `other` operand is `Digit::Zero`.
    fn div(self, other: Digit) -> Self::Output {
        if other == Digit::Zero {
            panic!("Cannot divide by zero.");
        }
        self * other
    }
}

impl BitAnd for Digit {
    type Output = Self;

    /// Performs a bitwise AND operation between two `Digit` values and returns the result.
    ///
    /// - The rules for the bitwise AND (`&`) operation are:
    ///   - If `self` is `Digit::Neg`, the result is always `Digit::Neg`.
    ///   - If `self` is `Digit::Zero`, the result depends on the value of `other`:
    ///     - `Digit::Neg` results in `Digit::Neg`.
    ///     - Otherwise, the result is `Digit::Zero`.
    ///   - If `self` is `Digit::Pos`, the result is simply `other`.
    ///
    /// # Returns:
    /// - A `Digit` value that is the result of the bitwise AND operation.
    ///
    /// # Examples:
    /// ```
    /// use balanced_ternary::Digit;
    /// use Digit::{Neg, Pos, Zero};
    ///
    /// assert_eq!(Neg & Pos, Neg);
    /// assert_eq!(Zero & Neg, Neg);
    /// assert_eq!(Zero & Pos, Zero);
    /// assert_eq!(Pos & Zero, Zero);
    /// ```
    fn bitand(self, other: Self) -> Self::Output {
        match self {
            Digit::Neg => Digit::Neg,
            Digit::Zero => other.negative(),
            Digit::Pos => other,
        }
    }
}

impl BitOr for Digit {
    type Output = Self;

    /// Performs a bitwise OR operation between two `Digit` values and returns the result.
    ///
    /// - The rules for the bitwise OR (`|`) operation are as follows:
    ///   - If `self` is `Digit::Neg`, the result is always the value of `other`.
    ///   - If `self` is `Digit::Zero`, the result depends on the value of `other`:
    ///     - `Digit::Pos` results in `Digit::Pos`.
    ///     - Otherwise, the result is `Digit::Zero`.
    ///   - If `self` is `Digit::Pos`, the result is always `Digit::Pos`.
    ///
    /// # Returns:
    /// - A `Digit` value that is the result of the bitwise OR operation.
    ///
    /// # Examples:
    /// ```
    /// use balanced_ternary::Digit;
    /// use Digit::{Neg, Pos, Zero};
    ///
    /// assert_eq!(Neg | Pos, Pos);
    /// assert_eq!(Zero | Neg, Zero);
    /// assert_eq!(Zero | Pos, Pos);
    /// assert_eq!(Pos | Zero, Pos);
    /// ```
    fn bitor(self, other: Self) -> Self::Output {
        match self {
            Digit::Neg => other,
            Digit::Zero => other.positive(),
            Digit::Pos => Digit::Pos,
        }
    }
}

impl BitXor for Digit {
    type Output = Self;

    /// Performs a bitwise XOR (exclusive OR) operation between two `Digit` values.
    ///
    /// - The rules for the bitwise XOR (`^`) operation are as follows:
    ///   - If `self` is `Digit::Neg`, the result is always the value of `rhs`.
    ///   - If `self` is `Digit::Zero`, the result is always `Digit::Zero`.
    ///   - If `self` is `Digit::Pos`, the result is the negation of `rhs`:
    ///     - `Digit::Neg` becomes `Digit::Pos`.
    ///     - `Digit::Zero` becomes `Digit::Zero`.
    ///     - `Digit::Pos` becomes `Digit::Neg`.
    ///
    /// # Returns:
    /// - A `Digit` value that is the result of the bitwise XOR operation.
    ///
    /// # Examples:
    /// ```
    /// use balanced_ternary::Digit;
    /// use Digit::{Neg, Pos, Zero};
    ///
    /// assert_eq!(Neg ^ Pos, Pos);
    /// assert_eq!(Zero ^ Neg, Zero);
    /// assert_eq!(Pos ^ Pos, Neg);
    /// ```
    fn bitxor(self, rhs: Self) -> Self::Output {
        match self {
            Digit::Neg => rhs,
            Digit::Zero => Digit::Zero,
            Digit::Pos => -rhs,
        }
    }
}