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
// 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::{ComparableFloat, ComparableFloatRef, Float};
use alloc::string::ToString;
use core::fmt::{Formatter, Result, Write};
use malachite_base::strings::latex::ToLatex;
impl ToLatex for Float {
/// Writes a [`Float`] as a LaTeX math-mode fragment.
///
/// This is as the primitive floats are written. A NaN becomes `\text{NaN}` and the infinities
/// become `\infty` and `-\infty`. A finite [`Float`] is written as
/// [`Display`](core::fmt::Display) writes it, with the exponent, if there is one, lifted into a
/// real power of ten: `1.3e30` becomes ``1.3 \times 10^{30}``.
///
/// As with [`Display`](core::fmt::Display), the digit count is determined by the [`Float`]'s
/// precision rather than by its value, and the two zeros are kept apart.
///
/// # 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.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::PowerOf2;
/// use malachite_base::num::basic::traits::{
/// Infinity, NaN, NegativeInfinity, NegativeZero, One, Zero,
/// };
/// use malachite_base::strings::latex::ToLatex;
/// use malachite_float::Float;
///
/// assert_eq!(Float::NAN.to_latex_string(), r"\text{NaN}");
/// assert_eq!(Float::INFINITY.to_latex_string(), r"\infty");
/// assert_eq!(Float::NEGATIVE_INFINITY.to_latex_string(), r"-\infty");
/// assert_eq!(Float::ZERO.to_latex_string(), "0.0");
/// assert_eq!(Float::NEGATIVE_ZERO.to_latex_string(), "-0.0");
/// assert_eq!(Float::ONE.to_latex_string(), "1.0");
/// assert_eq!(Float::from(1.5).to_latex_string(), "1.5");
/// assert_eq!(
/// Float::power_of_2(100u64).to_latex_string(),
/// r"1.3 \times 10^{30}"
/// );
/// assert_eq!(
/// Float::power_of_2(-100i64).to_latex_string(),
/// r"7.9 \times 10^{-31}"
/// );
/// ```
///
/// | value | fragment | renders as |
/// |-----------------------------|----------------------|----------------------|
/// | `Float::NAN` | `\text{NaN}` | $\text{NaN}$ |
/// | `Float::INFINITY` | `\infty` | $\infty$ |
/// | `Float::ONE` | `1.0` | $1.0$ |
/// | `Float::power_of_2(100u64)` | `1.3 \times 10^{30}` | $1.3 \times 10^{30}$ |
fn fmt_latex(&self, f: &mut Formatter) -> Result {
if self.is_nan() {
return f.write_str("\\text{NaN}");
} else if self.is_infinite() {
return f.write_str(if self.is_sign_positive() {
"\\infty"
} else {
"-\\infty"
});
}
let s = self.to_string();
let Some(e_index) = s.find('e') else {
return f.write_str(&s);
};
let (mantissa, exponent) = s.split_at(e_index);
let exponent = &exponent[1..];
f.write_str(mantissa)?;
f.write_str(" \\times 10^")?;
if exponent.len() == 1 {
// A lone digit needs no braces, and a lone digit is necessarily positive.
f.write_str(exponent)
} else {
f.write_char('{')?;
f.write_str(exponent)?;
f.write_char('}')
}
}
}
impl ToLatex for ComparableFloat {
/// Writes a [`ComparableFloat`] as a LaTeX math-mode fragment.
///
/// The fragment is the wrapped [`Float`]'s own: the wrapper exists to give an equality and an
/// ordering that tell more [`Float`]s apart than the usual ones do, and does not change what
/// the value is.
///
/// # Worst-case complexity
/// Same as the time and additional memory complexity of `fmt_latex` for [`Float`].
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_base::strings::latex::ToLatex;
/// use malachite_float::{ComparableFloat, Float};
///
/// assert_eq!(ComparableFloat(Float::ONE).to_latex_string(), "1.0");
/// ```
#[inline]
fn fmt_latex(&self, f: &mut Formatter) -> Result {
self.0.fmt_latex(f)
}
}
impl ToLatex for ComparableFloatRef<'_> {
/// Writes a [`ComparableFloatRef`] as a LaTeX math-mode fragment.
///
/// The fragment is the wrapped [`Float`]'s own: the wrapper exists to give an equality and an
/// ordering that tell more [`Float`]s apart than the usual ones do, and does not change what
/// the value is.
///
/// # Worst-case complexity
/// Same as the time and additional memory complexity of `fmt_latex` for [`Float`].
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::One;
/// use malachite_base::strings::latex::ToLatex;
/// use malachite_float::{ComparableFloatRef, Float};
///
/// let x = Float::ONE;
/// assert_eq!(ComparableFloatRef(&x).to_latex_string(), "1.0");
/// ```
#[inline]
fn fmt_latex(&self, f: &mut Formatter) -> Result {
self.0.fmt_latex(f)
}
}