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
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::InnerFloat::Finite;
use crate::float::conversion::string::get_str::get_str_ndigits;
use crate::float::conversion::string::to_sci::to_sci_string;
use crate::{ComparableFloat, ComparableFloatRef, Float};
use core::fmt::{Binary, Debug, Display, Formatter, LowerHex, Octal, Result, UpperHex, Write};
use malachite_base::num::arithmetic::traits::{DivRound, Mod, PowerOf2};
use malachite_base::num::conversion::string::options::ToSciOptions;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_base::rounding_modes::RoundingMode::Ceiling;
// The number of base-2^`digit_bits` digits that exactly cover a `Float` with binary exponent
// `exponent` and precision `precision`, with the digits aligned to the base-2^`digit_bits` point:
// the first digit holds `exponent mod digit_bits` significant bits (all `digit_bits` of them when
// the exponent is a multiple), and the rest of the precision fills subsequent digits.
fn power_of_2_digit_count(exponent: i32, precision: u64, digit_bits: u64) -> u64 {
let m = u64::exact_from(exponent.mod_op(i32::exact_from(digit_bits)));
let mut count = precision.saturating_sub(m).div_round(digit_bits, Ceiling).0;
if m != 0 {
count += 1;
}
count
}
// Writes `x` in the base 2^`digit_bits`, with exactly enough digits to represent it. When the
// formatter's alternate flag is set, `prefix` follows the sign for zero and finite values (but not
// NaN or the infinities).
fn fmt_power_of_2_base(
x: &Float,
f: &mut Formatter,
digit_bits: u64,
uppercase: bool,
prefix: &str,
) -> Result {
let mut options = ToSciOptions::default();
options.set_base(u8::power_of_2(digit_bits));
options.set_e_uppercase();
if uppercase {
options.set_uppercase();
}
if let Float(Finite {
exponent,
precision,
..
}) = x
{
options.set_precision(power_of_2_digit_count(*exponent, *precision, digit_bits));
options.set_include_trailing_zeros(true);
}
let s = to_sci_string(x, options);
if !x.is_nan() && !x.is_infinite() {
let (sign, body) = match s.strip_prefix('-') {
Some(body) => ("-", body),
None => ("", s.as_str()),
};
f.write_str(sign)?;
if f.alternate() {
f.write_str(prefix)?;
}
f.write_str(body)
} else {
f.write_str(&s)
}
}
impl Display for Float {
/// Converts a [`Float`] to a [`String`].
///
/// The output has enough digits to round-trip: a [`Float`] of precision $p$ is written with
/// $1+\lceil p \log_{10} 2 \rceil$ significant digits, correctly rounded to nearest. That count
/// depends only on the precision, so it is the same for every value of a given precision, and
/// trailing zeros are kept to reach it; a value of precision 1 prints as `"1.0"` where the same
/// value at precision 100 prints as `"1.0000000000000000000000000000000"`. A printed string
/// therefore does not by itself determine a [`Float`]; see [`ComparableFloat`], whose output
/// also records the precision.
///
/// The output of a finite value always contains a point. Values whose exponent is far from zero
/// use scientific notation, zeros are `0.0` and `-0.0`, and the special values are `NaN`,
/// `Infinity`, and `-Infinity`.
///
/// # Worst-case complexity
/// $T(n) = O(n (\log n)^2 \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::PowerOf2;
/// use malachite_base::num::basic::traits::{
/// Infinity, NaN, NegativeInfinity, NegativeZero, One, Zero,
/// };
/// use malachite_float::Float;
///
/// assert_eq!(Float::NAN.to_string(), "NaN");
/// assert_eq!(Float::INFINITY.to_string(), "Infinity");
/// assert_eq!(Float::NEGATIVE_INFINITY.to_string(), "-Infinity");
/// assert_eq!(Float::ZERO.to_string(), "0.0");
/// assert_eq!(Float::NEGATIVE_ZERO.to_string(), "-0.0");
///
/// assert_eq!(Float::ONE.to_string(), "1.0");
/// assert_eq!(Float::from(1.5).to_string(), "1.5");
/// assert_eq!(Float::from(255).to_string(), "255.0");
/// assert_eq!(
/// Float::from(core::f64::consts::PI).to_string(),
/// "3.1415926535897931"
/// );
///
/// // The digit count is determined by the precision, not by the value.
/// assert_eq!(
/// Float::one_prec(100).to_string(),
/// "1.0000000000000000000000000000000"
/// );
///
/// // Values far from 1 use scientific notation.
/// assert_eq!(Float::power_of_2(100u64).to_string(), "1.3e30");
/// assert_eq!(Float::power_of_2(-100i64).to_string(), "7.9e-31");
/// ```
fn fmt(&self, f: &mut Formatter) -> Result {
let mut options = ToSciOptions::default();
if let Self(Finite { precision, .. }) = self {
options.set_precision(u64::exact_from(get_str_ndigits(10, *precision)));
options.set_include_trailing_zeros(true);
}
f.write_str(&to_sci_string(self, options))
}
}
impl Debug for Float {
/// Converts a [`Float`] to a [`String`].
///
/// This is the same implementation as for [`Display`].
///
/// # Worst-case complexity
/// $T(n) = O(n (\log n)^2 \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{NaN, One, Zero};
/// use malachite_base::strings::ToDebugString;
/// use malachite_float::Float;
///
/// assert_eq!(Float::NAN.to_debug_string(), "NaN");
/// assert_eq!(Float::ZERO.to_debug_string(), "0.0");
/// assert_eq!(Float::ONE.to_debug_string(), "1.0");
/// assert_eq!(Float::from(1.5).to_debug_string(), "1.5");
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Display::fmt(self, f)
}
}
impl Binary for Float {
/// Converts a [`Float`] to a binary [`String`].
///
/// Using the `#` format flag prepends `"0b"` to the string, after any sign.
///
/// Two is a power of two, so every [`Float`] is exactly representable in this base: the output
/// has exactly as many digits as are needed to write the value, one per bit of precision, and
/// is never rounded. The exponent, when one is shown, is a decimal number following an `E`.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::PowerOf2;
/// use malachite_base::num::basic::traits::{NaN, One, Zero};
/// use malachite_base::strings::ToBinaryString;
/// use malachite_float::Float;
///
/// assert_eq!(Float::NAN.to_binary_string(), "NaN");
/// assert_eq!(Float::ZERO.to_binary_string(), "0.0");
/// assert_eq!(Float::ONE.to_binary_string(), "1.0");
/// assert_eq!(Float::from(1.5).to_binary_string(), "1.1");
/// assert_eq!(Float::from(255).to_binary_string(), "11111111.0");
/// assert_eq!(Float::power_of_2(100u64).to_binary_string(), "1.0E100");
///
/// assert_eq!(format!("{:#b}", Float::ZERO), "0b0.0");
/// assert_eq!(format!("{:#b}", Float::from(1.5)), "0b1.1");
/// assert_eq!(format!("{:#b}", Float::from(-1.5)), "-0b1.1");
/// // The specials are never prefixed.
/// assert_eq!(format!("{:#b}", Float::NAN), "NaN");
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
fmt_power_of_2_base(self, f, 1, false, "0b")
}
}
impl Octal for Float {
/// Converts a [`Float`] to an octal [`String`].
///
/// Using the `#` format flag prepends `"0o"` to the string, after any sign.
///
/// Eight is a power of two, so every [`Float`] is exactly representable in this base: the
/// output has exactly as many digits as are needed to write the value, and is never rounded.
/// The exponent, when one is shown, is a decimal number following an `E`.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::PowerOf2;
/// use malachite_base::num::basic::traits::{NaN, One, Zero};
/// use malachite_base::strings::ToOctalString;
/// use malachite_float::Float;
///
/// assert_eq!(Float::NAN.to_octal_string(), "NaN");
/// assert_eq!(Float::ZERO.to_octal_string(), "0.0");
/// assert_eq!(Float::ONE.to_octal_string(), "1.0");
/// assert_eq!(Float::from(1.5).to_octal_string(), "1.4");
/// assert_eq!(Float::from(255).to_octal_string(), "377.0");
/// assert_eq!(Float::power_of_2(100u64).to_octal_string(), "2.0E33");
///
/// assert_eq!(format!("{:#o}", Float::ZERO), "0o0.0");
/// assert_eq!(format!("{:#o}", Float::from(1.5)), "0o1.4");
/// assert_eq!(format!("{:#o}", Float::from(-1.5)), "-0o1.4");
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
fmt_power_of_2_base(self, f, 3, false, "0o")
}
}
impl LowerHex for Float {
/// Converts a [`Float`] to a hexadecimal [`String`], using lowercase digits.
///
/// Using the `#` format flag prepends `"0x"` to the string, after any sign.
///
/// Sixteen is a power of two, so every [`Float`] is exactly representable in this base: the
/// output has exactly as many digits as are needed to write the value, and is never rounded.
/// The exponent, when one is shown, is a decimal number following an `E`.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::PowerOf2;
/// use malachite_base::num::basic::traits::{NaN, One, Zero};
/// use malachite_base::strings::ToLowerHexString;
/// use malachite_float::Float;
///
/// assert_eq!(Float::NAN.to_lower_hex_string(), "NaN");
/// assert_eq!(Float::ZERO.to_lower_hex_string(), "0.0");
/// assert_eq!(Float::ONE.to_lower_hex_string(), "1.0");
/// assert_eq!(Float::from(1.5).to_lower_hex_string(), "1.8");
/// assert_eq!(Float::from(255).to_lower_hex_string(), "ff.0");
/// assert_eq!(Float::power_of_2(100u64).to_lower_hex_string(), "1.0E+25");
///
/// assert_eq!(format!("{:#x}", Float::ZERO), "0x0.0");
/// assert_eq!(format!("{:#x}", Float::from(1.5)), "0x1.8");
/// assert_eq!(format!("{:#x}", Float::from(-1.5)), "-0x1.8");
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
fmt_power_of_2_base(self, f, 4, false, "0x")
}
}
impl UpperHex for Float {
/// Converts a [`Float`] to a hexadecimal [`String`], using uppercase digits.
///
/// Using the `#` format flag prepends `"0x"` to the string, after any sign. As for the
/// primitive integers, the prefix stays lowercase.
///
/// This is the same as [`LowerHex`] apart from the case of the digits; see it for the
/// properties of the base.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{NaN, One, Zero};
/// use malachite_base::strings::ToUpperHexString;
/// use malachite_float::Float;
///
/// assert_eq!(Float::NAN.to_upper_hex_string(), "NaN");
/// assert_eq!(Float::ZERO.to_upper_hex_string(), "0.0");
/// assert_eq!(Float::ONE.to_upper_hex_string(), "1.0");
/// assert_eq!(Float::from(1.5).to_upper_hex_string(), "1.8");
/// assert_eq!(Float::from(255).to_upper_hex_string(), "FF.0");
///
/// assert_eq!(format!("{:#X}", Float::from(255)), "0xFF.0");
/// assert_eq!(format!("{:#X}", Float::from(-1.5)), "-0x1.8");
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
fmt_power_of_2_base(self, f, 4, true, "0x")
}
}
impl Display for ComparableFloat {
/// Converts a [`ComparableFloat`] to a [`String`].
///
/// This is the same implementation as for [`ComparableFloatRef`]: the wrapped [`Float`]'s
/// [`Display`] output, followed by `#` and the precision.
///
/// # Worst-case complexity
/// $T(n) = O(n (\log n)^2 \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_float::{ComparableFloat, Float};
///
/// assert_eq!(ComparableFloat(Float::ONE).to_string(), "1.0#1");
/// assert_eq!(ComparableFloat(Float::one_prec(100)).to_string().len(), 37);
/// assert_eq!(ComparableFloat(Float::from(1.5)).to_string(), "1.5#2");
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Display::fmt(&ComparableFloatRef(&self.0), f)
}
}
impl Debug for ComparableFloat {
/// Converts a [`ComparableFloat`] to a [`String`].
///
/// This is the same implementation as for [`Display`].
///
/// # Worst-case complexity
/// $T(n) = O(n (\log n)^2 \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_base::strings::ToDebugString;
/// use malachite_float::{ComparableFloat, Float};
///
/// assert_eq!(ComparableFloat(Float::ONE).to_debug_string(), "1.0#1");
/// assert_eq!(ComparableFloat(Float::from(1.5)).to_debug_string(), "1.5#2");
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Debug::fmt(&ComparableFloatRef(&self.0), f)
}
}
impl LowerHex for ComparableFloat {
/// Converts a [`ComparableFloat`] to a hexadecimal [`String`].
///
/// This is the same implementation as for [`ComparableFloatRef`]: the wrapped [`Float`]'s
/// [`LowerHex`] output, followed by `#` and the precision. Using the `#` format flag prepends
/// `"0x"` to the value, after any sign.
///
/// This is the form that identifies a [`Float`] exactly, and the one the tests use as their
/// canonical label: the digits are exact because the base is a power of two, and the suffix
/// records the precision, which the digits alone may not determine.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_float::{ComparableFloat, Float};
///
/// assert_eq!(format!("{:x}", ComparableFloat(Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#x}", ComparableFloat(Float::ONE)), "0x1.0#1");
/// assert_eq!(
/// format!("{:#x}", ComparableFloat(Float::from(1.5))),
/// "0x1.8#2"
/// );
/// assert_eq!(
/// format!("{:#x}", ComparableFloat(Float::from(-1.5))),
/// "-0x1.8#2"
/// );
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
LowerHex::fmt(&ComparableFloatRef(&self.0), f)
}
}
impl Binary for ComparableFloat {
/// Converts a [`ComparableFloat`] to a binary [`String`].
///
/// This is the same implementation as for [`ComparableFloatRef`]: the wrapped [`Float`]'s
/// [`Binary`] output, followed by `#` and the precision. Using the `#` format flag prepends
/// `"0b"` to the value, after any sign.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_float::{ComparableFloat, Float};
///
/// assert_eq!(format!("{:b}", ComparableFloat(Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#b}", ComparableFloat(Float::ONE)), "0b1.0#1");
/// assert_eq!(
/// format!("{:#b}", ComparableFloat(Float::from(-1.5))),
/// "-0b1.1#2"
/// );
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Binary::fmt(&ComparableFloatRef(&self.0), f)
}
}
impl Octal for ComparableFloat {
/// Converts a [`ComparableFloat`] to an octal [`String`].
///
/// This is the same implementation as for [`ComparableFloatRef`]: the wrapped [`Float`]'s
/// [`Octal`] output, followed by `#` and the precision. Using the `#` format flag prepends
/// `"0o"` to the value, after any sign.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_float::{ComparableFloat, Float};
///
/// assert_eq!(format!("{:o}", ComparableFloat(Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#o}", ComparableFloat(Float::ONE)), "0o1.0#1");
/// assert_eq!(
/// format!("{:#o}", ComparableFloat(Float::from(-1.5))),
/// "-0o1.4#2"
/// );
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Octal::fmt(&ComparableFloatRef(&self.0), f)
}
}
impl UpperHex for ComparableFloat {
/// Converts a [`ComparableFloat`] to a hexadecimal [`String`].
///
/// This is the same implementation as for [`ComparableFloatRef`]: the wrapped [`Float`]'s
/// [`UpperHex`] output, followed by `#` and the precision. Using the `#` format flag prepends
/// `"0x"` to the value, after any sign.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_float::{ComparableFloat, Float};
///
/// assert_eq!(format!("{:X}", ComparableFloat(Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#X}", ComparableFloat(Float::ONE)), "0x1.0#1");
/// assert_eq!(
/// format!("{:#X}", ComparableFloat(Float::from(255))),
/// "0xFF.0#8"
/// );
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
UpperHex::fmt(&ComparableFloatRef(&self.0), f)
}
}
impl Display for ComparableFloatRef<'_> {
/// Converts a [`ComparableFloatRef`] to a [`String`].
///
/// The output is the wrapped [`Float`]'s [`Display`] output, followed by `#` and the precision,
/// as in `"1.5#2"`. Because a [`Float`]'s decimal digits do not determine its precision, the
/// suffix is what makes the output identify the value that [`ComparableFloatRef`]'s [`Eq`]
/// compares. The special values and the zeros have no precision, so they are written exactly as
/// [`Float`] writes them.
///
/// # Worst-case complexity
/// $T(n) = O(n (\log n)^2 \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{NaN, One, Zero};
/// use malachite_float::{ComparableFloatRef, Float};
///
/// assert_eq!(ComparableFloatRef(&Float::ONE).to_string(), "1.0#1");
/// assert_eq!(ComparableFloatRef(&Float::from(1.5)).to_string(), "1.5#2");
/// assert_eq!(ComparableFloatRef(&Float::from(255)).to_string(), "255.0#8");
///
/// // The specials and the zeros carry no precision.
/// assert_eq!(ComparableFloatRef(&Float::NAN).to_string(), "NaN");
/// assert_eq!(ComparableFloatRef(&Float::ZERO).to_string(), "0.0");
/// ```
fn fmt(&self, f: &mut Formatter) -> Result {
if let x @ Float(Finite { precision, .. }) = &self.0 {
write!(f, "{x}")?;
f.write_char('#')?;
write!(f, "{precision}")
} else {
Display::fmt(&self.0, f)
}
}
}
impl LowerHex for ComparableFloatRef<'_> {
/// Converts a [`ComparableFloatRef`] to a hexadecimal [`String`].
///
/// The output is the wrapped [`Float`]'s [`LowerHex`] output, followed by `#` and the
/// precision, as in `"1.8#2"`. Using the `#` format flag prepends `"0x"` to the value, after
/// any sign, giving `"0x1.8#2"`.
///
/// This is the form that identifies a [`Float`] exactly: the digits are exact because the base
/// is a power of two, and the suffix supplies the precision. It is also what a base-16
/// [`FromStringBase`](malachite_base::num::conversion::traits::FromStringBase) parse accepts,
/// so the two round-trip, which is why the tests use it as their canonical label.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{NaN, One};
/// use malachite_float::{ComparableFloatRef, Float};
///
/// assert_eq!(format!("{:x}", ComparableFloatRef(&Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#x}", ComparableFloatRef(&Float::ONE)), "0x1.0#1");
/// assert_eq!(
/// format!("{:#x}", ComparableFloatRef(&Float::from(1.5))),
/// "0x1.8#2"
/// );
/// assert_eq!(
/// format!("{:#x}", ComparableFloatRef(&Float::from(255))),
/// "0xff.0#8"
/// );
/// assert_eq!(format!("{:#x}", ComparableFloatRef(&Float::NAN)), "NaN");
/// ```
fn fmt(&self, f: &mut Formatter) -> Result {
if let x @ Float(Finite { precision, .. }) = &self.0 {
if f.alternate() {
write!(f, "{x:#x}")?;
} else {
write!(f, "{x:x}")?;
}
f.write_char('#')?;
write!(f, "{precision}")
} else {
LowerHex::fmt(&self.0, f)
}
}
}
impl Binary for ComparableFloatRef<'_> {
/// Converts a [`ComparableFloatRef`] to a binary [`String`].
///
/// The output is the wrapped [`Float`]'s [`Binary`] output, followed by `#` and the precision.
/// Using the `#` format flag prepends `"0b"` to the value, after any sign.
///
/// Like the hexadecimal form, this identifies a [`Float`] exactly: the digits are exact because
/// the base is a power of two, and the suffix supplies the precision, which the digits alone
/// may not determine.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{NaN, One};
/// use malachite_float::{ComparableFloatRef, Float};
///
/// assert_eq!(format!("{:b}", ComparableFloatRef(&Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#b}", ComparableFloatRef(&Float::ONE)), "0b1.0#1");
/// assert_eq!(
/// format!("{:#b}", ComparableFloatRef(&Float::from(1.5))),
/// "0b1.1#2"
/// );
/// assert_eq!(
/// format!("{:#b}", ComparableFloatRef(&Float::from(255))),
/// "0b11111111.0#8"
/// );
/// assert_eq!(format!("{:#b}", ComparableFloatRef(&Float::NAN)), "NaN");
/// ```
fn fmt(&self, f: &mut Formatter) -> Result {
if let x @ Float(Finite { precision, .. }) = &self.0 {
if f.alternate() {
write!(f, "{x:#b}")?;
} else {
write!(f, "{x:b}")?;
}
f.write_char('#')?;
write!(f, "{precision}")
} else {
Binary::fmt(&self.0, f)
}
}
}
impl Octal for ComparableFloatRef<'_> {
/// Converts a [`ComparableFloatRef`] to an octal [`String`].
///
/// The output is the wrapped [`Float`]'s [`Octal`] output, followed by `#` and the precision.
/// Using the `#` format flag prepends `"0o"` to the value, after any sign.
///
/// Like the hexadecimal form, this identifies a [`Float`] exactly: the digits are exact because
/// the base is a power of two, and the suffix supplies the precision, which the digits alone
/// may not determine.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{NaN, One};
/// use malachite_float::{ComparableFloatRef, Float};
///
/// assert_eq!(format!("{:o}", ComparableFloatRef(&Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#o}", ComparableFloatRef(&Float::ONE)), "0o1.0#1");
/// assert_eq!(
/// format!("{:#o}", ComparableFloatRef(&Float::from(1.5))),
/// "0o1.4#2"
/// );
/// assert_eq!(
/// format!("{:#o}", ComparableFloatRef(&Float::from(255))),
/// "0o377.0#8"
/// );
/// assert_eq!(format!("{:#o}", ComparableFloatRef(&Float::NAN)), "NaN");
/// ```
fn fmt(&self, f: &mut Formatter) -> Result {
if let x @ Float(Finite { precision, .. }) = &self.0 {
if f.alternate() {
write!(f, "{x:#o}")?;
} else {
write!(f, "{x:o}")?;
}
f.write_char('#')?;
write!(f, "{precision}")
} else {
Octal::fmt(&self.0, f)
}
}
}
impl UpperHex for ComparableFloatRef<'_> {
/// Converts a [`ComparableFloatRef`] to a hexadecimal [`String`].
///
/// The output is the wrapped [`Float`]'s [`UpperHex`] output, followed by `#` and the
/// precision. Using the `#` format flag prepends `"0x"` to the value, after any sign.
///
/// Like the hexadecimal form, this identifies a [`Float`] exactly: the digits are exact because
/// the base is a power of two, and the suffix supplies the precision, which the digits alone
/// may not determine.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{NaN, One};
/// use malachite_float::{ComparableFloatRef, Float};
///
/// assert_eq!(format!("{:X}", ComparableFloatRef(&Float::ONE)), "1.0#1");
/// assert_eq!(format!("{:#X}", ComparableFloatRef(&Float::ONE)), "0x1.0#1");
/// assert_eq!(
/// format!("{:#X}", ComparableFloatRef(&Float::from(255))),
/// "0xFF.0#8"
/// );
/// // As for `Float`, the prefix stays lowercase, matching the primitive integers.
/// assert_eq!(
/// format!("{:#X}", ComparableFloatRef(&Float::from(-1.5))),
/// "-0x1.8#2"
/// );
/// assert_eq!(format!("{:#X}", ComparableFloatRef(&Float::NAN)), "NaN");
/// ```
fn fmt(&self, f: &mut Formatter) -> Result {
if let x @ Float(Finite { precision, .. }) = &self.0 {
if f.alternate() {
write!(f, "{x:#X}")?;
} else {
write!(f, "{x:X}")?;
}
f.write_char('#')?;
write!(f, "{precision}")
} else {
UpperHex::fmt(&self.0, f)
}
}
}
impl Debug for ComparableFloatRef<'_> {
/// Converts a [`ComparableFloatRef`] to a [`String`].
///
/// This is the same implementation as for [`Display`].
///
/// # Worst-case complexity
/// $T(n) = O(n (\log n)^2 \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.0.complexity()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_base::strings::ToDebugString;
/// use malachite_float::{ComparableFloatRef, Float};
///
/// assert_eq!(ComparableFloatRef(&Float::ONE).to_debug_string(), "1.0#1");
/// assert_eq!(
/// ComparableFloatRef(&Float::from(1.5)).to_debug_string(),
/// "1.5#2"
/// );
/// ```
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Display::fmt(self, f)
}
}