malachite_float/arithmetic/
mod.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9/// Absolute value of [`Float`](super::Float)s.
10pub mod abs;
11/// Addition of [`Float`](super::Float)s, and of [`Float`](super::Float)s with
12/// [`Rational`](malachite_q::Rational)s.
13pub mod add;
14/// Division of [`Float`](super::Float)s, of [`Float`](super::Float)s by
15/// [`Rational`](malachite_q::Rational)s, and of [`Rational`](malachite_q::Rational)s by
16/// [`Float`](super::Float)s.
17pub mod div;
18/// An implementations of [`IsPowerOf2`](malachite_base::num::arithmetic::traits::IsPowerOf2), a
19/// trait for determining whether a number is an integer power of 2.
20pub mod is_power_of_2;
21/// Multiplication of [`Float`](super::Float)s, and of [`Float`](super::Float)s with
22/// [`Rational`](malachite_q::Rational)s.
23pub mod mul;
24/// Negation of [`Float`](super::Float)s.
25pub mod neg;
26/// Implementations of [`PowerOf2`](malachite_base::num::arithmetic::traits::PowerOf2), a trait for
27/// computing a power of 2.
28pub mod power_of_2;
29/// Implementations of [`Reciprocal`](malachite_base::num::arithmetic::traits::Reciprocal) and
30/// [`ReciprocalAssign`](malachite_base::num::arithmetic::traits::ReciprocalAssign), traits for
31/// computing the reciprocal of a number.
32pub mod reciprocal;
33/// Left-shifting a [`Float`](super::Float) (multiplying it by a power of 2).
34///
35/// # shl
36/// ```
37/// use malachite_base::num::basic::traits::{Infinity, Zero};
38/// use malachite_float::Float;
39///
40/// assert_eq!(Float::ZERO << 10, 0);
41/// assert_eq!(Float::INFINITY << 10, Float::INFINITY);
42/// assert_eq!(
43///     (Float::from(std::f64::consts::PI) << 10u8).to_string(),
44///     "3216.990877275948"
45/// );
46/// assert_eq!(
47///     (Float::from(std::f64::consts::PI) << -10i8).to_string(),
48///     "0.003067961575771282"
49/// );
50///
51/// assert_eq!(&Float::ZERO << 10, 0);
52/// assert_eq!(&Float::INFINITY << 10, Float::INFINITY);
53/// assert_eq!(
54///     (&Float::from(std::f64::consts::PI) << 10u8).to_string(),
55///     "3216.990877275948"
56/// );
57/// assert_eq!(
58///     (&Float::from(std::f64::consts::PI) << -10i8).to_string(),
59///     "0.003067961575771282"
60/// );
61/// ```
62///
63/// # shl_assign
64/// ```
65/// use malachite_base::num::basic::traits::{Infinity, Zero};
66/// use malachite_float::Float;
67///
68/// let mut x = Float::ZERO;
69/// x <<= 10;
70/// assert_eq!(x, 0);
71///
72/// let mut x = Float::INFINITY;
73/// x <<= 10;
74/// assert_eq!(x, Float::INFINITY);
75///
76/// let mut x = Float::from(std::f64::consts::PI);
77/// x <<= 10;
78/// assert_eq!(x.to_string(), "3216.990877275948");
79///
80/// let mut x = Float::from(std::f64::consts::PI);
81/// x <<= -10;
82/// assert_eq!(x.to_string(), "0.003067961575771282");
83/// ```
84pub mod shl;
85/// Implementations of [`ShlRound`](malachite_base::num::arithmetic::traits::ShlRound) and
86/// [`ShlRoundAssign`](malachite_base::num::arithmetic::traits::ShlRoundAssign), traits for
87/// multiplying a number by a power of 2 and rounding according to a specified
88/// [`RoundingMode`](malachite_base::rounding_modes::RoundingMode). For [`Float`](super::Float)s,
89/// rounding is only necessary in the cases of overflow and underflow.
90///
91/// # shl_round
92/// ```
93/// use malachite_base::num::arithmetic::traits::ShlRound;
94/// use malachite_base::rounding_modes::RoundingMode::*;
95/// use malachite_float::Float;
96/// use std::cmp::Ordering::*;
97///
98/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(10u8, Nearest);
99/// assert_eq!(shifted.to_string(), "3216.990877275948");
100/// assert_eq!(o, Equal);
101///
102/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(-10i8, Nearest);
103/// assert_eq!(shifted.to_string(), "0.003067961575771282");
104/// assert_eq!(o, Equal);
105///
106/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(u32::MAX, Floor);
107/// assert_eq!(shifted.to_string(), "too_big");
108/// assert_eq!(o, Less);
109///
110/// let (shifted, o) = Float::from(std::f64::consts::PI).shl_round(u32::MAX, Ceiling);
111/// assert_eq!(shifted.to_string(), "Infinity");
112/// assert_eq!(o, Greater);
113///
114/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(10u8, Nearest);
115/// assert_eq!(shifted.to_string(), "3216.990877275948");
116/// assert_eq!(o, Equal);
117///
118/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(-10i8, Nearest);
119/// assert_eq!(shifted.to_string(), "0.003067961575771282");
120/// assert_eq!(o, Equal);
121///
122/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(u32::MAX, Floor);
123/// assert_eq!(shifted.to_string(), "too_big");
124/// assert_eq!(o, Less);
125///
126/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shl_round(u32::MAX, Ceiling);
127/// assert_eq!(shifted.to_string(), "Infinity");
128/// assert_eq!(o, Greater);
129/// ```
130///
131/// # shl_assign
132/// ```
133/// use malachite_base::num::arithmetic::traits::ShlRoundAssign;
134/// use malachite_base::rounding_modes::RoundingMode::*;
135/// use malachite_float::Float;
136/// use std::cmp::Ordering::*;
137///
138/// let mut x = Float::from(std::f64::consts::PI);
139/// assert_eq!(x.shl_round_assign(10u8, Nearest), Equal);
140/// assert_eq!(x.to_string(), "3216.990877275948");
141///
142/// let mut x = Float::from(std::f64::consts::PI);
143/// assert_eq!(x.shl_round_assign(-10i8, Nearest), Equal);
144/// assert_eq!(x.to_string(), "0.003067961575771282");
145///
146/// let mut x = Float::from(std::f64::consts::PI);
147/// assert_eq!(x.shl_round_assign(u32::MAX, Floor), Less);
148/// assert_eq!(x.to_string(), "too_big");
149///
150/// let mut x = Float::from(std::f64::consts::PI);
151/// assert_eq!(x.shl_round_assign(u32::MAX, Ceiling), Greater);
152/// assert_eq!(x.to_string(), "Infinity");
153/// ```
154pub mod shl_round;
155/// Right-shifting a [`Float`](super::Float) (dividing it by a power of 2).
156///
157/// # shr
158/// ```
159/// use malachite_base::num::basic::traits::{Infinity, Zero};
160/// use malachite_float::Float;
161///
162/// assert_eq!(Float::ZERO >> 10, 0);
163/// assert_eq!(Float::INFINITY >> 10, Float::INFINITY);
164/// assert_eq!(
165///     (Float::from(std::f64::consts::PI) >> 10u8).to_string(),
166///     "0.003067961575771282"
167/// );
168/// assert_eq!(
169///     (Float::from(std::f64::consts::PI) >> -10i8).to_string(),
170///     "3216.990877275948"
171/// );
172///
173/// assert_eq!(&Float::ZERO >> 10, 0);
174/// assert_eq!(&Float::INFINITY >> 10, Float::INFINITY);
175/// assert_eq!(
176///     (&Float::from(std::f64::consts::PI) >> 10u8).to_string(),
177///     "0.003067961575771282"
178/// );
179/// assert_eq!(
180///     (&Float::from(std::f64::consts::PI) >> -10i8).to_string(),
181///     "3216.990877275948"
182/// );
183/// ```
184///
185/// # shr_assign
186/// ```
187/// use malachite_base::num::basic::traits::{Infinity, Zero};
188/// use malachite_float::Float;
189///
190/// let mut x = Float::ZERO;
191/// x >>= 10;
192/// assert_eq!(x, 0);
193///
194/// let mut x = Float::INFINITY;
195/// x >>= 10;
196/// assert_eq!(x, Float::INFINITY);
197///
198/// let mut x = Float::from(std::f64::consts::PI);
199/// x >>= 10;
200/// assert_eq!(x.to_string(), "0.003067961575771282");
201///
202/// let mut x = Float::from(std::f64::consts::PI);
203/// x >>= -10;
204/// assert_eq!(x.to_string(), "3216.990877275948");
205/// ```
206pub mod shr;
207/// Implementations of [`ShlRound`](malachite_base::num::arithmetic::traits::ShrRound) and
208/// [`ShrRoundAssign`](malachite_base::num::arithmetic::traits::ShrRoundAssign), traits for dividing
209/// a number by a power of 2 and rounding according to a specified
210/// [`RoundingMode`](malachite_base::rounding_modes::RoundingMode). For [`Float`](super::Float)s,
211/// rounding is only necessary in the cases of overflow and underflow.
212///
213/// # shr_round
214/// ```
215/// use malachite_base::num::arithmetic::traits::ShrRound;
216/// use malachite_base::rounding_modes::RoundingMode::*;
217/// use malachite_float::Float;
218/// use std::cmp::Ordering::*;
219///
220/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(10u8, Nearest);
221/// assert_eq!(shifted.to_string(), "0.003067961575771282");
222/// assert_eq!(o, Equal);
223///
224/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(-10i8, Nearest);
225/// assert_eq!(shifted.to_string(), "3216.990877275948");
226/// assert_eq!(o, Equal);
227///
228/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(u32::MAX, Floor);
229/// assert_eq!(shifted.to_string(), "0.0");
230/// assert_eq!(o, Less);
231///
232/// let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(u32::MAX, Ceiling);
233/// assert_eq!(shifted.to_string(), "too_small");
234/// assert_eq!(o, Greater);
235///
236/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(10u8, Nearest);
237/// assert_eq!(shifted.to_string(), "0.003067961575771282");
238/// assert_eq!(o, Equal);
239///
240/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(-10i8, Nearest);
241/// assert_eq!(shifted.to_string(), "3216.990877275948");
242/// assert_eq!(o, Equal);
243///
244/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(u32::MAX, Floor);
245/// assert_eq!(shifted.to_string(), "0.0");
246/// assert_eq!(o, Less);
247///
248/// let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(u32::MAX, Ceiling);
249/// assert_eq!(shifted.to_string(), "too_small");
250/// assert_eq!(o, Greater);
251/// ```
252///
253/// # shr_assign
254/// ```
255/// use malachite_base::num::arithmetic::traits::ShrRoundAssign;
256/// use malachite_base::rounding_modes::RoundingMode::*;
257/// use malachite_float::Float;
258/// use std::cmp::Ordering::*;
259///
260/// let mut x = Float::from(std::f64::consts::PI);
261/// assert_eq!(x.shr_round_assign(10u8, Nearest), Equal);
262/// assert_eq!(x.to_string(), "0.003067961575771282");
263///
264/// let mut x = Float::from(std::f64::consts::PI);
265/// assert_eq!(x.shr_round_assign(-10i8, Nearest), Equal);
266/// assert_eq!(x.to_string(), "3216.990877275948");
267///
268/// let mut x = Float::from(std::f64::consts::PI);
269/// assert_eq!(x.shr_round_assign(u32::MAX, Floor), Less);
270/// assert_eq!(x.to_string(), "0.0");
271///
272/// let mut x = Float::from(std::f64::consts::PI);
273/// assert_eq!(x.shr_round_assign(u32::MAX, Ceiling), Greater);
274/// assert_eq!(x.to_string(), "too_small");
275/// ```
276pub mod shr_round;
277/// An implementation of [`Sign`](malachite_base::num::arithmetic::traits::Sign), a trait for
278/// determining the sign of a number.
279pub mod sign;
280/// Squaring of [`Float`](super::Float)s.
281pub mod square;
282/// Subtraction of [`Float`](super::Float)s, of [`Float`](super::Float)s by
283/// [`Rational`](malachite_q::Rational)s, and of [`Rational`](malachite_q::Rational)s by
284/// [`Float`](super::Float)s.
285pub mod sub;