malachite_float/arithmetic/
mod.rs

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
// Copyright © 2025 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/>.

/// Absolute value of [`Float`](super::Float)s.
pub mod abs;
/// Addition of [`Float`](super::Float)s, and of [`Float`](super::Float)s with
/// [`Rational`](malachite_q::Rational)s.
pub mod add;
/// Division of [`Float`](super::Float)s, of [`Float`](super::Float)s by
/// [`Rational`](malachite_q::Rational)s, and of [`Rational`](malachite_q::Rational)s by
/// [`Float`](super::Float)s.
pub mod div;
/// An implementations of [`IsPowerOf2`](malachite_base::num::arithmetic::traits::IsPowerOf2), a
/// trait for determining whether a number is an integer power of 2.
pub mod is_power_of_2;
/// Multiplication of [`Float`](super::Float)s, and of [`Float`](super::Float)s with
/// [`Rational`](malachite_q::Rational)s.
pub mod mul;
/// Negation of [`Float`](super::Float)s.
pub mod neg;
/// Implementations of [`PowerOf2`](malachite_base::num::arithmetic::traits::PowerOf2), a trait for
/// computing a power of 2.
pub mod power_of_2;
/// Implementations of [`Reciprocal`](malachite_base::num::arithmetic::traits::Reciprocal) and
/// [`ReciprocalAssign`](malachite_base::num::arithmetic::traits::ReciprocalAssign), traits for
/// computing the reciprocal of a number.
pub mod reciprocal;
/// Left-shifting a [`Float`](super::Float) (multiplying it by a power of 2).
///
/// # shl
/// ```
/// use malachite_base::num::basic::traits::{Infinity, Zero};
/// use malachite_float::Float;
///
/// assert_eq!(Float::ZERO << 10, 0);
/// assert_eq!(Float::INFINITY << 10, Float::INFINITY);
/// assert_eq!(
///     (Float::from(std::f64::consts::PI) << 10u8).to_string(),
///     "3216.990877275948"
/// );
/// assert_eq!(
///     (Float::from(std::f64::consts::PI) << -10i8).to_string(),
///     "0.003067961575771282"
/// );
///
/// assert_eq!(&Float::ZERO << 10, 0);
/// assert_eq!(&Float::INFINITY << 10, Float::INFINITY);
/// assert_eq!(
///     (&Float::from(std::f64::consts::PI) << 10u8).to_string(),
///     "3216.990877275948"
/// );
/// assert_eq!(
///     (&Float::from(std::f64::consts::PI) << -10i8).to_string(),
///     "0.003067961575771282"
/// );
/// ```
///
/// # shl_assign
/// ```
/// use malachite_base::num::basic::traits::{Infinity, Zero};
/// use malachite_float::Float;
///
/// let mut x = Float::ZERO;
/// x <<= 10;
/// assert_eq!(x, 0);
///
/// let mut x = Float::INFINITY;
/// x <<= 10;
/// assert_eq!(x, Float::INFINITY);
///
/// let mut x = Float::from(std::f64::consts::PI);
/// x <<= 10;
/// assert_eq!(x.to_string(), "3216.990877275948");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// x <<= -10;
/// assert_eq!(x.to_string(), "0.003067961575771282");
/// ```
pub mod shl;
/// Implementations of [`ShlRound`](malachite_base::num::arithmetic::traits::ShlRound) and
/// [`ShlRoundAssign`](malachite_base::num::arithmetic::traits::ShlRoundAssign), traits for
/// multiplying a number by a power of 2 and rounding according to a specified
/// [`RoundingMode`](malachite_base::rounding_modes::RoundingMode). For [`Float`](super::Float)s,
/// rounding is only necessary in the cases of overflow and underflow.
///
/// # shl_round
/// ```
/// use malachite_base::num::arithmetic::traits::ShlRound;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(10u8, Nearest);
/// assert_eq!(shifted.to_string(), "3216.990877275948");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(-10i8, Nearest);
/// assert_eq!(shifted.to_string(), "0.003067961575771282");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(u32::MAX, Floor);
/// assert_eq!(shifted.to_string(), "too_big");
/// assert_eq!(o, Less);
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(u32::MAX, Ceiling);
/// assert_eq!(shifted.to_string(), "Infinity");
/// assert_eq!(o, Greater);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(10u8, Nearest);
/// assert_eq!(shifted.to_string(), "3216.990877275948");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(-10i8, Nearest);
/// assert_eq!(shifted.to_string(), "0.003067961575771282");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(u32::MAX, Floor);
/// assert_eq!(shifted.to_string(), "too_big");
/// assert_eq!(o, Less);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(u32::MAX, Ceiling);
/// assert_eq!(shifted.to_string(), "Infinity");
/// assert_eq!(o, Greater);
/// ```
///
/// # shl_assign
/// ```
/// use malachite_base::num::arithmetic::traits::ShlRoundAssign;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shl_round_assign(10u8, Nearest), Equal);
/// assert_eq!(x.to_string(), "3216.990877275948");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shl_round_assign(-10i8, Nearest), Equal);
/// assert_eq!(x.to_string(), "0.003067961575771282");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shl_round_assign(u32::MAX, Floor), Less);
/// assert_eq!(x.to_string(), "too_big");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shl_round_assign(u32::MAX, Ceiling), Greater);
/// assert_eq!(x.to_string(), "Infinity");
/// ```
pub mod shl_round;
/// Right-shifting a [`Float`](super::Float) (dividing it by a power of 2).
///
/// # shr
/// ```
/// use malachite_base::num::basic::traits::{Infinity, Zero};
/// use malachite_float::Float;
///
/// assert_eq!(Float::ZERO >> 10, 0);
/// assert_eq!(Float::INFINITY >> 10, Float::INFINITY);
/// assert_eq!(
///     (Float::from(std::f64::consts::PI) >> 10u8).to_string(),
///     "0.003067961575771282"
/// );
/// assert_eq!(
///     (Float::from(std::f64::consts::PI) >> -10i8).to_string(),
///     "3216.990877275948"
/// );
///
/// assert_eq!(&Float::ZERO >> 10, 0);
/// assert_eq!(&Float::INFINITY >> 10, Float::INFINITY);
/// assert_eq!(
///     (&Float::from(std::f64::consts::PI) >> 10u8).to_string(),
///     "0.003067961575771282"
/// );
/// assert_eq!(
///     (&Float::from(std::f64::consts::PI) >> -10i8).to_string(),
///     "3216.990877275948"
/// );
/// ```
///
/// # shr_assign
/// ```
/// use malachite_base::num::basic::traits::{Infinity, Zero};
/// use malachite_float::Float;
///
/// let mut x = Float::ZERO;
/// x >>= 10;
/// assert_eq!(x, 0);
///
/// let mut x = Float::INFINITY;
/// x >>= 10;
/// assert_eq!(x, Float::INFINITY);
///
/// let mut x = Float::from(std::f64::consts::PI);
/// x >>= 10;
/// assert_eq!(x.to_string(), "0.003067961575771282");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// x >>= -10;
/// assert_eq!(x.to_string(), "3216.990877275948");
/// ```
pub mod shr;
/// Implementations of [`ShlRound`](malachite_base::num::arithmetic::traits::ShrRound) and
/// [`ShrRoundAssign`](malachite_base::num::arithmetic::traits::ShrRoundAssign), traits for dividing
/// a number by a power of 2 and rounding according to a specified
/// [`RoundingMode`](malachite_base::rounding_modes::RoundingMode). For [`Float`](super::Float)s,
/// rounding is only necessary in the cases of overflow and underflow.
///
/// # shr_round
/// ```
/// use malachite_base::num::arithmetic::traits::ShrRound;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(10u8, Nearest);
/// assert_eq!(shifted.to_string(), "0.003067961575771282");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(-10i8, Nearest);
/// assert_eq!(shifted.to_string(), "3216.990877275948");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(u32::MAX, Floor);
/// assert_eq!(shifted.to_string(), "0.0");
/// assert_eq!(o, Less);
///
/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(u32::MAX, Ceiling);
/// assert_eq!(shifted.to_string(), "too_small");
/// assert_eq!(o, Greater);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(10u8, Nearest);
/// assert_eq!(shifted.to_string(), "0.003067961575771282");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(-10i8, Nearest);
/// assert_eq!(shifted.to_string(), "3216.990877275948");
/// assert_eq!(o, Equal);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(u32::MAX, Floor);
/// assert_eq!(shifted.to_string(), "0.0");
/// assert_eq!(o, Less);
///
/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(u32::MAX, Ceiling);
/// assert_eq!(shifted.to_string(), "too_small");
/// assert_eq!(o, Greater);
/// ```
///
/// # shr_assign
/// ```
/// use malachite_base::num::arithmetic::traits::ShrRoundAssign;
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shr_round_assign(10u8, Nearest), Equal);
/// assert_eq!(x.to_string(), "0.003067961575771282");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shr_round_assign(-10i8, Nearest), Equal);
/// assert_eq!(x.to_string(), "3216.990877275948");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shr_round_assign(u32::MAX, Floor), Less);
/// assert_eq!(x.to_string(), "0.0");
///
/// let mut x = Float::from(std::f64::consts::PI);
/// assert_eq!(x.shr_round_assign(u32::MAX, Ceiling), Greater);
/// assert_eq!(x.to_string(), "too_small");
/// ```
pub mod shr_round;
/// An implementation of [`Sign`](malachite_base::num::arithmetic::traits::Sign), a trait for
/// determining the sign of a number.
pub mod sign;
/// Squaring of [`Float`](super::Float)s.
pub mod square;
/// Subtraction of [`Float`](super::Float)s, of [`Float`](super::Float)s by
/// [`Rational`](malachite_q::Rational)s, and of [`Rational`](malachite_q::Rational)s by
/// [`Float`](super::Float)s.
pub mod sub;