malachite_float/float/arithmetic/atan2.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5// Copyright 2005-2025 Free Software Foundation, Inc.
6//
7// Contributed by the Pascaline and Caramba projects, INRIA.
8//
9// This file is part of Malachite.
10//
11// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
12// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
13// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
14
15use crate::float::arithmetic::atan::{
16 atan_rational_helper, atan_with_period_rational_helper, scaled_unsigned,
17};
18use crate::float::arithmetic::sin::{SCALE, SCALED_INPUT_EXPONENT, scaled_underflow};
19use crate::{Float, emulate_float_float_to_float_fn, emulate_rational_rational_to_float_fn};
20use core::cmp::Ordering::{self, Equal, Greater, Less};
21use core::cmp::{max, min};
22use malachite_base::num::arithmetic::traits::{
23 Abs, AbsAssign, Atan2, Atan2Assign, CeilingLogBase2, IsPowerOf2,
24};
25use malachite_base::num::basic::floats::PrimitiveFloat;
26use malachite_base::num::basic::integers::PrimitiveInt;
27use malachite_base::num::basic::traits::{
28 NaN as NaNTrait, NegativeZero as NegativeZeroTrait, Zero as ZeroTrait,
29};
30use malachite_base::num::comparison::traits::{EqAbs, PartialOrdAbs};
31use malachite_base::num::conversion::traits::ExactFrom;
32use malachite_base::num::logic::traits::{SignificantBits, TrailingZeros};
33use malachite_base::rounding_modes::RoundingMode::{self, *};
34use malachite_nz::natural::arithmetic::float::round::float_can_round;
35use malachite_nz::platform::Limb;
36use malachite_q::Rational;
37
38// pi/2^i, negated when `neg`. This is pi_div_2ui from atan2.c, MPFR 4.2.2; the shift is exact, so
39// it does not disturb the ternary value.
40fn pi_div_2ui(i: u32, neg: bool, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
41 assert_ne!(rm, Exact, "Inexact atan2");
42 let (pi, o) = Float::pi_prec_round(prec, if neg { -rm } else { rm });
43 let q = pi >> i;
44 if neg { (-q, o.reverse()) } else { (q, o) }
45}
46
47// +-3 pi/4, for an infinite y over a negative infinite x. MPFR gives this its own Ziv loop, since
48// unlike the other quadrant boundaries it is not a power of 2 times pi.
49fn three_pi_over_4(neg: bool, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
50 assert_ne!(rm, Exact, "Inexact atan2");
51 let mut w = prec + 10;
52 let mut increment = Limb::WIDTH;
53 loop {
54 // error <= 2 ulps
55 let mut t = Float::pi_prec(w)
56 .0
57 .mul_prec(const { Float::const_from_unsigned(3) }, w)
58 .0;
59 t >>= 2u32;
60 if float_can_round(t.significand_ref().unwrap(), w - 2, prec, rm) {
61 let t = if neg { -t } else { t };
62 return Float::from_float_prec_round(t, prec, rm);
63 }
64 w += increment;
65 increment = w >> 1;
66 }
67}
68
69// The result of a computation that underflowed: a signed zero or the smallest positive `Float`, by
70// the rounding mode alone. This is mpfr_underflow from mpfr-impl.h, MPFR 4.2.2, where `Nearest`
71// rounds away from zero; the caller substitutes `Down` for the cases where it must not.
72fn underflow(positive: bool, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
73 let away = match rm {
74 Ceiling => positive,
75 Floor => !positive,
76 Up | Nearest => true,
77 _ => false,
78 };
79 let min_positive = Float::min_positive_value_prec(prec);
80 match (positive, away) {
81 (true, true) => (min_positive, Greater),
82 (true, false) => (Float::ZERO, Less),
83 (false, true) => (-min_positive, Less),
84 (false, false) => (Float::NEGATIVE_ZERO, Greater),
85 }
86}
87
88// Whether |y/x| is below 2^(MIN_EXPONENT - 1), the smallest positive `Float`, so that the quotient
89// underflows. MPFR reads this off the division's underflow flag; its exponent range is wide enough
90// that the case never arises for representable inputs, while here it does.
91//
92// |y/x| = (my/mx) 2^d, where d is the difference of the exponents and my and mx, the significands,
93// both lie in [1/2, 1). Only the middle binade needs the two significands compared, which the
94// shifts below do exactly.
95fn quotient_underflows(y: &Float, x: &Float, exp_y: i64, exp_x: i64) -> bool {
96 match (exp_y - exp_x).cmp(&(Float::MIN_EXPONENT_I64 - 1)) {
97 Less => true,
98 Greater => false,
99 Equal => (y >> exp_y).lt_abs(&(x >> exp_x)),
100 }
101}
102
103// atan2(y, x) when |y/x| is beyond the top of the exponent range, so that the quotient is not a
104// `Float`. MPFR widens its range for the whole computation and never meets this case; here the
105// arctangent has to be taken from its limit instead.
106//
107// For z > 0, pi/2 - 1/z < atan z < pi/2. With |y/x| > 2^k the result is therefore atan|y/x| = pi/2
108// - delta for x > 0, and pi - atan|y/x| = pi/2 + delta for x < 0, where 0 < delta < 2^-k: either
109// way it is pi/2 perturbed by less than 2^-k, carrying the sign of y. Since k is at least
110// MAX_EXPONENT - 1, that perturbation is far below the rounding error of pi itself at any usable
111// precision, and the loop below is the ordinary one for pi/2.
112fn atan2_huge_quotient(k: u64, negative: bool, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
113 let mut w = prec + 10;
114 let mut increment = Limb::WIDTH;
115 loop {
116 // |v - pi/2| <= 2^-w, and EXP(v) = 1, so v is good to min(w, k) - 1 bits once delta is
117 // counted too
118 let v = Float::pi_prec(w).0 >> 1u32;
119 if float_can_round(v.significand_ref().unwrap(), min(w, k) - 1, prec, rm) {
120 return Float::from_float_prec_round(if negative { -v } else { v }, prec, rm);
121 }
122 w += increment;
123 increment = w >> 1;
124 }
125}
126
127// Computes atan2(y, x) for finite nonzero y and x, rounded to precision `prec` with rounding mode
128// `rm`.
129//
130// This is mpfr_atan2 from atan2.c, MPFR 4.2.2, past the special cases.
131fn atan2_prec_round_normal_ref(
132 y: &Float,
133 x: &Float,
134 prec: u64,
135 rm: RoundingMode,
136) -> (Float, Ordering) {
137 assert_ne!(rm, Exact, "Inexact atan2");
138 let exp_y = i64::from(y.get_exponent().unwrap());
139 let exp_x = i64::from(x.get_exponent().unwrap());
140 let x_positive = *x > 0u32;
141 // When x is a power of two, y/x is exact, so atan takes it directly. The shift is exact only if
142 // it stays inside the exponent range, which MPFR checks through the division's flags.
143 if x_positive && x.significand_ref().unwrap().is_power_of_2() {
144 let shifted = exp_y - exp_x + 1;
145 if (Float::MIN_EXPONENT_I64..=Float::MAX_EXPONENT_I64).contains(&shifted) {
146 return (y >> (exp_x - 1)).atan_prec_round(prec, rm);
147 }
148 }
149 let y_negative = *y < 0u32;
150 // |y/x| lies in (2^(d - 1), 2^(d + 1)), so a d this large puts it beyond the top of the range
151 if exp_y - exp_x >= Float::MAX_EXPONENT_I64 {
152 return atan2_huge_quotient(u64::exact_from(exp_y - exp_x - 1), y_negative, prec, rm);
153 }
154 let mut w = prec + 3 + prec.ceiling_log_base_2();
155 let mut increment = Limb::WIDTH;
156 if x_positive {
157 // atan2(y, x) = atan(y/x)
158 loop {
159 let (t, div_o) = y.div_prec_ref_ref(x, w);
160 if div_o == Equal {
161 // the quotient is exact, so its arctangent is the whole answer
162 return t.atan_prec_round(prec, rm);
163 }
164 // error <= 1 ulp, except on underflow or overflow
165 if quotient_underflows(y, x, exp_y, exp_x) {
166 // |atan z| < |z|, so an underflowing quotient gives an underflowing result. MPFR
167 // takes the sign from the quotient; in this branch x is positive, so it is the sign
168 // of y. With `Nearest` a quotient that rounded to zero is below a quarter of the
169 // smallest positive `Float`, and rounds toward zero rather than away.
170 let rm = if rm == Nearest && t == 0u32 { Down } else { rm };
171 return underflow(!y_negative, prec, rm);
172 }
173 // error <= 2 ulps, since |atan'| <= 1
174 let mut t = t;
175 t.atan_prec_assign(w);
176 if float_can_round(t.significand_ref().unwrap(), w - 2, prec, rm) {
177 return Float::from_float_prec_round(t, prec, rm);
178 }
179 w += increment;
180 increment = w >> 1;
181 }
182 }
183 // atan2(y, x) = sign(y) (pi - atan|y/x|)
184 loop {
185 // error <= 1 ulp
186 let mut t = y.div_prec_ref_ref(x, w).0.abs();
187 // error <= 2 ulps, since |atan'| <= 1
188 t.atan_prec_assign(w);
189 // error <= 1/2 ulp
190 let pi = Float::pi_prec(w).0;
191 // if the quotient was zero, so is its arctangent, and |y/x| was below 2^(MIN_EXPONENT - 1)
192 let e = if t == 0u32 {
193 Float::MIN_EXPONENT_I64 - 1
194 } else {
195 i64::from(t.get_exponent().unwrap())
196 };
197 let exp_pi = i64::from(pi.get_exponent().unwrap());
198 let t = pi.sub_prec(t, w).0;
199 let t = if y_negative { -t } else { t };
200 let exp_t = i64::from(t.get_exponent().unwrap());
201 // error(t) is at most (1/2 + 2^(EXP(pi) - EXP(t) - 1) + 2^(e - EXP(t) + 1)) ulps, and so at
202 // most 2^(max(max(EXP(pi) - EXP(t) - 1, e - EXP(t) + 1), -1) + 2) ulps
203 let e = max(max(exp_pi - exp_t - 1, e - exp_t + 1), -1) + 2;
204 if e < i64::exact_from(w)
205 && float_can_round(
206 t.significand_ref().unwrap(),
207 w - u64::exact_from(e),
208 prec,
209 rm,
210 )
211 {
212 return Float::from_float_prec_round(t, prec, rm);
213 }
214 w += increment;
215 increment = w >> 1;
216 }
217}
218
219// Computes atan2(y, x) for nonzero `Rational`s y and x, rounded to precision `prec` with rounding
220// mode `rm`. (The zero cases are handled by the caller.)
221//
222// The quotient y/x is exact here, so nothing corresponds to the `Float` case's division, its
223// underflow, or its overflow beyond the exponent range: `atan_rational_helper` already covers every
224// magnitude, including the two ends where the quotient is not a `Float` at all. Only the negative-x
225// reflection needs a loop of its own, and it is MPFR's, with the arctangent taken from the
226// `Rational` directly rather than from a rounded quotient.
227fn atan2_rational_prec_round_normal_ref(
228 y: &Rational,
229 x: &Rational,
230 prec: u64,
231 rm: RoundingMode,
232) -> (Float, Ordering) {
233 assert_ne!(rm, Exact, "Inexact atan2_rational");
234 let q = y / x;
235 if *x > 0u32 {
236 // atan2(y, x) = atan(y/x)
237 return atan_rational_helper(&q, prec, rm);
238 }
239 // atan2(y, x) = sign(y) (pi - atan|y/x|)
240 let y_negative = *y < 0u32;
241 let aq = q.abs();
242 let mut w = prec + 3 + prec.ceiling_log_base_2();
243 let mut increment = Limb::WIDTH;
244 loop {
245 // correctly rounded, so the error is at most 1/2 ulp
246 let t = atan_rational_helper(&aq, w, Nearest).0;
247 // error <= 1/2 ulp
248 let pi = Float::pi_prec(w).0;
249 let exp_pi = i64::from(pi.get_exponent().unwrap());
250 // if the arctangent underflowed to zero, |y/x| was below 2^(MIN_EXPONENT - 1)
251 let e = if t == 0u32 {
252 Float::MIN_EXPONENT_I64 - 1
253 } else {
254 i64::from(t.get_exponent().unwrap())
255 };
256 // pi - atan|y/x| lies in [pi/2, pi], so it is never zero and never cancels
257 let t = pi.sub_prec(t, w).0;
258 let t = if y_negative { -t } else { t };
259 let exp_t = i64::from(t.get_exponent().unwrap());
260 // the same bound as the `Float` case, which is conservative here since the arctangent is
261 // correctly rounded rather than two ulps out
262 let e = max(max(exp_pi - exp_t - 1, e - exp_t + 1), -1) + 2;
263 if e < i64::exact_from(w)
264 && float_can_round(
265 t.significand_ref().unwrap(),
266 w - u64::exact_from(e),
267 prec,
268 rm,
269 )
270 {
271 return Float::from_float_prec_round(t, prec, rm);
272 }
273 w += increment;
274 increment = w >> 1;
275 }
276}
277
278// The number of bits in MPFR's unsigned long, which bounds u.
279const ULSIZE: u64 = 64;
280// Wide enough to hold 3u exactly, and so u/2 and u/4 as well.
281const AUX_PREC: u64 = ULSIZE + 2;
282
283// z = s 3u 2^-k, with k between 1 and 3. This is mpfr_atan2u_aux2 from atan2u.c, MPFR 4.2.2.
284fn atan2u_aux2(u: u64, k: u32, positive: bool, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
285 // 3u needs at most ULSIZE + 2 bits, so t is exact
286 let t = Float::from_unsigned_prec_round(u, AUX_PREC, Exact)
287 .0
288 .mul_prec_round(const { Float::const_from_unsigned(3) }, AUX_PREC, Exact)
289 .0
290 >> k;
291 Float::from_float_prec_round(if positive { t } else { -t }, prec, rm)
292}
293
294// round(s (u/2 - eps)), where eps < 1/2 ulp(u/2). This is mpfr_atan2u_aux3.
295fn atan2u_aux3(u: u64, positive: bool, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
296 // exact, since the working precision is at least ULSIZE
297 let mut t = Float::from_unsigned_prec_round(u, max(prec + 2, ULSIZE), Exact).0 >> 1u32;
298 // u/2 - 1/4 ulp_p(u/2) <= t <= u/2 for p = prec, which makes t round like u/2 - eps
299 t.decrement();
300 Float::from_float_prec_round(if positive { t } else { -t }, prec, rm)
301}
302
303// round(sign(y) (u/4 - sign(x) eps)), where eps < 1/2 ulp(u/4). This is mpfr_atan2u_aux4.
304fn atan2u_aux4(
305 u: u64,
306 x_positive: bool,
307 y_positive: bool,
308 prec: u64,
309 rm: RoundingMode,
310) -> (Float, Ordering) {
311 let w = if prec > ULSIZE { prec + 2 } else { AUX_PREC };
312 // exact
313 let mut t = Float::from_unsigned_prec_round(u, w, Exact).0 >> 2u32;
314 if x_positive {
315 t.decrement();
316 } else {
317 t.increment();
318 }
319 Float::from_float_prec_round(if y_positive { t } else { -t }, prec, rm)
320}
321
322// atan2u(y, x, u) when |y/x| is below the bottom of the exponent range and x is positive.
323//
324// MPFR reaches this only when the result underflows too, and asserts as much; here a large u can
325// lift |y/x| u/(2 pi) back into the range, since Malachite's range is so much narrower. For a |y/x|
326// this small atan|y/x| is its own leading term, so the quotient is formed from the numerator scaled
327// up by 2^SCALE, exactly as `sin_with_period` and `atan_with_period_rational` do, and the underflow
328// that remains is decided by the rounding mode alone.
329fn atan2u_tiny(
330 y: &Float,
331 x: &Float,
332 u: u64,
333 positive: bool,
334 prec: u64,
335 rm: RoundingMode,
336) -> (Float, Ordering) {
337 // |y| 2^SCALE stays well inside the range: this branch needs EXP(y) <= EXP(x) + MIN_EXPONENT,
338 // and EXP(x) is at most MAX_EXPONENT = -MIN_EXPONENT, so EXP(y) is at most 1 x is positive in
339 // this branch, so the quotient carries the sign of y; keeping it here rather than taking
340 // absolute values is what makes `Up` mean away from zero and lets the rounding mode see the
341 // sign it must round with
342 let ys = y << SCALE;
343 let xa = x.clone();
344 let mut w = prec + prec.ceiling_log_base_2() + 10;
345 let mut increment = Limb::WIDTH;
346 let u_float = Float::from(u);
347 loop {
348 // rounded away from zero throughout, so each step is a relative 1 + theta with |theta| <=
349 // 2^(1 - w)
350 let mut t = ys.div_prec_round_ref_ref(&xa, w, Up).0;
351 t.mul_prec_round_assign_ref(&u_float, w, Up);
352 // 2 pi rounded toward zero, so that the quotient rounds away
353 let two_pi = Float::pi_prec_round(w, Down).0 << 1u32;
354 t.div_prec_round_assign(two_pi, w, Up);
355 if let Some(result) = scaled_underflow(&t, positive, prec, rm) {
356 return result;
357 }
358 let t = t >> SCALE;
359 if float_can_round(t.significand_ref().unwrap(), w - 4, prec, rm) {
360 return Float::from_float_prec_round(t, prec, rm);
361 }
362 w += increment;
363 increment = w >> 1;
364 }
365}
366
367// Computes atan2u(y, x, u) = atan2(y, x) u/(2 pi) for finite nonzero y and x with |y| != |x| and
368// nonzero u, rounded to precision `prec` with rounding mode `rm`.
369//
370// This is mpfr_atan2u from atan2u.c, MPFR 4.2.2, past the special cases.
371fn atan2_with_period_prec_round_normal_ref(
372 y: &Float,
373 x: &Float,
374 u: u64,
375 prec: u64,
376 rm: RoundingMode,
377) -> (Float, Ordering) {
378 assert_ne!(rm, Exact, "Inexact atan2_with_period");
379 let x_positive = *x > 0u32;
380 let y_positive = *y > 0u32;
381 // When |y/x| is extreme the result lies astronomically close to a quadrant boundary: u/4 as
382 // |y/x| grows without bound, and u/2 as it shrinks to nothing with x negative. If that boundary
383 // is also a rounding boundary at the target precision -- that is, if u is representable in prec
384 // + 1 bits, so that u/4 and u/2 are either representable or exactly halfway between two
385 // representable numbers -- the loop below cannot settle the rounding until its working
386 // precision passes |EXP(y) - EXP(x)|, which the exponent range allows to be about 2^31. The two
387 // helpers answer such cases directly.
388 //
389 // MPFR reaches those helpers only when the division returns zero or an infinity, which is a
390 // much rarer condition than the situation itself, so mpfr_atan2u hangs here; this is the one
391 // place where the port deliberately departs from its structure. Where u is not representable in
392 // prec + 1 bits the loop settles quickly, since the boundary then lies strictly inside a
393 // rounding interval.
394 let exp_y = i64::from(y.get_exponent().unwrap());
395 let exp_x = i64::from(x.get_exponent().unwrap());
396 let d = exp_y - exp_x;
397 let p = i64::exact_from(prec);
398 if i64::exact_from(u.significant_bits() - TrailingZeros::trailing_zeros(u)) <= p + 1 {
399 // |y/x| >= 2^(d - 1) and u/(2 pi) < 2^(EXP(u) - 2), so u/(2 pi |y/x|) is below half an ulp
400 // of u/4 once d >= p + 2
401 if d >= p + 2 {
402 return atan2u_aux4(u, x_positive, y_positive, prec, rm);
403 }
404 // |y/x| < 2^(d + 1), so atanu(|y/x|) is below half an ulp of u/2 once d <= -p - 1; for a
405 // negative x the result is then just below u/2. For a positive x it is just above zero,
406 // which the loop handles, since there the limit is approached relatively rather than
407 // absolutely.
408 if !x_positive && d < -p {
409 return atan2u_aux3(u, y_positive, prec, rm);
410 }
411 }
412 // The periodic arctangent underflows for a tiny quotient with a small u, which MPFR, computing
413 // inside a temporarily extended exponent range, never sees. This is decided from the exponents
414 // rather than from the computed value: an arctangent that rounded up to the smallest positive
415 // `Float` is not zero, so a test on the value misses it, and no working precision can ever
416 // certify it, so the loop below would spin forever. The bound is the one `sin_with_period`
417 // scales at; past it |y/x| is above 2^(MIN_EXPONENT + 65), whose arctangent in u ths of a turn
418 // is far clear of the bottom.
419 if d <= SCALED_INPUT_EXPONENT {
420 return if x_positive {
421 atan2u_tiny(y, x, u, y_positive, prec, rm)
422 } else {
423 // u/2 minus a quantity this small rounds like u/2 stepped one ulp toward zero, whether
424 // or not u/2 lies on a rounding boundary
425 atan2u_aux3(u, y_positive, prec, rm)
426 };
427 }
428 let log_u = u.ceiling_log_base_2();
429 let mut w = prec + prec.ceiling_log_base_2() + 10;
430 let mut increment = Limb::WIDTH;
431 loop {
432 // In atan2pi units the four quadrants are [0, 1/2], [1/2, 1], [-1, -1/2] and [-1/2, 0];
433 // here they are [0, u/4], [u/4, u/2], [-u/2, -u/4] and [-u/4, 0].
434 let t = y.div_prec_ref_ref(x, w).0;
435 // the quotient can still overflow, which MPFR's range does not let it do
436 if !t.is_finite() {
437 return atan2u_aux4(u, x_positive, y_positive, prec, rm);
438 }
439 let mut t = t;
440 t.abs_assign();
441 let exp_t = i64::from(t.get_exponent().unwrap());
442 // |t - |y/x|| <= e1 := 1/2 ulp(t) = 2^(exp_t - w - 1)
443 t.atan_with_period_prec_assign(u, w);
444 // the derivative of atanu(s) is u/(1 + s^2)/(2 pi), so the new t is within 1/2 ulp(t) + e1
445 // u/(1 + s^2)/4 of atanu(|y/x|)
446 let e = if exp_t < 1 { 0 } else { exp_t - 1 };
447 // max(1, |t|) >= 2^e, so 1/(1 + t^2) <= 2^(-2 e)
448 let mut e = exp_t - (e << 1) + i64::exact_from(log_u) - 2;
449 // now e1 u/(1 + t^2)/4 <= 2^(e - w - 1), so |t - atanu(y/x)| <= 2^(e - w)
450 let mut exp_t = i64::from(t.get_exponent().unwrap());
451 e = max(e, exp_t);
452 if !x_positive {
453 // compute u/2 - t
454 t <<= 1u32; // error <= 2^(e + 1 - w)
455 t = Float::from(u).sub_prec(t, w).0;
456 exp_t = i64::from(t.get_exponent().unwrap());
457 // error <= 2^(exp_t - w - 1) + 2^(e + 1 - w)
458 e = max(exp_t - 1, e + 1);
459 // error <= 2^(e + 1 - w)
460 t >>= 1u32;
461 // error <= 2^(e - w)
462 exp_t = i64::from(t.get_exponent().unwrap());
463 }
464 // either way the error is at most 2^(e - w); expressed relative to t, that is 2^(exp_t - w
465 // + err) with err = e - exp_t
466 e -= exp_t;
467 // atan2u is odd with respect to y
468 let t = if y_positive { t } else { -t };
469 // a negative e claims better than half-ulp accuracy, which cannot beat t's own precision
470 let err = min(i64::exact_from(w), i64::exact_from(w) - e);
471 if err > 0 && float_can_round(t.significand_ref().unwrap(), u64::exact_from(err), prec, rm)
472 {
473 return Float::from_float_prec_round(t, prec, rm);
474 }
475 w += increment;
476 increment = w >> 1;
477 }
478}
479
480// Computes atan2u(y, x, u) = atan2(y, x) u/(2 pi) for nonzero `Rational`s y and x with |y| != |x|
481// and nonzero u, rounded to precision `prec` with rounding mode `rm`. (The rest is handled by the
482// caller.)
483//
484// The quotient y/x is exact here, so nothing corresponds to the `Float` case's division or to its
485// underflow and overflow: for a positive x the whole computation is the `Rational` arctangent in u
486// ths of a turn, which already covers every magnitude. Only the negative-x reflection needs a loop,
487// and it is MPFR's, with the arctangent taken from the `Rational` directly.
488fn atan2_with_period_rational_prec_round_normal_ref(
489 y: &Rational,
490 x: &Rational,
491 u: u64,
492 prec: u64,
493 rm: RoundingMode,
494) -> (Float, Ordering) {
495 assert_ne!(rm, Exact, "Inexact atan2_with_period_rational");
496 let q = y / x;
497 if *x > 0u32 {
498 // atan2u(y, x, u) = atanu(y/x, u)
499 return atan_with_period_rational_helper(&q, u, prec, rm);
500 }
501 // atan2u(y, x, u) = sign(y) (u/2 - atanu(|y/x|, u))
502 let y_positive = *y > 0u32;
503 let aq = q.abs();
504 let d = aq.floor_log_base_2_abs() + 1;
505 let p = i64::exact_from(prec);
506 // An arctangent this small underflows, and would leave the loop below with a value it can never
507 // certify; u/2 minus it rounds like u/2 stepped one ulp toward zero either way.
508 if d <= SCALED_INPUT_EXPONENT {
509 return atan2u_aux3(u, y_positive, prec, rm);
510 }
511 // As in the `Float` case, an extreme quotient puts the result astronomically close to a
512 // quadrant boundary, which the loop cannot settle when that boundary is also a rounding
513 // boundary. Here |y/x| growing takes the result to u/4 from above, and |y/x| shrinking takes it
514 // to u/2 from below.
515 if i64::exact_from(u.significant_bits() - TrailingZeros::trailing_zeros(u)) <= p + 1 {
516 if d >= p + 2 {
517 return atan2u_aux4(u, false, y_positive, prec, rm);
518 }
519 if d < -p {
520 return atan2u_aux3(u, y_positive, prec, rm);
521 }
522 }
523 let mut w = prec + prec.ceiling_log_base_2() + 10;
524 let mut increment = Limb::WIDTH;
525 loop {
526 // correctly rounded, so the error is under an ulp: e below is EXP(t), which states it as
527 // 2^(e - w)
528 let t = atan_with_period_rational_helper(&aq, u, w, Nearest).0;
529 let mut e = i64::from(t.get_exponent().unwrap());
530 // u/2 - t, formed as (u - 2 t)/2 so that u stays an integer
531 let t = Float::from(u).sub_prec(t << 1u32, w).0;
532 let exp_t = i64::from(t.get_exponent().unwrap());
533 // error <= 2^(exp_t - w - 1) + 2^(e + 1 - w) <= 2^(e + 1 - w) for the e below
534 e = max(exp_t - 1, e + 1);
535 let t = t >> 1u32;
536 let exp_t = i64::from(t.get_exponent().unwrap());
537 // the error is at most 2^(e - w); relative to t that is 2^(exp_t - w + err)
538 e -= exp_t;
539 // atan2u is odd with respect to y
540 let t = if y_positive { t } else { -t };
541 let err = min(i64::exact_from(w), i64::exact_from(w) - e);
542 if err > 0 && float_can_round(t.significand_ref().unwrap(), u64::exact_from(err), prec, rm)
543 {
544 return Float::from_float_prec_round(t, prec, rm);
545 }
546 w += increment;
547 increment = w >> 1;
548 }
549}
550
551// A signed zero, exactly.
552const fn signed_zero(negative: bool) -> (Float, Ordering) {
553 (
554 if negative {
555 Float::NEGATIVE_ZERO
556 } else {
557 Float::ZERO
558 },
559 Equal,
560 )
561}
562
563impl Float {
564 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
565 /// positive $x$-axis, rounding the result to the specified precision and with the specified
566 /// rounding mode. The [`Float`]s are both taken by reference. An [`Ordering`] is also returned,
567 /// indicating whether the rounded angle is less than, equal to, or greater than the exact
568 /// angle. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
569 /// `NaN` it also returns `Equal`.
570 ///
571 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
572 /// complexity; this function behaves the same way.
573 ///
574 /// # Panics
575 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
576 /// with the given precision (which is the case unless the result is a zero).
577 ///
578 /// # Examples
579 /// ```
580 /// use malachite_base::num::basic::traits::{NegativeOne, One, Zero};
581 /// use malachite_base::rounding_modes::RoundingMode::*;
582 /// use malachite_float::Float;
583 /// use std::cmp::Ordering::*;
584 ///
585 /// let (t, o) = (&Float::ONE).atan2_prec_round_ref_ref(&Float::ONE, 10, Floor);
586 /// assert_eq!(t.to_string(), "0.78516");
587 /// assert_eq!(o, Less);
588 ///
589 /// // a negative x with a zero y is half a turn
590 /// let (t, o) = (&Float::ZERO).atan2_prec_round_ref_ref(&Float::NEGATIVE_ONE, 10, Floor);
591 /// assert_eq!(t.to_string(), "3.1406");
592 /// assert_eq!(o, Less);
593 /// ```
594 pub fn atan2_prec_round_ref_ref(
595 &self,
596 other: &Self,
597 prec: u64,
598 rm: RoundingMode,
599 ) -> (Self, Ordering) {
600 assert_ne!(prec, 0);
601 let (y, x) = (self, other);
602 // atan2 is NaN if either argument is
603 if y.is_nan() || x.is_nan() {
604 return (Self::NAN, Equal);
605 }
606 // the quadrant is chosen by the sign bits, so a signed zero behaves like a signed number
607 let y_negative = y.is_sign_negative();
608 let x_negative = x.is_sign_negative();
609 // atan2(+-0, x) = +-pi for x < 0 (or -0.0), and +-0 for x > 0 (or +0.0)
610 if *y == 0u32 {
611 return if x_negative {
612 pi_div_2ui(0, y_negative, prec, rm)
613 } else {
614 signed_zero(y_negative)
615 };
616 }
617 // atan2(y, +-0) = +-pi/2, with the sign of y
618 if *x == 0u32 {
619 return pi_div_2ui(1, y_negative, prec, rm);
620 }
621 if !y.is_finite() {
622 // atan2(+-infinity, x) = +-pi/2 for finite x, +-pi/4 for +infinity, +-3pi/4 for
623 // -infinity
624 return if x.is_finite() {
625 pi_div_2ui(1, y_negative, prec, rm)
626 } else if x_negative {
627 three_pi_over_4(y_negative, prec, rm)
628 } else {
629 pi_div_2ui(2, y_negative, prec, rm)
630 };
631 }
632 // atan2(+-y, -infinity) = +-pi, atan2(+-y, +infinity) = +-0, for finite nonzero y
633 if !x.is_finite() {
634 return if x_negative {
635 pi_div_2ui(0, y_negative, prec, rm)
636 } else {
637 signed_zero(y_negative)
638 };
639 }
640 atan2_prec_round_normal_ref(y, x, prec, rm)
641 }
642
643 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
644 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
645 /// and with the specified rounding mode. The [`Float`]s are both taken by reference. An
646 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
647 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
648 /// whenever this function returns a `NaN` it also returns `Equal`.
649 ///
650 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
651 /// underflow, and the complexity; this function behaves the same way.
652 ///
653 /// # Panics
654 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
655 /// with the given precision.
656 ///
657 /// # Examples
658 /// ```
659 /// use malachite_base::num::basic::traits::{One, Two};
660 /// use malachite_base::rounding_modes::RoundingMode::*;
661 /// use malachite_float::Float;
662 /// use std::cmp::Ordering::*;
663 ///
664 /// // an eighth of a turn
665 /// let (t, o) =
666 /// (&Float::ONE).atan2_with_period_prec_round_ref_ref(&Float::ONE, 360, 10, Exact);
667 /// assert_eq!(t.to_string(), "45.000");
668 /// assert_eq!(o, Equal);
669 ///
670 /// let (t, o) =
671 /// (&Float::ONE).atan2_with_period_prec_round_ref_ref(&Float::TWO, 360, 10, Floor);
672 /// assert_eq!(t.to_string(), "26.562");
673 /// assert_eq!(o, Less);
674 /// ```
675 pub fn atan2_with_period_prec_round_ref_ref(
676 &self,
677 other: &Self,
678 u: u64,
679 prec: u64,
680 rm: RoundingMode,
681 ) -> (Self, Ordering) {
682 assert_ne!(prec, 0);
683 let (y, x) = (self, other);
684 // atan2u is NaN if either argument is
685 if y.is_nan() || x.is_nan() {
686 return (Self::NAN, Equal);
687 }
688 // the quadrant is chosen by the sign bits, so a signed zero behaves like a signed number
689 let y_positive = y.is_sign_positive();
690 let x_positive = x.is_sign_positive();
691 if !x.is_finite() {
692 if !y.is_finite() {
693 return if x_positive {
694 // atan2u(+-infinity, +infinity, u) = +-u/8
695 scaled_unsigned(u, 3, y_positive, prec, rm)
696 } else {
697 // atan2u(+-infinity, -infinity, u) = +-3u/8
698 atan2u_aux2(u, 3, y_positive, prec, rm)
699 };
700 }
701 // atan2u(+-y, -infinity, u) = +-u/2 and atan2u(+-y, +infinity, u) = +-0, which are also
702 // the IEEE 754-2019 answers for a zero y against a nonzero x
703 return if x_positive {
704 signed_zero(!y_positive)
705 } else {
706 scaled_unsigned(u, 1, y_positive, prec, rm)
707 };
708 }
709 // atan2u(+-infinity, x, u) = +-u/4 for a finite x
710 if !y.is_finite() {
711 return scaled_unsigned(u, 2, y_positive, prec, rm);
712 }
713 if *y == 0u32 {
714 return if x_positive {
715 // atan2u(+-0.0, x, u) = +-0.0 for a positive-signed x
716 signed_zero(!y_positive)
717 } else {
718 // atan2u(+-0.0, x, u) = +-u/2 for a negative-signed x
719 scaled_unsigned(u, 1, y_positive, prec, rm)
720 };
721 }
722 // atan2u(y, +-0.0, u) = +-u/4, with the sign of y
723 if *x == 0u32 {
724 return scaled_unsigned(u, 2, y_positive, prec, rm);
725 }
726 // |y| = |x| puts the angle on a quadrant diagonal, an exact eighth or three eighths of a
727 // turn
728 if y.eq_abs(x) {
729 return if x_positive {
730 scaled_unsigned(u, 3, y_positive, prec, rm)
731 } else {
732 atan2u_aux2(u, 3, y_positive, prec, rm)
733 };
734 }
735 // Every angle measures zero units when the whole turn does. MPFR returns +-1 here for a
736 // negative x, which disagrees with its own definition, with the formula it uses for that
737 // quadrant (u/2 - atanu, which is 0 - 0), and with the branches above, all of which return
738 // zero for u = 0.
739 if u == 0 {
740 return signed_zero(!y_positive);
741 }
742 atan2_with_period_prec_round_normal_ref(y, x, u, prec, rm)
743 }
744
745 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
746 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
747 /// and with the specified rounding mode. The [`Float`]s are both taken by value. An
748 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
749 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
750 /// whenever this function returns a `NaN` it also returns `Equal`.
751 ///
752 /// See [`RoundingMode`] for a description of the possible rounding modes.
753 ///
754 /// $$
755 /// f(y,x,u,p,m) = \operatorname{atan2}(y,x)u/(2\pi)+\varepsilon.
756 /// $$
757 /// - If $y$ or $x$ is NaN, or the result is one of the exact cases below, $\varepsilon$ may be
758 /// ignored or assumed to be 0.
759 /// - Otherwise, if $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
760 /// |\operatorname{atan2}(y,x)u/(2\pi)|\rfloor-p+1}$.
761 /// - Otherwise, if $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
762 /// |\operatorname{atan2}(y,x)u/(2\pi)|\rfloor-p}$.
763 ///
764 /// Special cases, in which the sign of a zero argument selects the quadrant:
765 /// - $f(\text{NaN},x,u,p,m)=f(y,\text{NaN},u,p,m)=\text{NaN}$
766 /// - $f(\pm\infty,+\infty,u,p,m)=\pm u/8$ and $f(\pm\infty,-\infty,u,p,m)=\pm3u/8$
767 /// - $f(\pm\infty,x,u,p,m)=\pm u/4$ for finite $x$
768 /// - $f(y,+\infty,u,p,m)=\pm0.0$ and $f(y,-\infty,u,p,m)=\pm u/2$, with the sign of $y$
769 /// - $f(\pm0.0,x,u,p,m)=\pm0.0$ if $x$ is positive or $+0.0$, and $\pm u/2$ if $x$ is negative
770 /// or $-0.0$
771 /// - $f(y,\pm0.0,u,p,m)=\pm u/4$, with the sign of $y$, for nonzero $y$
772 /// - $f(\pm x,x,u,p,m)=\pm u/8$ for positive $x$, and $\pm3u/8$ for negative $x$
773 /// - $f(y,x,0,p,m)=\pm0.0$, with the sign of $y$
774 ///
775 /// These are the only exact cases, and the turn fractions are exact only when $p$ is large
776 /// enough to hold them.
777 ///
778 /// The last is a deliberate divergence from MPFR, whose `mpfr_atan2u` returns $\pm1$ for a
779 /// negative $x$ when $u$ is zero. That disagrees with the function's own definition, with the
780 /// formula MPFR uses for that quadrant, and with MPFR's own answers when $y$ is zero or
781 /// infinite or $|y|=|x|$, all of which are zero.
782 ///
783 /// Overflow is not possible, since $|f(y,x,u,p,m)| \leq u/2 < 2^{63}$. The result underflows
784 /// only for a positive $x$ with $|y/x|$ tiny and $u$ small, where it is about $yu/(2\pi x)$.
785 ///
786 /// If the output has a precision, it is `prec`.
787 ///
788 /// If you know you'll be using `Nearest`, consider using [`Float::atan2_with_period_prec`]
789 /// instead. If you know that your target precision is the precision of the inputs, consider
790 /// using [`Float::atan2_with_period_round`] instead.
791 ///
792 /// # Worst-case complexity
793 /// $T(n, m) = O(n (\log n)^3 \log\log n + m (\log m)^2 \log\log m)$
794 ///
795 /// $M(n, m) = O(n \log n + m \log m)$
796 ///
797 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
798 /// `max(self.significant_bits(), other.significant_bits())`: the quotient is formed at a
799 /// working precision of about $n$ bits and its periodic arctangent taken there, which costs the
800 /// first term; the second covers the inputs. The magnitudes of the inputs do not drive the
801 /// cost.
802 ///
803 /// # Panics
804 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
805 /// with the given precision.
806 ///
807 /// # Examples
808 /// ```
809 /// use malachite_base::num::basic::traits::{One, Two};
810 /// use malachite_base::rounding_modes::RoundingMode::*;
811 /// use malachite_float::Float;
812 /// use std::cmp::Ordering::*;
813 ///
814 /// // an eighth of a turn
815 /// let (t, o) = Float::ONE.atan2_with_period_prec_round(Float::ONE, 360, 10, Exact);
816 /// assert_eq!(t.to_string(), "45.000");
817 /// assert_eq!(o, Equal);
818 ///
819 /// let (t, o) = Float::ONE.atan2_with_period_prec_round(Float::TWO, 360, 10, Floor);
820 /// assert_eq!(t.to_string(), "26.562");
821 /// assert_eq!(o, Less);
822 /// ```
823 #[inline]
824 #[allow(clippy::needless_pass_by_value)]
825 pub fn atan2_with_period_prec_round(
826 self,
827 other: Self,
828 u: u64,
829 prec: u64,
830 rm: RoundingMode,
831 ) -> (Self, Ordering) {
832 self.atan2_with_period_prec_round_ref_ref(&other, u, prec, rm)
833 }
834
835 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
836 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
837 /// and with the specified rounding mode. The first [`Float`] is taken by value and the second
838 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded angle is less
839 /// than, equal to, or greater than the exact angle. Although `NaN`s are not comparable to any
840 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
841 ///
842 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
843 /// underflow, and the complexity; this function behaves the same way.
844 ///
845 /// # Panics
846 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
847 /// with the given precision.
848 ///
849 /// # Examples
850 /// ```
851 /// use malachite_base::num::basic::traits::{One, Two};
852 /// use malachite_base::rounding_modes::RoundingMode::*;
853 /// use malachite_float::Float;
854 /// use std::cmp::Ordering::*;
855 ///
856 /// // an eighth of a turn
857 /// let (t, o) = Float::ONE.atan2_with_period_prec_round_val_ref(&Float::ONE, 360, 10, Exact);
858 /// assert_eq!(t.to_string(), "45.000");
859 /// assert_eq!(o, Equal);
860 ///
861 /// let (t, o) = Float::ONE.atan2_with_period_prec_round_val_ref(&Float::TWO, 360, 10, Floor);
862 /// assert_eq!(t.to_string(), "26.562");
863 /// assert_eq!(o, Less);
864 /// ```
865 #[inline]
866 #[allow(clippy::needless_pass_by_value)]
867 pub fn atan2_with_period_prec_round_val_ref(
868 self,
869 other: &Self,
870 u: u64,
871 prec: u64,
872 rm: RoundingMode,
873 ) -> (Self, Ordering) {
874 self.atan2_with_period_prec_round_ref_ref(other, u, prec, rm)
875 }
876
877 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
878 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
879 /// and with the specified rounding mode. The first [`Float`] is taken by reference and the
880 /// second by value. An [`Ordering`] is also returned, indicating whether the rounded angle is
881 /// less than, equal to, or greater than the exact angle. Although `NaN`s are not comparable to
882 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
883 ///
884 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
885 /// underflow, and the complexity; this function behaves the same way.
886 ///
887 /// # Panics
888 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
889 /// with the given precision.
890 ///
891 /// # Examples
892 /// ```
893 /// use malachite_base::num::basic::traits::{One, Two};
894 /// use malachite_base::rounding_modes::RoundingMode::*;
895 /// use malachite_float::Float;
896 /// use std::cmp::Ordering::*;
897 ///
898 /// // an eighth of a turn
899 /// let (t, o) = (&Float::ONE).atan2_with_period_prec_round_ref_val(Float::ONE, 360, 10, Exact);
900 /// assert_eq!(t.to_string(), "45.000");
901 /// assert_eq!(o, Equal);
902 ///
903 /// let (t, o) = (&Float::ONE).atan2_with_period_prec_round_ref_val(Float::TWO, 360, 10, Floor);
904 /// assert_eq!(t.to_string(), "26.562");
905 /// assert_eq!(o, Less);
906 /// ```
907 #[inline]
908 #[allow(clippy::needless_pass_by_value)]
909 pub fn atan2_with_period_prec_round_ref_val(
910 &self,
911 other: Self,
912 u: u64,
913 prec: u64,
914 rm: RoundingMode,
915 ) -> (Self, Ordering) {
916 self.atan2_with_period_prec_round_ref_ref(&other, u, prec, rm)
917 }
918
919 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
920 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
921 /// specified precision. The [`Float`]s are both taken by value. An [`Ordering`] is also
922 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
923 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
924 /// returns a `NaN` it also returns `Equal`.
925 ///
926 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
927 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
928 /// the `Nearest` rounding mode.
929 ///
930 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
931 /// underflow, and the complexity; this function is that one with `Nearest`.
932 ///
933 /// If you want to use a rounding mode other than `Nearest`, consider using
934 /// [`Float::atan2_with_period_prec_round`] instead.
935 ///
936 /// # Panics
937 /// Panics if `prec` is zero.
938 ///
939 /// # Examples
940 /// ```
941 /// use malachite_base::num::basic::traits::{One, Two};
942 /// use malachite_float::Float;
943 /// use std::cmp::Ordering::*;
944 ///
945 /// let (t, o) = Float::ONE.atan2_with_period_prec(Float::TWO, 360, 10);
946 /// assert_eq!(t.to_string(), "26.562");
947 /// assert_eq!(o, Less);
948 /// ```
949 #[inline]
950 #[allow(clippy::needless_pass_by_value)]
951 pub fn atan2_with_period_prec(self, other: Self, u: u64, prec: u64) -> (Self, Ordering) {
952 self.atan2_with_period_prec_ref_ref(&other, u, prec)
953 }
954
955 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
956 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
957 /// specified precision. The first [`Float`] is taken by value and the second by reference. An
958 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
959 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
960 /// whenever this function returns a `NaN` it also returns `Equal`.
961 ///
962 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
963 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
964 /// the `Nearest` rounding mode.
965 ///
966 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
967 /// underflow, and the complexity; this function is that one with `Nearest`.
968 ///
969 /// If you want to use a rounding mode other than `Nearest`, consider using
970 /// [`Float::atan2_with_period_prec_round`] instead.
971 ///
972 /// # Panics
973 /// Panics if `prec` is zero.
974 ///
975 /// # Examples
976 /// ```
977 /// use malachite_base::num::basic::traits::{One, Two};
978 /// use malachite_float::Float;
979 /// use std::cmp::Ordering::*;
980 ///
981 /// let (t, o) = Float::ONE.atan2_with_period_prec_val_ref(&Float::TWO, 360, 10);
982 /// assert_eq!(t.to_string(), "26.562");
983 /// assert_eq!(o, Less);
984 /// ```
985 #[inline]
986 #[allow(clippy::needless_pass_by_value)]
987 pub fn atan2_with_period_prec_val_ref(
988 self,
989 other: &Self,
990 u: u64,
991 prec: u64,
992 ) -> (Self, Ordering) {
993 self.atan2_with_period_prec_ref_ref(other, u, prec)
994 }
995
996 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
997 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
998 /// specified precision. The first [`Float`] is taken by reference and the second by value. An
999 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
1000 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
1001 /// whenever this function returns a `NaN` it also returns `Equal`.
1002 ///
1003 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1004 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1005 /// the `Nearest` rounding mode.
1006 ///
1007 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1008 /// underflow, and the complexity; this function is that one with `Nearest`.
1009 ///
1010 /// If you want to use a rounding mode other than `Nearest`, consider using
1011 /// [`Float::atan2_with_period_prec_round`] instead.
1012 ///
1013 /// # Panics
1014 /// Panics if `prec` is zero.
1015 ///
1016 /// # Examples
1017 /// ```
1018 /// use malachite_base::num::basic::traits::{One, Two};
1019 /// use malachite_float::Float;
1020 /// use std::cmp::Ordering::*;
1021 ///
1022 /// let (t, o) = (&Float::ONE).atan2_with_period_prec_ref_val(Float::TWO, 360, 10);
1023 /// assert_eq!(t.to_string(), "26.562");
1024 /// assert_eq!(o, Less);
1025 /// ```
1026 #[inline]
1027 #[allow(clippy::needless_pass_by_value)]
1028 pub fn atan2_with_period_prec_ref_val(
1029 &self,
1030 other: Self,
1031 u: u64,
1032 prec: u64,
1033 ) -> (Self, Ordering) {
1034 self.atan2_with_period_prec_ref_ref(&other, u, prec)
1035 }
1036
1037 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1038 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
1039 /// specified precision. The [`Float`]s are both taken by reference. An [`Ordering`] is also
1040 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
1041 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
1042 /// returns a `NaN` it also returns `Equal`.
1043 ///
1044 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1045 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1046 /// the `Nearest` rounding mode.
1047 ///
1048 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1049 /// underflow, and the complexity; this function is that one with `Nearest`.
1050 ///
1051 /// If you want to use a rounding mode other than `Nearest`, consider using
1052 /// [`Float::atan2_with_period_prec_round`] instead.
1053 ///
1054 /// # Panics
1055 /// Panics if `prec` is zero.
1056 ///
1057 /// # Examples
1058 /// ```
1059 /// use malachite_base::num::basic::traits::{One, Two};
1060 /// use malachite_float::Float;
1061 /// use std::cmp::Ordering::*;
1062 ///
1063 /// let (t, o) = (&Float::ONE).atan2_with_period_prec_ref_ref(&Float::TWO, 360, 10);
1064 /// assert_eq!(t.to_string(), "26.562");
1065 /// assert_eq!(o, Less);
1066 /// ```
1067 #[inline]
1068 pub fn atan2_with_period_prec_ref_ref(
1069 &self,
1070 other: &Self,
1071 u: u64,
1072 prec: u64,
1073 ) -> (Self, Ordering) {
1074 self.atan2_with_period_prec_round_ref_ref(other, u, prec, Nearest)
1075 }
1076
1077 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1078 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified rounding
1079 /// mode. The [`Float`]s are both taken by value. An [`Ordering`] is also returned, indicating
1080 /// whether the rounded angle is less than, equal to, or greater than the exact angle. Although
1081 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1082 /// returns `Equal`.
1083 ///
1084 /// The precision of the output is the maximum of the precisions of the inputs. See
1085 /// [`RoundingMode`] for a description of the possible rounding modes.
1086 ///
1087 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1088 /// underflow, and the complexity; this function is that one with `prec` the maximum input
1089 /// precision.
1090 ///
1091 /// If you want to specify the output precision, consider using
1092 /// [`Float::atan2_with_period_prec_round`] instead.
1093 ///
1094 /// # Panics
1095 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1096 /// the inputs.
1097 ///
1098 /// # Examples
1099 /// ```
1100 /// use malachite_base::rounding_modes::RoundingMode::*;
1101 /// use malachite_float::Float;
1102 /// use std::cmp::Ordering::*;
1103 ///
1104 /// let (t, o) = Float::from(0.3f64).atan2_with_period_round(Float::from(0.4f64), 360, Floor);
1105 /// assert_eq!(t.to_string(), "36.869897645844013");
1106 /// assert_eq!(o, Less);
1107 /// ```
1108 #[inline]
1109 #[allow(clippy::needless_pass_by_value)]
1110 pub fn atan2_with_period_round(
1111 self,
1112 other: Self,
1113 u: u64,
1114 rm: RoundingMode,
1115 ) -> (Self, Ordering) {
1116 let prec = max(self.significant_bits(), other.significant_bits());
1117 self.atan2_with_period_prec_round_ref_ref(&other, u, prec, rm)
1118 }
1119
1120 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1121 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified rounding
1122 /// mode. The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is
1123 /// also returned, indicating whether the rounded angle is less than, equal to, or greater than
1124 /// the exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
1125 /// returns a `NaN` it also returns `Equal`.
1126 ///
1127 /// The precision of the output is the maximum of the precisions of the inputs. See
1128 /// [`RoundingMode`] for a description of the possible rounding modes.
1129 ///
1130 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1131 /// underflow, and the complexity; this function is that one with `prec` the maximum input
1132 /// precision.
1133 ///
1134 /// If you want to specify the output precision, consider using
1135 /// [`Float::atan2_with_period_prec_round`] instead.
1136 ///
1137 /// # Panics
1138 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1139 /// the inputs.
1140 ///
1141 /// # Examples
1142 /// ```
1143 /// use malachite_base::rounding_modes::RoundingMode::*;
1144 /// use malachite_float::Float;
1145 /// use std::cmp::Ordering::*;
1146 ///
1147 /// let (t, o) =
1148 /// Float::from(0.3f64).atan2_with_period_round_val_ref(&Float::from(0.4f64), 360, Floor);
1149 /// assert_eq!(t.to_string(), "36.869897645844013");
1150 /// assert_eq!(o, Less);
1151 /// ```
1152 #[inline]
1153 #[allow(clippy::needless_pass_by_value)]
1154 pub fn atan2_with_period_round_val_ref(
1155 self,
1156 other: &Self,
1157 u: u64,
1158 rm: RoundingMode,
1159 ) -> (Self, Ordering) {
1160 let prec = max(self.significant_bits(), other.significant_bits());
1161 self.atan2_with_period_prec_round_ref_ref(other, u, prec, rm)
1162 }
1163
1164 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1165 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified rounding
1166 /// mode. The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is
1167 /// also returned, indicating whether the rounded angle is less than, equal to, or greater than
1168 /// the exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
1169 /// returns a `NaN` it also returns `Equal`.
1170 ///
1171 /// The precision of the output is the maximum of the precisions of the inputs. See
1172 /// [`RoundingMode`] for a description of the possible rounding modes.
1173 ///
1174 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1175 /// underflow, and the complexity; this function is that one with `prec` the maximum input
1176 /// precision.
1177 ///
1178 /// If you want to specify the output precision, consider using
1179 /// [`Float::atan2_with_period_prec_round`] instead.
1180 ///
1181 /// # Panics
1182 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1183 /// the inputs.
1184 ///
1185 /// # Examples
1186 /// ```
1187 /// use malachite_base::rounding_modes::RoundingMode::*;
1188 /// use malachite_float::Float;
1189 /// use std::cmp::Ordering::*;
1190 ///
1191 /// let (t, o) =
1192 /// (&Float::from(0.3f64)).atan2_with_period_round_ref_val(Float::from(0.4f64), 360, Floor);
1193 /// assert_eq!(t.to_string(), "36.869897645844013");
1194 /// assert_eq!(o, Less);
1195 /// ```
1196 #[inline]
1197 #[allow(clippy::needless_pass_by_value)]
1198 pub fn atan2_with_period_round_ref_val(
1199 &self,
1200 other: Self,
1201 u: u64,
1202 rm: RoundingMode,
1203 ) -> (Self, Ordering) {
1204 let prec = max(self.significant_bits(), other.significant_bits());
1205 self.atan2_with_period_prec_round_ref_ref(&other, u, prec, rm)
1206 }
1207
1208 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1209 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified rounding
1210 /// mode. The [`Float`]s are both taken by reference. An [`Ordering`] is also returned,
1211 /// indicating whether the rounded angle is less than, equal to, or greater than the exact
1212 /// angle. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
1213 /// `NaN` it also returns `Equal`.
1214 ///
1215 /// The precision of the output is the maximum of the precisions of the inputs. See
1216 /// [`RoundingMode`] for a description of the possible rounding modes.
1217 ///
1218 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1219 /// underflow, and the complexity; this function is that one with `prec` the maximum input
1220 /// precision.
1221 ///
1222 /// If you want to specify the output precision, consider using
1223 /// [`Float::atan2_with_period_prec_round`] instead.
1224 ///
1225 /// # Panics
1226 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1227 /// the inputs.
1228 ///
1229 /// # Examples
1230 /// ```
1231 /// use malachite_base::rounding_modes::RoundingMode::*;
1232 /// use malachite_float::Float;
1233 /// use std::cmp::Ordering::*;
1234 ///
1235 /// let y = Float::from(0.3f64);
1236 /// let x = Float::from(0.4f64);
1237 /// let (t, o) = (&y).atan2_with_period_round_ref_ref(&x, 360, Floor);
1238 /// assert_eq!(t.to_string(), "36.869897645844013");
1239 /// assert_eq!(o, Less);
1240 /// ```
1241 #[inline]
1242 pub fn atan2_with_period_round_ref_ref(
1243 &self,
1244 other: &Self,
1245 u: u64,
1246 rm: RoundingMode,
1247 ) -> (Self, Ordering) {
1248 let prec = max(self.significant_bits(), other.significant_bits());
1249 self.atan2_with_period_prec_round_ref_ref(other, u, prec, rm)
1250 }
1251
1252 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1253 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
1254 /// and with the specified rounding mode. The first [`Float`] is replaced by the result, and the
1255 /// second is taken by value. An [`Ordering`] is returned, indicating whether the rounded angle
1256 /// is less than, equal to, or greater than the exact angle.
1257 ///
1258 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1259 /// underflow, and the complexity; this function behaves the same way.
1260 ///
1261 /// # Panics
1262 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1263 /// with the given precision.
1264 ///
1265 /// # Examples
1266 /// ```
1267 /// use malachite_base::num::basic::traits::{One, Two};
1268 /// use malachite_base::rounding_modes::RoundingMode::*;
1269 /// use malachite_float::Float;
1270 /// use std::cmp::Ordering::*;
1271 ///
1272 /// let mut y = Float::ONE;
1273 /// assert_eq!(
1274 /// y.atan2_with_period_prec_round_assign(Float::TWO, 360, 10, Floor),
1275 /// Less
1276 /// );
1277 /// assert_eq!(y.to_string(), "26.562");
1278 /// ```
1279 #[inline]
1280 #[allow(clippy::needless_pass_by_value)]
1281 pub fn atan2_with_period_prec_round_assign(
1282 &mut self,
1283 other: Self,
1284 u: u64,
1285 prec: u64,
1286 rm: RoundingMode,
1287 ) -> Ordering {
1288 let (t, o) = self.atan2_with_period_prec_round_ref_ref(&other, u, prec, rm);
1289 *self = t;
1290 o
1291 }
1292
1293 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1294 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
1295 /// and with the specified rounding mode. The first [`Float`] is replaced by the result, and the
1296 /// second is taken by reference. An [`Ordering`] is returned, indicating whether the rounded
1297 /// angle is less than, equal to, or greater than the exact angle.
1298 ///
1299 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1300 /// underflow, and the complexity; this function behaves the same way.
1301 ///
1302 /// # Panics
1303 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1304 /// with the given precision.
1305 ///
1306 /// # Examples
1307 /// ```
1308 /// use malachite_base::num::basic::traits::{One, Two};
1309 /// use malachite_base::rounding_modes::RoundingMode::*;
1310 /// use malachite_float::Float;
1311 /// use std::cmp::Ordering::*;
1312 ///
1313 /// let mut y = Float::ONE;
1314 /// assert_eq!(
1315 /// y.atan2_with_period_prec_round_assign_ref(&Float::TWO, 360, 10, Floor),
1316 /// Less
1317 /// );
1318 /// assert_eq!(y.to_string(), "26.562");
1319 /// ```
1320 #[inline]
1321 pub fn atan2_with_period_prec_round_assign_ref(
1322 &mut self,
1323 other: &Self,
1324 u: u64,
1325 prec: u64,
1326 rm: RoundingMode,
1327 ) -> Ordering {
1328 let (t, o) = self.atan2_with_period_prec_round_ref_ref(other, u, prec, rm);
1329 *self = t;
1330 o
1331 }
1332
1333 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1334 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
1335 /// specified precision. The first [`Float`] is replaced by the result, and the second is taken
1336 /// by value. An [`Ordering`] is returned, indicating whether the rounded angle is less than,
1337 /// equal to, or greater than the exact angle.
1338 ///
1339 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1340 /// underflow, and the complexity; this function behaves the same way.
1341 ///
1342 /// # Panics
1343 /// Panics if `prec` is zero.
1344 ///
1345 /// # Examples
1346 /// ```
1347 /// use malachite_base::num::basic::traits::{One, Two};
1348 /// use malachite_float::Float;
1349 /// use std::cmp::Ordering::*;
1350 ///
1351 /// let mut y = Float::ONE;
1352 /// assert_eq!(y.atan2_with_period_prec_assign(Float::TWO, 360, 10), Less);
1353 /// assert_eq!(y.to_string(), "26.562");
1354 /// ```
1355 #[inline]
1356 #[allow(clippy::needless_pass_by_value)]
1357 pub fn atan2_with_period_prec_assign(&mut self, other: Self, u: u64, prec: u64) -> Ordering {
1358 let (t, o) = self.atan2_with_period_prec_ref_ref(&other, u, prec);
1359 *self = t;
1360 o
1361 }
1362
1363 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1364 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
1365 /// specified precision. The first [`Float`] is replaced by the result, and the second is taken
1366 /// by reference. An [`Ordering`] is returned, indicating whether the rounded angle is less
1367 /// than, equal to, or greater than the exact angle.
1368 ///
1369 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1370 /// underflow, and the complexity; this function behaves the same way.
1371 ///
1372 /// # Panics
1373 /// Panics if `prec` is zero.
1374 ///
1375 /// # Examples
1376 /// ```
1377 /// use malachite_base::num::basic::traits::{One, Two};
1378 /// use malachite_float::Float;
1379 /// use std::cmp::Ordering::*;
1380 ///
1381 /// let mut y = Float::ONE;
1382 /// assert_eq!(
1383 /// y.atan2_with_period_prec_assign_ref(&Float::TWO, 360, 10),
1384 /// Less
1385 /// );
1386 /// assert_eq!(y.to_string(), "26.562");
1387 /// ```
1388 #[inline]
1389 pub fn atan2_with_period_prec_assign_ref(
1390 &mut self,
1391 other: &Self,
1392 u: u64,
1393 prec: u64,
1394 ) -> Ordering {
1395 let (t, o) = self.atan2_with_period_prec_ref_ref(other, u, prec);
1396 *self = t;
1397 o
1398 }
1399
1400 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1401 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified rounding
1402 /// mode. The first [`Float`] is replaced by the result, and the second is taken by value. An
1403 /// [`Ordering`] is returned, indicating whether the rounded angle is less than, equal to, or
1404 /// greater than the exact angle.
1405 ///
1406 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1407 /// underflow, and the complexity; this function behaves the same way.
1408 ///
1409 /// # Panics
1410 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1411 /// the inputs.
1412 ///
1413 /// # Examples
1414 /// ```
1415 /// use malachite_base::rounding_modes::RoundingMode::*;
1416 /// use malachite_float::Float;
1417 /// use std::cmp::Ordering::*;
1418 ///
1419 /// let mut y = Float::from(0.3f64);
1420 /// assert_eq!(
1421 /// y.atan2_with_period_round_assign(Float::from(0.4f64), 360, Floor),
1422 /// Less
1423 /// );
1424 /// assert_eq!(y.to_string(), "36.869897645844013");
1425 /// ```
1426 #[inline]
1427 #[allow(clippy::needless_pass_by_value)]
1428 pub fn atan2_with_period_round_assign(
1429 &mut self,
1430 other: Self,
1431 u: u64,
1432 rm: RoundingMode,
1433 ) -> Ordering {
1434 let prec = max(self.significant_bits(), other.significant_bits());
1435 let (t, o) = self.atan2_with_period_prec_round_ref_ref(&other, u, prec, rm);
1436 *self = t;
1437 o
1438 }
1439
1440 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
1441 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified rounding
1442 /// mode. The first [`Float`] is replaced by the result, and the second is taken by reference.
1443 /// An [`Ordering`] is returned, indicating whether the rounded angle is less than, equal to, or
1444 /// greater than the exact angle.
1445 ///
1446 /// See [`Float::atan2_with_period_prec_round`] for the error bounds, the special cases,
1447 /// underflow, and the complexity; this function behaves the same way.
1448 ///
1449 /// # Panics
1450 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1451 /// the inputs.
1452 ///
1453 /// # Examples
1454 /// ```
1455 /// use malachite_base::rounding_modes::RoundingMode::*;
1456 /// use malachite_float::Float;
1457 /// use std::cmp::Ordering::*;
1458 ///
1459 /// let mut y = Float::from(0.3f64);
1460 /// assert_eq!(
1461 /// y.atan2_with_period_round_assign_ref(&Float::from(0.4f64), 360, Floor),
1462 /// Less
1463 /// );
1464 /// assert_eq!(y.to_string(), "36.869897645844013");
1465 /// ```
1466 #[inline]
1467 pub fn atan2_with_period_round_assign_ref(
1468 &mut self,
1469 other: &Self,
1470 u: u64,
1471 rm: RoundingMode,
1472 ) -> Ordering {
1473 let prec = max(self.significant_bits(), other.significant_bits());
1474 let (t, o) = self.atan2_with_period_prec_round_ref_ref(other, u, prec, rm);
1475 *self = t;
1476 o
1477 }
1478
1479 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1480 /// positive $x$-axis, rounding the result to the specified precision and with the specified
1481 /// rounding mode. The [`Float`]s are both taken by value. An [`Ordering`] is also returned,
1482 /// indicating whether the rounded angle is less than, equal to, or greater than the exact
1483 /// angle. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
1484 /// `NaN` it also returns `Equal`.
1485 ///
1486 /// See [`RoundingMode`] for a description of the possible rounding modes.
1487 ///
1488 /// $$
1489 /// f(y,x,p,m) = \operatorname{atan2}(y,x)+\varepsilon.
1490 /// $$
1491 /// - If $y$ or $x$ is NaN, or the result is a zero, $\varepsilon$ may be ignored or assumed to
1492 /// be 0.
1493 /// - Otherwise, if $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
1494 /// |\operatorname{atan2}(y,x)|\rfloor-p+1}$.
1495 /// - Otherwise, if $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
1496 /// |\operatorname{atan2}(y,x)|\rfloor-p}$.
1497 ///
1498 /// Special cases, in which the sign of a zero argument selects the quadrant:
1499 /// - $f(\text{NaN},x,p,m)=f(y,\text{NaN},p,m)=\text{NaN}$
1500 /// - $f(\pm0.0,x,p,m)=\pm0.0$ if $x$ is positive or $+0.0$
1501 /// - $f(\pm0.0,x,p,m)=\pm\pi$ if $x$ is negative or $-0.0$
1502 /// - $f(y,\pm0.0,p,m)=\pm\pi/2$, with the sign of $y$, for nonzero $y$
1503 /// - $f(\pm\infty,x,p,m)=\pm\pi/2$ for finite $x$
1504 /// - $f(\pm\infty,+\infty,p,m)=\pm\pi/4$
1505 /// - $f(\pm\infty,-\infty,p,m)=\pm3\pi/4$
1506 /// - $f(y,+\infty,p,m)=\pm0.0$, with the sign of $y$, for finite nonzero $y$
1507 /// - $f(y,-\infty,p,m)=\pm\pi$, with the sign of $y$, for finite nonzero $y$
1508 ///
1509 /// The zeros are the only exact cases; every other result is a nonzero multiple of $\pi$ or an
1510 /// arctangent, and so is irrational.
1511 ///
1512 /// Overflow is not possible, since $|\operatorname{atan2}(y,x)| \leq \pi$. The result
1513 /// underflows only for a positive $x$ with $|y/x|$ below $2^{-2^{30}}$, where it is about
1514 /// $y/x$; there $0.0$ or $\pm2^{-2^{30}}$ is returned instead, by the rounding mode alone.
1515 ///
1516 /// If the output has a precision, it is `prec`.
1517 ///
1518 /// If you know you'll be using `Nearest`, consider using [`Float::atan2_prec`] instead. If you
1519 /// know that your target precision is the precision of the inputs, consider using
1520 /// [`Float::atan2_round`] instead.
1521 ///
1522 /// # Worst-case complexity
1523 /// $T(n, m) = O(n (\log n)^3 \log\log n + m (\log m)^2 \log\log m)$
1524 ///
1525 /// $M(n, m) = O(n \log n + m \log m)$
1526 ///
1527 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1528 /// `max(self.significant_bits(), other.significant_bits())`: the quotient is formed at a
1529 /// working precision of about $n$ bits and its arctangent taken there, which costs the first
1530 /// term; the second covers the inputs. The magnitudes of the inputs do not drive the cost.
1531 ///
1532 /// # Panics
1533 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1534 /// with the given precision (which is the case unless the result is a zero).
1535 ///
1536 /// # Examples
1537 /// ```
1538 /// use malachite_base::num::basic::traits::{NegativeOne, One, Zero};
1539 /// use malachite_base::rounding_modes::RoundingMode::*;
1540 /// use malachite_float::Float;
1541 /// use std::cmp::Ordering::*;
1542 ///
1543 /// let (t, o) = Float::ONE.atan2_prec_round(Float::ONE, 10, Floor);
1544 /// assert_eq!(t.to_string(), "0.78516");
1545 /// assert_eq!(o, Less);
1546 ///
1547 /// // a negative x with a zero y is half a turn
1548 /// let (t, o) = Float::ZERO.atan2_prec_round(Float::NEGATIVE_ONE, 10, Floor);
1549 /// assert_eq!(t.to_string(), "3.1406");
1550 /// assert_eq!(o, Less);
1551 /// ```
1552 #[inline]
1553 #[allow(clippy::needless_pass_by_value)]
1554 pub fn atan2_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1555 self.atan2_prec_round_ref_ref(&other, prec, rm)
1556 }
1557
1558 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1559 /// positive $x$-axis, rounding the result to the specified precision and with the specified
1560 /// rounding mode. The first [`Float`] is taken by value and the second by reference. An
1561 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
1562 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
1563 /// whenever this function returns a `NaN` it also returns `Equal`.
1564 ///
1565 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1566 /// complexity; this function behaves the same way.
1567 ///
1568 /// # Panics
1569 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1570 /// with the given precision (which is the case unless the result is a zero).
1571 ///
1572 /// # Examples
1573 /// ```
1574 /// use malachite_base::num::basic::traits::{NegativeOne, One, Zero};
1575 /// use malachite_base::rounding_modes::RoundingMode::*;
1576 /// use malachite_float::Float;
1577 /// use std::cmp::Ordering::*;
1578 ///
1579 /// let (t, o) = Float::ONE.atan2_prec_round_val_ref(&Float::ONE, 10, Floor);
1580 /// assert_eq!(t.to_string(), "0.78516");
1581 /// assert_eq!(o, Less);
1582 ///
1583 /// // a negative x with a zero y is half a turn
1584 /// let (t, o) = Float::ZERO.atan2_prec_round_val_ref(&Float::NEGATIVE_ONE, 10, Floor);
1585 /// assert_eq!(t.to_string(), "3.1406");
1586 /// assert_eq!(o, Less);
1587 /// ```
1588 #[inline]
1589 #[allow(clippy::needless_pass_by_value)]
1590 pub fn atan2_prec_round_val_ref(
1591 self,
1592 other: &Self,
1593 prec: u64,
1594 rm: RoundingMode,
1595 ) -> (Self, Ordering) {
1596 self.atan2_prec_round_ref_ref(other, prec, rm)
1597 }
1598
1599 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1600 /// positive $x$-axis, rounding the result to the specified precision and with the specified
1601 /// rounding mode. The first [`Float`] is taken by reference and the second by value. An
1602 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
1603 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
1604 /// whenever this function returns a `NaN` it also returns `Equal`.
1605 ///
1606 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1607 /// complexity; this function behaves the same way.
1608 ///
1609 /// # Panics
1610 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1611 /// with the given precision (which is the case unless the result is a zero).
1612 ///
1613 /// # Examples
1614 /// ```
1615 /// use malachite_base::num::basic::traits::{NegativeOne, One, Zero};
1616 /// use malachite_base::rounding_modes::RoundingMode::*;
1617 /// use malachite_float::Float;
1618 /// use std::cmp::Ordering::*;
1619 ///
1620 /// let (t, o) = (&Float::ONE).atan2_prec_round_ref_val(Float::ONE, 10, Floor);
1621 /// assert_eq!(t.to_string(), "0.78516");
1622 /// assert_eq!(o, Less);
1623 ///
1624 /// // a negative x with a zero y is half a turn
1625 /// let (t, o) = (&Float::ZERO).atan2_prec_round_ref_val(Float::NEGATIVE_ONE, 10, Floor);
1626 /// assert_eq!(t.to_string(), "3.1406");
1627 /// assert_eq!(o, Less);
1628 /// ```
1629 #[inline]
1630 #[allow(clippy::needless_pass_by_value)]
1631 pub fn atan2_prec_round_ref_val(
1632 &self,
1633 other: Self,
1634 prec: u64,
1635 rm: RoundingMode,
1636 ) -> (Self, Ordering) {
1637 self.atan2_prec_round_ref_ref(&other, prec, rm)
1638 }
1639
1640 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1641 /// positive $x$-axis, rounding the result to the nearest value of the specified precision. The
1642 /// [`Float`]s are both taken by value. An [`Ordering`] is also returned, indicating whether the
1643 /// rounded angle is less than, equal to, or greater than the exact angle. Although `NaN`s are
1644 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1645 /// `Equal`.
1646 ///
1647 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1648 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1649 /// the `Nearest` rounding mode.
1650 ///
1651 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1652 /// complexity; this function is that one with `Nearest`.
1653 ///
1654 /// If you want to use a rounding mode other than `Nearest`, consider using
1655 /// [`Float::atan2_prec_round`] instead.
1656 ///
1657 /// # Panics
1658 /// Panics if `prec` is zero.
1659 ///
1660 /// # Examples
1661 /// ```
1662 /// use malachite_base::num::basic::traits::One;
1663 /// use malachite_float::Float;
1664 /// use std::cmp::Ordering::*;
1665 ///
1666 /// let (t, o) = Float::ONE.atan2_prec(Float::ONE, 10);
1667 /// assert_eq!(t.to_string(), "0.78516");
1668 /// assert_eq!(o, Less);
1669 /// ```
1670 #[inline]
1671 #[allow(clippy::needless_pass_by_value)]
1672 pub fn atan2_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
1673 self.atan2_prec_round_ref_ref(&other, prec, Nearest)
1674 }
1675
1676 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1677 /// positive $x$-axis, rounding the result to the nearest value of the specified precision. The
1678 /// first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
1679 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
1680 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
1681 /// returns a `NaN` it also returns `Equal`.
1682 ///
1683 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1684 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1685 /// the `Nearest` rounding mode.
1686 ///
1687 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1688 /// complexity; this function is that one with `Nearest`.
1689 ///
1690 /// If you want to use a rounding mode other than `Nearest`, consider using
1691 /// [`Float::atan2_prec_round`] instead.
1692 ///
1693 /// # Panics
1694 /// Panics if `prec` is zero.
1695 ///
1696 /// # Examples
1697 /// ```
1698 /// use malachite_base::num::basic::traits::One;
1699 /// use malachite_float::Float;
1700 /// use std::cmp::Ordering::*;
1701 ///
1702 /// let (t, o) = Float::ONE.atan2_prec_val_ref(&Float::ONE, 10);
1703 /// assert_eq!(t.to_string(), "0.78516");
1704 /// assert_eq!(o, Less);
1705 /// ```
1706 #[inline]
1707 #[allow(clippy::needless_pass_by_value)]
1708 pub fn atan2_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
1709 self.atan2_prec_round_ref_ref(other, prec, Nearest)
1710 }
1711
1712 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1713 /// positive $x$-axis, rounding the result to the nearest value of the specified precision. The
1714 /// first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
1715 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
1716 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
1717 /// returns a `NaN` it also returns `Equal`.
1718 ///
1719 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1720 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1721 /// the `Nearest` rounding mode.
1722 ///
1723 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1724 /// complexity; this function is that one with `Nearest`.
1725 ///
1726 /// If you want to use a rounding mode other than `Nearest`, consider using
1727 /// [`Float::atan2_prec_round`] instead.
1728 ///
1729 /// # Panics
1730 /// Panics if `prec` is zero.
1731 ///
1732 /// # Examples
1733 /// ```
1734 /// use malachite_base::num::basic::traits::One;
1735 /// use malachite_float::Float;
1736 /// use std::cmp::Ordering::*;
1737 ///
1738 /// let (t, o) = (&Float::ONE).atan2_prec_ref_val(Float::ONE, 10);
1739 /// assert_eq!(t.to_string(), "0.78516");
1740 /// assert_eq!(o, Less);
1741 /// ```
1742 #[inline]
1743 #[allow(clippy::needless_pass_by_value)]
1744 pub fn atan2_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
1745 self.atan2_prec_round_ref_ref(&other, prec, Nearest)
1746 }
1747
1748 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1749 /// positive $x$-axis, rounding the result to the nearest value of the specified precision. The
1750 /// [`Float`]s are both taken by reference. An [`Ordering`] is also returned, indicating whether
1751 /// the rounded angle is less than, equal to, or greater than the exact angle. Although `NaN`s
1752 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
1753 /// `Equal`.
1754 ///
1755 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1756 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1757 /// the `Nearest` rounding mode.
1758 ///
1759 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1760 /// complexity; this function is that one with `Nearest`.
1761 ///
1762 /// If you want to use a rounding mode other than `Nearest`, consider using
1763 /// [`Float::atan2_prec_round`] instead.
1764 ///
1765 /// # Panics
1766 /// Panics if `prec` is zero.
1767 ///
1768 /// # Examples
1769 /// ```
1770 /// use malachite_base::num::basic::traits::One;
1771 /// use malachite_float::Float;
1772 /// use std::cmp::Ordering::*;
1773 ///
1774 /// let (t, o) = (&Float::ONE).atan2_prec_ref_ref(&Float::ONE, 10);
1775 /// assert_eq!(t.to_string(), "0.78516");
1776 /// assert_eq!(o, Less);
1777 /// ```
1778 #[inline]
1779 pub fn atan2_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
1780 self.atan2_prec_round_ref_ref(other, prec, Nearest)
1781 }
1782
1783 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1784 /// positive $x$-axis, rounding the result with the specified rounding mode. The [`Float`]s are
1785 /// both taken by value. An [`Ordering`] is also returned, indicating whether the rounded angle
1786 /// is less than, equal to, or greater than the exact angle. Although `NaN`s are not comparable
1787 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1788 ///
1789 /// The precision of the output is the maximum of the precisions of the inputs. See
1790 /// [`RoundingMode`] for a description of the possible rounding modes.
1791 ///
1792 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1793 /// complexity; this function is that one with `prec` the maximum input precision.
1794 ///
1795 /// If you want to specify the output precision, consider using [`Float::atan2_prec_round`]
1796 /// instead.
1797 ///
1798 /// # Panics
1799 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1800 /// the inputs.
1801 ///
1802 /// # Examples
1803 /// ```
1804 /// use malachite_base::rounding_modes::RoundingMode::*;
1805 /// use malachite_float::Float;
1806 /// use std::cmp::Ordering::*;
1807 ///
1808 /// let (t, o) = Float::from(0.3f64).atan2_round(Float::from(0.4f64), Floor);
1809 /// assert_eq!(t.to_string(), "0.64350110879328426");
1810 /// assert_eq!(o, Less);
1811 /// ```
1812 #[inline]
1813 #[allow(clippy::needless_pass_by_value)]
1814 pub fn atan2_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
1815 let prec = max(self.significant_bits(), other.significant_bits());
1816 self.atan2_prec_round_ref_ref(&other, prec, rm)
1817 }
1818
1819 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1820 /// positive $x$-axis, rounding the result with the specified rounding mode. The first [`Float`]
1821 /// is taken by value and the second by reference. An [`Ordering`] is also returned, indicating
1822 /// whether the rounded angle is less than, equal to, or greater than the exact angle. Although
1823 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1824 /// returns `Equal`.
1825 ///
1826 /// The precision of the output is the maximum of the precisions of the inputs. See
1827 /// [`RoundingMode`] for a description of the possible rounding modes.
1828 ///
1829 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1830 /// complexity; this function is that one with `prec` the maximum input precision.
1831 ///
1832 /// If you want to specify the output precision, consider using [`Float::atan2_prec_round`]
1833 /// instead.
1834 ///
1835 /// # Panics
1836 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1837 /// the inputs.
1838 ///
1839 /// # Examples
1840 /// ```
1841 /// use malachite_base::rounding_modes::RoundingMode::*;
1842 /// use malachite_float::Float;
1843 /// use std::cmp::Ordering::*;
1844 ///
1845 /// let (t, o) = Float::from(0.3f64).atan2_round_val_ref(&Float::from(0.4f64), Floor);
1846 /// assert_eq!(t.to_string(), "0.64350110879328426");
1847 /// assert_eq!(o, Less);
1848 /// ```
1849 #[inline]
1850 #[allow(clippy::needless_pass_by_value)]
1851 pub fn atan2_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
1852 let prec = max(self.significant_bits(), other.significant_bits());
1853 self.atan2_prec_round_ref_ref(other, prec, rm)
1854 }
1855
1856 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1857 /// positive $x$-axis, rounding the result with the specified rounding mode. The first [`Float`]
1858 /// is taken by reference and the second by value. An [`Ordering`] is also returned, indicating
1859 /// whether the rounded angle is less than, equal to, or greater than the exact angle. Although
1860 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1861 /// returns `Equal`.
1862 ///
1863 /// The precision of the output is the maximum of the precisions of the inputs. See
1864 /// [`RoundingMode`] for a description of the possible rounding modes.
1865 ///
1866 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1867 /// complexity; this function is that one with `prec` the maximum input precision.
1868 ///
1869 /// If you want to specify the output precision, consider using [`Float::atan2_prec_round`]
1870 /// instead.
1871 ///
1872 /// # Panics
1873 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1874 /// the inputs.
1875 ///
1876 /// # Examples
1877 /// ```
1878 /// use malachite_base::rounding_modes::RoundingMode::*;
1879 /// use malachite_float::Float;
1880 /// use std::cmp::Ordering::*;
1881 ///
1882 /// let (t, o) = (&Float::from(0.3f64)).atan2_round_ref_val(Float::from(0.4f64), Floor);
1883 /// assert_eq!(t.to_string(), "0.64350110879328426");
1884 /// assert_eq!(o, Less);
1885 /// ```
1886 #[inline]
1887 #[allow(clippy::needless_pass_by_value)]
1888 pub fn atan2_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
1889 let prec = max(self.significant_bits(), other.significant_bits());
1890 self.atan2_prec_round_ref_ref(&other, prec, rm)
1891 }
1892
1893 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1894 /// positive $x$-axis, rounding the result with the specified rounding mode. The [`Float`]s are
1895 /// both taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
1896 /// angle is less than, equal to, or greater than the exact angle. Although `NaN`s are not
1897 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1898 ///
1899 /// The precision of the output is the maximum of the precisions of the inputs. See
1900 /// [`RoundingMode`] for a description of the possible rounding modes.
1901 ///
1902 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1903 /// complexity; this function is that one with `prec` the maximum input precision.
1904 ///
1905 /// If you want to specify the output precision, consider using [`Float::atan2_prec_round`]
1906 /// instead.
1907 ///
1908 /// # Panics
1909 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1910 /// the inputs.
1911 ///
1912 /// # Examples
1913 /// ```
1914 /// use malachite_base::rounding_modes::RoundingMode::*;
1915 /// use malachite_float::Float;
1916 /// use std::cmp::Ordering::*;
1917 ///
1918 /// let (t, o) = (&Float::from(0.3f64)).atan2_round_ref_ref(&Float::from(0.4f64), Floor);
1919 /// assert_eq!(t.to_string(), "0.64350110879328426");
1920 /// assert_eq!(o, Less);
1921 /// ```
1922 #[inline]
1923 pub fn atan2_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
1924 let prec = max(self.significant_bits(), other.significant_bits());
1925 self.atan2_prec_round_ref_ref(other, prec, rm)
1926 }
1927
1928 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1929 /// positive $x$-axis, rounding the result to the specified precision and with the specified
1930 /// rounding mode. The first [`Float`] is replaced by the result, and the second is taken by
1931 /// value. An [`Ordering`] is returned, indicating whether the rounded angle is less than, equal
1932 /// to, or greater than the exact angle.
1933 ///
1934 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1935 /// complexity; this function behaves the same way.
1936 ///
1937 /// # Panics
1938 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1939 /// with the given precision (which is the case unless the result is a zero).
1940 ///
1941 /// # Examples
1942 /// ```
1943 /// use malachite_base::num::basic::traits::One;
1944 /// use malachite_base::rounding_modes::RoundingMode::*;
1945 /// use malachite_float::Float;
1946 /// use std::cmp::Ordering::*;
1947 ///
1948 /// let mut y = Float::ONE;
1949 /// assert_eq!(y.atan2_prec_round_assign(Float::ONE, 10, Floor), Less);
1950 /// assert_eq!(y.to_string(), "0.78516");
1951 /// ```
1952 #[inline]
1953 #[allow(clippy::needless_pass_by_value)]
1954 pub fn atan2_prec_round_assign(
1955 &mut self,
1956 other: Self,
1957 prec: u64,
1958 rm: RoundingMode,
1959 ) -> Ordering {
1960 let (t, o) = self.atan2_prec_round_ref_ref(&other, prec, rm);
1961 *self = t;
1962 o
1963 }
1964
1965 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
1966 /// positive $x$-axis, rounding the result to the specified precision and with the specified
1967 /// rounding mode. The first [`Float`] is replaced by the result, and the second is taken by
1968 /// reference. An [`Ordering`] is returned, indicating whether the rounded angle is less than,
1969 /// equal to, or greater than the exact angle.
1970 ///
1971 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
1972 /// complexity; this function behaves the same way.
1973 ///
1974 /// # Panics
1975 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1976 /// with the given precision (which is the case unless the result is a zero).
1977 ///
1978 /// # Examples
1979 /// ```
1980 /// use malachite_base::num::basic::traits::One;
1981 /// use malachite_base::rounding_modes::RoundingMode::*;
1982 /// use malachite_float::Float;
1983 /// use std::cmp::Ordering::*;
1984 ///
1985 /// let mut y = Float::ONE;
1986 /// assert_eq!(y.atan2_prec_round_assign_ref(&Float::ONE, 10, Floor), Less);
1987 /// assert_eq!(y.to_string(), "0.78516");
1988 /// ```
1989 #[inline]
1990 pub fn atan2_prec_round_assign_ref(
1991 &mut self,
1992 other: &Self,
1993 prec: u64,
1994 rm: RoundingMode,
1995 ) -> Ordering {
1996 let (t, o) = self.atan2_prec_round_ref_ref(other, prec, rm);
1997 *self = t;
1998 o
1999 }
2000
2001 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2002 /// positive $x$-axis, rounding the result to the nearest value of the specified precision. The
2003 /// first [`Float`] is replaced by the result, and the second is taken by value. An [`Ordering`]
2004 /// is returned, indicating whether the rounded angle is less than, equal to, or greater than
2005 /// the exact angle.
2006 ///
2007 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
2008 /// complexity; this function behaves the same way.
2009 ///
2010 /// # Panics
2011 /// Panics if `prec` is zero.
2012 ///
2013 /// # Examples
2014 /// ```
2015 /// use malachite_base::num::basic::traits::One;
2016 /// use malachite_float::Float;
2017 /// use std::cmp::Ordering::*;
2018 ///
2019 /// let mut y = Float::ONE;
2020 /// assert_eq!(y.atan2_prec_assign(Float::ONE, 10), Less);
2021 /// assert_eq!(y.to_string(), "0.78516");
2022 /// ```
2023 #[inline]
2024 #[allow(clippy::needless_pass_by_value)]
2025 pub fn atan2_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
2026 let (t, o) = self.atan2_prec_ref_ref(&other, prec);
2027 *self = t;
2028 o
2029 }
2030
2031 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2032 /// positive $x$-axis, rounding the result to the nearest value of the specified precision. The
2033 /// first [`Float`] is replaced by the result, and the second is taken by reference. An
2034 /// [`Ordering`] is returned, indicating whether the rounded angle is less than, equal to, or
2035 /// greater than the exact angle.
2036 ///
2037 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
2038 /// complexity; this function behaves the same way.
2039 ///
2040 /// # Panics
2041 /// Panics if `prec` is zero.
2042 ///
2043 /// # Examples
2044 /// ```
2045 /// use malachite_base::num::basic::traits::One;
2046 /// use malachite_float::Float;
2047 /// use std::cmp::Ordering::*;
2048 ///
2049 /// let mut y = Float::ONE;
2050 /// assert_eq!(y.atan2_prec_assign_ref(&Float::ONE, 10), Less);
2051 /// assert_eq!(y.to_string(), "0.78516");
2052 /// ```
2053 #[inline]
2054 pub fn atan2_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
2055 let (t, o) = self.atan2_prec_ref_ref(other, prec);
2056 *self = t;
2057 o
2058 }
2059
2060 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2061 /// positive $x$-axis, rounding the result to the specified rounding mode. The first [`Float`]
2062 /// is replaced by the result, and the second is taken by value. An [`Ordering`] is returned,
2063 /// indicating whether the rounded angle is less than, equal to, or greater than the exact
2064 /// angle.
2065 ///
2066 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
2067 /// complexity; this function behaves the same way.
2068 ///
2069 /// # Panics
2070 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
2071 /// the inputs.
2072 ///
2073 /// # Examples
2074 /// ```
2075 /// use malachite_base::rounding_modes::RoundingMode::*;
2076 /// use malachite_float::Float;
2077 /// use std::cmp::Ordering::*;
2078 ///
2079 /// let mut y = Float::from(0.3f64);
2080 /// assert_eq!(y.atan2_round_assign(Float::from(0.4f64), Floor), Less);
2081 /// assert_eq!(y.to_string(), "0.64350110879328426");
2082 /// ```
2083 #[inline]
2084 #[allow(clippy::needless_pass_by_value)]
2085 pub fn atan2_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
2086 let prec = max(self.significant_bits(), other.significant_bits());
2087 let (t, o) = self.atan2_prec_round_ref_ref(&other, prec, rm);
2088 *self = t;
2089 o
2090 }
2091
2092 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2093 /// positive $x$-axis, rounding the result to the specified rounding mode. The first [`Float`]
2094 /// is replaced by the result, and the second is taken by reference. An [`Ordering`] is
2095 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
2096 /// exact angle.
2097 ///
2098 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
2099 /// complexity; this function behaves the same way.
2100 ///
2101 /// # Panics
2102 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
2103 /// the inputs.
2104 ///
2105 /// # Examples
2106 /// ```
2107 /// use malachite_base::rounding_modes::RoundingMode::*;
2108 /// use malachite_float::Float;
2109 /// use std::cmp::Ordering::*;
2110 ///
2111 /// let mut y = Float::from(0.3f64);
2112 /// assert_eq!(y.atan2_round_assign_ref(&Float::from(0.4f64), Floor), Less);
2113 /// assert_eq!(y.to_string(), "0.64350110879328426");
2114 /// ```
2115 #[inline]
2116 pub fn atan2_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
2117 let prec = max(self.significant_bits(), other.significant_bits());
2118 let (t, o) = self.atan2_prec_round_ref_ref(other, prec, rm);
2119 *self = t;
2120 o
2121 }
2122
2123 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2124 /// positive $x$-axis, rounding the result to the specified precision and with the specified
2125 /// rounding mode and returning the result as a [`Float`]. The [`Rational`]s are both taken by
2126 /// value. An [`Ordering`] is also returned, indicating whether the rounded angle is less than,
2127 /// equal to, or greater than the exact angle.
2128 ///
2129 /// See [`RoundingMode`] for a description of the possible rounding modes.
2130 ///
2131 /// $$
2132 /// f(y,x,p,m) = \operatorname{atan2}(y,x)+\varepsilon.
2133 /// $$
2134 /// - If the result is zero, $\varepsilon$ may be ignored or assumed to be 0.
2135 /// - Otherwise, if $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
2136 /// |\operatorname{atan2}(y,x)|\rfloor-p+1}$.
2137 /// - Otherwise, if $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
2138 /// |\operatorname{atan2}(y,x)|\rfloor-p}$.
2139 ///
2140 /// The output has precision `prec`.
2141 ///
2142 /// Special cases:
2143 /// - $f(0,x,p,m)=0.0$ if $x \geq 0$, and $\pi$ if $x < 0$
2144 /// - $f(y,0,p,m)=\pm\pi/2$, with the sign of $y$, for nonzero $y$
2145 ///
2146 /// A [`Rational`] has no signed zeros and no infinities, so the quadrant-selecting sign of a
2147 /// zero argument has no counterpart here: the zero result is a positive zero, and it is the
2148 /// only exact case.
2149 ///
2150 /// Overflow is not possible, since $|\operatorname{atan2}(y,x)| \leq \pi$. The result
2151 /// underflows only for a positive $x$ with $|y/x|$ below $2^{-2^{30}}$, where it is about
2152 /// $y/x$; there $0.0$ or $\pm2^{-2^{30}}$ is returned instead, by the rounding mode alone.
2153 ///
2154 /// If you know you'll be using `Nearest`, consider using [`Float::atan2_rational_prec`]
2155 /// instead.
2156 ///
2157 /// # Worst-case complexity
2158 /// $T(n, m) = O(n (\log n)^3 \log\log n + m (\log m)^2 \log\log m)$
2159 ///
2160 /// $M(n, m) = O(n \log n + m \log m)$
2161 ///
2162 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2163 /// `max(y.significant_bits(), x.significant_bits())`: the quotient is formed exactly, then
2164 /// rounded once and its [`Float`] arctangent taken at a working precision of about $n$ bits,
2165 /// which costs the first term; the second covers the inputs. The magnitudes of the inputs do
2166 /// not drive the cost.
2167 ///
2168 /// # Panics
2169 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2170 /// with the given precision (which is the case unless the result is zero).
2171 ///
2172 /// # Examples
2173 /// ```
2174 /// use malachite_base::num::basic::traits::{NegativeOne, Zero};
2175 /// use malachite_base::rounding_modes::RoundingMode::*;
2176 /// use malachite_float::Float;
2177 /// use malachite_q::Rational;
2178 /// use std::cmp::Ordering::*;
2179 ///
2180 /// let (t, o) =
2181 /// Float::atan2_rational_prec_round(Rational::from(3), Rational::from(4), 10, Floor);
2182 /// assert_eq!(t.to_string(), "0.64258");
2183 /// assert_eq!(o, Less);
2184 ///
2185 /// // a negative x with a zero y is half a turn
2186 /// let (t, o) =
2187 /// Float::atan2_rational_prec_round(Rational::ZERO, Rational::NEGATIVE_ONE, 10, Floor);
2188 /// assert_eq!(t.to_string(), "3.1406");
2189 /// assert_eq!(o, Less);
2190 /// ```
2191 #[inline]
2192 #[allow(clippy::needless_pass_by_value)]
2193 pub fn atan2_rational_prec_round(
2194 y: Rational,
2195 x: Rational,
2196 prec: u64,
2197 rm: RoundingMode,
2198 ) -> (Self, Ordering) {
2199 Self::atan2_rational_prec_round_ref(&y, &x, prec, rm)
2200 }
2201
2202 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2203 /// positive $x$-axis, rounding the result to the specified precision and with the specified
2204 /// rounding mode and returning the result as a [`Float`]. The [`Rational`]s are both taken by
2205 /// reference. An [`Ordering`] is also returned, indicating whether the rounded angle is less
2206 /// than, equal to, or greater than the exact angle.
2207 ///
2208 /// See [`Float::atan2_rational_prec_round`] for the error bounds, the special cases, underflow,
2209 /// and the complexity; this function behaves the same way.
2210 ///
2211 /// # Panics
2212 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2213 /// with the given precision.
2214 ///
2215 /// # Examples
2216 /// ```
2217 /// use malachite_base::rounding_modes::RoundingMode::*;
2218 /// use malachite_float::Float;
2219 /// use malachite_q::Rational;
2220 /// use std::cmp::Ordering::*;
2221 ///
2222 /// let (t, o) = Float::atan2_rational_prec_round_ref(
2223 /// &Rational::from(3),
2224 /// &Rational::from(4),
2225 /// 10,
2226 /// Ceiling,
2227 /// );
2228 /// assert_eq!(t.to_string(), "0.64355");
2229 /// assert_eq!(o, Greater);
2230 /// ```
2231 pub fn atan2_rational_prec_round_ref(
2232 y: &Rational,
2233 x: &Rational,
2234 prec: u64,
2235 rm: RoundingMode,
2236 ) -> (Self, Ordering) {
2237 assert_ne!(prec, 0);
2238 // atan2(0, x) = 0 for a nonnegative x and pi for a negative one; a `Rational` zero is
2239 // unsigned, so there is no negative-zero branch as there is for `Float`s
2240 if *y == 0u32 {
2241 return if *x < 0u32 {
2242 pi_div_2ui(0, false, prec, rm)
2243 } else {
2244 (Self::ZERO, Equal)
2245 };
2246 }
2247 // atan2(y, 0) = +-pi/2, with the sign of y
2248 if *x == 0u32 {
2249 return pi_div_2ui(1, *y < 0u32, prec, rm);
2250 }
2251 atan2_rational_prec_round_normal_ref(y, x, prec, rm)
2252 }
2253
2254 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2255 /// positive $x$-axis, rounding the result to the nearest value of the specified precision and
2256 /// returning the result as a [`Float`]. The [`Rational`]s are both taken by value. An
2257 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
2258 /// or greater than the exact angle.
2259 ///
2260 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2261 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2262 /// the `Nearest` rounding mode.
2263 ///
2264 /// See [`Float::atan2_rational_prec_round`] for the error bounds, the special cases, underflow,
2265 /// and the complexity; this function is that one with `Nearest`.
2266 ///
2267 /// If you want to use a rounding mode other than `Nearest`, consider using
2268 /// [`Float::atan2_rational_prec_round`] instead.
2269 ///
2270 /// # Panics
2271 /// Panics if `prec` is zero.
2272 ///
2273 /// # Examples
2274 /// ```
2275 /// use malachite_float::Float;
2276 /// use malachite_q::Rational;
2277 /// use std::cmp::Ordering::*;
2278 ///
2279 /// let (t, o) = Float::atan2_rational_prec(Rational::from(3), Rational::from(4), 10);
2280 /// assert_eq!(t.to_string(), "0.64355");
2281 /// assert_eq!(o, Greater);
2282 ///
2283 /// let (t, o) = Float::atan2_rational_prec(Rational::from(3), Rational::from(4), 53);
2284 /// assert_eq!(t.to_string(), "0.64350110879328437");
2285 /// assert_eq!(o, Less);
2286 /// ```
2287 #[inline]
2288 #[allow(clippy::needless_pass_by_value)]
2289 pub fn atan2_rational_prec(y: Rational, x: Rational, prec: u64) -> (Self, Ordering) {
2290 Self::atan2_rational_prec_round_ref(&y, &x, prec, Nearest)
2291 }
2292
2293 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
2294 /// positive $x$-axis, rounding the result to the nearest value of the specified precision and
2295 /// returning the result as a [`Float`]. The [`Rational`]s are both taken by reference. An
2296 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
2297 /// or greater than the exact angle.
2298 ///
2299 /// See [`Float::atan2_rational_prec`] for the error bounds, the special cases, underflow, and
2300 /// the complexity; this function behaves the same way.
2301 ///
2302 /// # Panics
2303 /// Panics if `prec` is zero.
2304 ///
2305 /// # Examples
2306 /// ```
2307 /// use malachite_float::Float;
2308 /// use malachite_q::Rational;
2309 /// use std::cmp::Ordering::*;
2310 ///
2311 /// let (t, o) = Float::atan2_rational_prec_ref(&Rational::from(3), &Rational::from(4), 53);
2312 /// assert_eq!(t.to_string(), "0.64350110879328437");
2313 /// assert_eq!(o, Less);
2314 /// ```
2315 #[inline]
2316 pub fn atan2_rational_prec_ref(y: &Rational, x: &Rational, prec: u64) -> (Self, Ordering) {
2317 Self::atan2_rational_prec_round_ref(y, x, prec, Nearest)
2318 }
2319
2320 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
2321 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
2322 /// and with the specified rounding mode and returning the result as a [`Float`]. The
2323 /// [`Rational`]s are both taken by value. An [`Ordering`] is also returned, indicating whether
2324 /// the rounded angle is less than, equal to, or greater than the exact angle.
2325 ///
2326 /// See [`RoundingMode`] for a description of the possible rounding modes.
2327 ///
2328 /// $$
2329 /// f(y,x,u,p,m) = \operatorname{atan2}(y,x)u/(2\pi)+\varepsilon.
2330 /// $$
2331 /// - If the result is one of the exact cases below, $\varepsilon$ may be ignored or assumed to
2332 /// be 0.
2333 /// - Otherwise, if $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
2334 /// |\operatorname{atan2}(y,x)u/(2\pi)|\rfloor-p+1}$.
2335 /// - Otherwise, if $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
2336 /// |\operatorname{atan2}(y,x)u/(2\pi)|\rfloor-p}$.
2337 ///
2338 /// Special cases:
2339 /// - $f(0,x,u,p,m)=0.0$ if $x \geq 0$, and $u/2$ if $x < 0$
2340 /// - $f(y,0,u,p,m)=\pm u/4$, with the sign of $y$, for nonzero $y$
2341 /// - $f(\pm x,x,u,p,m)=\pm u/8$ for positive $x$, and $\pm3u/8$ for negative $x$
2342 /// - $f(y,x,0,p,m)=0.0$
2343 ///
2344 /// These are the only exact cases, and the turn fractions are exact only when $p$ is large
2345 /// enough to hold them. A [`Rational`] has no NaN, no infinities, and no signed zeros, so the
2346 /// quadrant-selecting sign of a zero argument has no counterpart here. As in the [`Float`]
2347 /// case, $u = 0$ gives a zero throughout, where MPFR's `mpfr_atan2u` returns $\pm1$ for a
2348 /// negative $x$.
2349 ///
2350 /// Overflow is not possible, since $|f(y,x,u,p,m)| \leq u/2 < 2^{63}$. The result underflows
2351 /// only for a positive $x$ with $|y/x|$ tiny and $u$ small.
2352 ///
2353 /// The output has precision `prec`.
2354 ///
2355 /// If you know you'll be using `Nearest`, consider using
2356 /// [`Float::atan2_with_period_rational_prec`] instead.
2357 ///
2358 /// # Worst-case complexity
2359 /// $T(n, m) = O(n (\log n)^3 \log\log n + m (\log m)^2 \log\log m)$
2360 ///
2361 /// $M(n, m) = O(n \log n + m \log m)$
2362 ///
2363 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2364 /// `max(y.significant_bits(), x.significant_bits())`: the quotient is formed exactly, then
2365 /// rounded once and its periodic arctangent taken at a working precision of about $n$ bits,
2366 /// which costs the first term; the second covers the inputs.
2367 ///
2368 /// # Panics
2369 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2370 /// with the given precision.
2371 ///
2372 /// # Examples
2373 /// ```
2374 /// use malachite_base::num::basic::traits::One;
2375 /// use malachite_base::rounding_modes::RoundingMode::*;
2376 /// use malachite_float::Float;
2377 /// use malachite_q::Rational;
2378 /// use std::cmp::Ordering::*;
2379 ///
2380 /// let (t, o) = Float::atan2_with_period_rational_prec_round(
2381 /// Rational::from(3),
2382 /// Rational::from(4),
2383 /// 360,
2384 /// 10,
2385 /// Floor,
2386 /// );
2387 /// assert_eq!(t.to_string(), "36.812");
2388 /// assert_eq!(o, Less);
2389 ///
2390 /// // the first quadrant's diagonal is an eighth of a turn
2391 /// let (t, o) = Float::atan2_with_period_rational_prec_round(
2392 /// Rational::ONE,
2393 /// Rational::ONE,
2394 /// 360,
2395 /// 10,
2396 /// Exact,
2397 /// );
2398 /// assert_eq!(t.to_string(), "45.000");
2399 /// assert_eq!(o, Equal);
2400 /// ```
2401 #[inline]
2402 #[allow(clippy::needless_pass_by_value)]
2403 pub fn atan2_with_period_rational_prec_round(
2404 y: Rational,
2405 x: Rational,
2406 u: u64,
2407 prec: u64,
2408 rm: RoundingMode,
2409 ) -> (Self, Ordering) {
2410 Self::atan2_with_period_rational_prec_round_ref(&y, &x, u, prec, rm)
2411 }
2412
2413 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
2414 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the specified precision
2415 /// and with the specified rounding mode and returning the result as a [`Float`]. The
2416 /// [`Rational`]s are both taken by reference. An [`Ordering`] is also returned, indicating
2417 /// whether the rounded angle is less than, equal to, or greater than the exact angle.
2418 ///
2419 /// See [`Float::atan2_with_period_rational_prec_round`] for the error bounds, the special
2420 /// cases, underflow, and the complexity; this function behaves the same way.
2421 ///
2422 /// # Panics
2423 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2424 /// with the given precision.
2425 ///
2426 /// # Examples
2427 /// ```
2428 /// use malachite_base::rounding_modes::RoundingMode::*;
2429 /// use malachite_float::Float;
2430 /// use malachite_q::Rational;
2431 /// use std::cmp::Ordering::*;
2432 ///
2433 /// let (t, o) = Float::atan2_with_period_rational_prec_round_ref(
2434 /// &Rational::from(3),
2435 /// &Rational::from(4),
2436 /// 360,
2437 /// 10,
2438 /// Ceiling,
2439 /// );
2440 /// assert_eq!(t.to_string(), "36.875");
2441 /// assert_eq!(o, Greater);
2442 /// ```
2443 pub fn atan2_with_period_rational_prec_round_ref(
2444 y: &Rational,
2445 x: &Rational,
2446 u: u64,
2447 prec: u64,
2448 rm: RoundingMode,
2449 ) -> (Self, Ordering) {
2450 assert_ne!(prec, 0);
2451 // atan2u(0, x, u) = 0 for a nonnegative x and u/2 for a negative one
2452 if *y == 0u32 {
2453 return if *x < 0u32 {
2454 scaled_unsigned(u, 1, true, prec, rm)
2455 } else {
2456 (Self::ZERO, Equal)
2457 };
2458 }
2459 let y_positive = *y > 0u32;
2460 // atan2u(y, 0, u) = +-u/4, with the sign of y
2461 if *x == 0u32 {
2462 return scaled_unsigned(u, 2, y_positive, prec, rm);
2463 }
2464 // |y| = |x| puts the angle on a quadrant diagonal, an exact eighth or three eighths of a
2465 // turn
2466 if y.eq_abs(x) {
2467 return if *x > 0u32 {
2468 scaled_unsigned(u, 3, y_positive, prec, rm)
2469 } else {
2470 atan2u_aux2(u, 3, y_positive, prec, rm)
2471 };
2472 }
2473 // every angle measures zero units when the whole turn does; see the `Float` version for why
2474 // this departs from MPFR
2475 if u == 0 {
2476 return (Self::ZERO, Equal);
2477 }
2478 atan2_with_period_rational_prec_round_normal_ref(y, x, u, prec, rm)
2479 }
2480
2481 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
2482 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
2483 /// specified precision and returning the result as a [`Float`]. The [`Rational`]s are both
2484 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded angle is
2485 /// less than, equal to, or greater than the exact angle.
2486 ///
2487 /// If the angle is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2488 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2489 /// the `Nearest` rounding mode.
2490 ///
2491 /// See [`Float::atan2_with_period_rational_prec_round`] for the error bounds, the special
2492 /// cases, underflow, and the complexity; this function is that one with `Nearest`.
2493 ///
2494 /// If you want to use a rounding mode other than `Nearest`, consider using
2495 /// [`Float::atan2_with_period_rational_prec_round`] instead.
2496 ///
2497 /// # Panics
2498 /// Panics if `prec` is zero.
2499 ///
2500 /// # Examples
2501 /// ```
2502 /// use malachite_float::Float;
2503 /// use malachite_q::Rational;
2504 /// use std::cmp::Ordering::*;
2505 ///
2506 /// let (t, o) =
2507 /// Float::atan2_with_period_rational_prec(Rational::from(3), Rational::from(4), 360, 53);
2508 /// assert_eq!(t.to_string(), "36.869897645844020");
2509 /// assert_eq!(o, Less);
2510 /// ```
2511 #[inline]
2512 #[allow(clippy::needless_pass_by_value)]
2513 pub fn atan2_with_period_rational_prec(
2514 y: Rational,
2515 x: Rational,
2516 u: u64,
2517 prec: u64,
2518 ) -> (Self, Ordering) {
2519 Self::atan2_with_period_rational_prec_round_ref(&y, &x, u, prec, Nearest)
2520 }
2521
2522 /// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from
2523 /// the positive $x$-axis in $u$ths of a turn, rounding the result to the nearest value of the
2524 /// specified precision and returning the result as a [`Float`]. The [`Rational`]s are both
2525 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded angle
2526 /// is less than, equal to, or greater than the exact angle.
2527 ///
2528 /// See [`Float::atan2_with_period_rational_prec`] for the error bounds, the special cases,
2529 /// underflow, and the complexity; this function behaves the same way.
2530 ///
2531 /// # Panics
2532 /// Panics if `prec` is zero.
2533 ///
2534 /// # Examples
2535 /// ```
2536 /// use malachite_float::Float;
2537 /// use malachite_q::Rational;
2538 /// use std::cmp::Ordering::*;
2539 ///
2540 /// let (t, o) = Float::atan2_with_period_rational_prec_ref(
2541 /// &Rational::from(3),
2542 /// &Rational::from(4),
2543 /// 360,
2544 /// 53,
2545 /// );
2546 /// assert_eq!(t.to_string(), "36.869897645844020");
2547 /// assert_eq!(o, Less);
2548 /// ```
2549 #[inline]
2550 pub fn atan2_with_period_rational_prec_ref(
2551 y: &Rational,
2552 x: &Rational,
2553 u: u64,
2554 prec: u64,
2555 ) -> (Self, Ordering) {
2556 Self::atan2_with_period_rational_prec_round_ref(y, x, u, prec, Nearest)
2557 }
2558
2559 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2560 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
2561 /// specified rounding mode. The [`Float`]s are both taken by value. An [`Ordering`] is also
2562 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
2563 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
2564 /// returns a `NaN` it also returns `Equal`.
2565 ///
2566 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2567 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2568 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2569 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2570 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2571 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2572 ///
2573 /// # Panics
2574 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2575 /// with the given precision.
2576 ///
2577 /// # Examples
2578 /// ```
2579 /// use malachite_base::num::basic::traits::{One, Two};
2580 /// use malachite_base::rounding_modes::RoundingMode::*;
2581 /// use malachite_float::Float;
2582 /// use std::cmp::Ordering::*;
2583 ///
2584 /// // the first quadrant's diagonal is a quarter turn
2585 /// let (t, o) = Float::ONE.atan2_pi_prec_round(Float::ONE, 10, Exact);
2586 /// assert_eq!(t.to_string(), "0.25000");
2587 /// assert_eq!(o, Equal);
2588 ///
2589 /// let (t, o) = Float::ONE.atan2_pi_prec_round(Float::TWO, 10, Floor);
2590 /// assert_eq!(t.to_string(), "0.14746");
2591 /// assert_eq!(o, Less);
2592 /// ```
2593 #[inline]
2594 #[allow(clippy::needless_pass_by_value)]
2595 pub fn atan2_pi_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
2596 self.atan2_with_period_prec_round(other, 2, prec, rm)
2597 }
2598
2599 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2600 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
2601 /// specified rounding mode. The first [`Float`] is taken by value and the second by reference.
2602 /// An [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal
2603 /// to, or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
2604 /// whenever this function returns a `NaN` it also returns `Equal`.
2605 ///
2606 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2607 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2608 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2609 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2610 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2611 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2612 ///
2613 /// # Panics
2614 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2615 /// with the given precision.
2616 ///
2617 /// # Examples
2618 /// ```
2619 /// use malachite_base::num::basic::traits::{One, Two};
2620 /// use malachite_base::rounding_modes::RoundingMode::*;
2621 /// use malachite_float::Float;
2622 /// use std::cmp::Ordering::*;
2623 ///
2624 /// // the first quadrant's diagonal is a quarter turn
2625 /// let (t, o) = Float::ONE.atan2_pi_prec_round_val_ref(&Float::ONE, 10, Exact);
2626 /// assert_eq!(t.to_string(), "0.25000");
2627 /// assert_eq!(o, Equal);
2628 ///
2629 /// let (t, o) = Float::ONE.atan2_pi_prec_round_val_ref(&Float::TWO, 10, Floor);
2630 /// assert_eq!(t.to_string(), "0.14746");
2631 /// assert_eq!(o, Less);
2632 /// ```
2633 #[inline]
2634 #[allow(clippy::needless_pass_by_value)]
2635 pub fn atan2_pi_prec_round_val_ref(
2636 self,
2637 other: &Self,
2638 prec: u64,
2639 rm: RoundingMode,
2640 ) -> (Self, Ordering) {
2641 self.atan2_with_period_prec_round_val_ref(other, 2, prec, rm)
2642 }
2643
2644 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2645 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
2646 /// specified rounding mode. The first [`Float`] is taken by reference and the second by value.
2647 /// An [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal
2648 /// to, or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
2649 /// whenever this function returns a `NaN` it also returns `Equal`.
2650 ///
2651 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2652 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2653 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2654 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2655 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2656 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2657 ///
2658 /// # Panics
2659 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2660 /// with the given precision.
2661 ///
2662 /// # Examples
2663 /// ```
2664 /// use malachite_base::num::basic::traits::{One, Two};
2665 /// use malachite_base::rounding_modes::RoundingMode::*;
2666 /// use malachite_float::Float;
2667 /// use std::cmp::Ordering::*;
2668 ///
2669 /// // the first quadrant's diagonal is a quarter turn
2670 /// let (t, o) = (&Float::ONE).atan2_pi_prec_round_ref_val(Float::ONE, 10, Exact);
2671 /// assert_eq!(t.to_string(), "0.25000");
2672 /// assert_eq!(o, Equal);
2673 ///
2674 /// let (t, o) = (&Float::ONE).atan2_pi_prec_round_ref_val(Float::TWO, 10, Floor);
2675 /// assert_eq!(t.to_string(), "0.14746");
2676 /// assert_eq!(o, Less);
2677 /// ```
2678 #[inline]
2679 #[allow(clippy::needless_pass_by_value)]
2680 pub fn atan2_pi_prec_round_ref_val(
2681 &self,
2682 other: Self,
2683 prec: u64,
2684 rm: RoundingMode,
2685 ) -> (Self, Ordering) {
2686 self.atan2_with_period_prec_round_ref_val(other, 2, prec, rm)
2687 }
2688
2689 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2690 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
2691 /// specified rounding mode. The [`Float`]s are both taken by reference. An [`Ordering`] is also
2692 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
2693 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
2694 /// returns a `NaN` it also returns `Equal`.
2695 ///
2696 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2697 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2698 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2699 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2700 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2701 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2702 ///
2703 /// # Panics
2704 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2705 /// with the given precision.
2706 ///
2707 /// # Examples
2708 /// ```
2709 /// use malachite_base::num::basic::traits::{One, Two};
2710 /// use malachite_base::rounding_modes::RoundingMode::*;
2711 /// use malachite_float::Float;
2712 /// use std::cmp::Ordering::*;
2713 ///
2714 /// // the first quadrant's diagonal is a quarter turn
2715 /// let (t, o) = (&Float::ONE).atan2_pi_prec_round_ref_ref(&Float::ONE, 10, Exact);
2716 /// assert_eq!(t.to_string(), "0.25000");
2717 /// assert_eq!(o, Equal);
2718 ///
2719 /// let (t, o) = (&Float::ONE).atan2_pi_prec_round_ref_ref(&Float::TWO, 10, Floor);
2720 /// assert_eq!(t.to_string(), "0.14746");
2721 /// assert_eq!(o, Less);
2722 /// ```
2723 #[inline]
2724 pub fn atan2_pi_prec_round_ref_ref(
2725 &self,
2726 other: &Self,
2727 prec: u64,
2728 rm: RoundingMode,
2729 ) -> (Self, Ordering) {
2730 self.atan2_with_period_prec_round_ref_ref(other, 2, prec, rm)
2731 }
2732
2733 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2734 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
2735 /// precision. The [`Float`]s are both taken by value. An [`Ordering`] is also returned,
2736 /// indicating whether the rounded angle is less than, equal to, or greater than the exact
2737 /// angle. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
2738 /// `NaN` it also returns `Equal`.
2739 ///
2740 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2741 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2742 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2743 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2744 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2745 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2746 ///
2747 /// # Panics
2748 /// Panics if `prec` is zero.
2749 ///
2750 /// # Examples
2751 /// ```
2752 /// use malachite_base::num::basic::traits::{One, Two};
2753 /// use malachite_float::Float;
2754 /// use std::cmp::Ordering::*;
2755 ///
2756 /// let (t, o) = Float::ONE.atan2_pi_prec(Float::TWO, 10);
2757 /// assert_eq!(t.to_string(), "0.14771");
2758 /// assert_eq!(o, Greater);
2759 /// ```
2760 #[inline]
2761 #[allow(clippy::needless_pass_by_value)]
2762 pub fn atan2_pi_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
2763 self.atan2_with_period_prec(other, 2, prec)
2764 }
2765
2766 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2767 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
2768 /// precision. The first [`Float`] is taken by value and the second by reference. An
2769 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
2770 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
2771 /// whenever this function returns a `NaN` it also returns `Equal`.
2772 ///
2773 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2774 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2775 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2776 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2777 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2778 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2779 ///
2780 /// # Panics
2781 /// Panics if `prec` is zero.
2782 ///
2783 /// # Examples
2784 /// ```
2785 /// use malachite_base::num::basic::traits::{One, Two};
2786 /// use malachite_float::Float;
2787 /// use std::cmp::Ordering::*;
2788 ///
2789 /// let (t, o) = Float::ONE.atan2_pi_prec_val_ref(&Float::TWO, 10);
2790 /// assert_eq!(t.to_string(), "0.14771");
2791 /// assert_eq!(o, Greater);
2792 /// ```
2793 #[inline]
2794 #[allow(clippy::needless_pass_by_value)]
2795 pub fn atan2_pi_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
2796 self.atan2_with_period_prec_val_ref(other, 2, prec)
2797 }
2798
2799 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2800 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
2801 /// precision. The first [`Float`] is taken by reference and the second by value. An
2802 /// [`Ordering`] is also returned, indicating whether the rounded angle is less than, equal to,
2803 /// or greater than the exact angle. Although `NaN`s are not comparable to any [`Float`],
2804 /// whenever this function returns a `NaN` it also returns `Equal`.
2805 ///
2806 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2807 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2808 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2809 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2810 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2811 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2812 ///
2813 /// # Panics
2814 /// Panics if `prec` is zero.
2815 ///
2816 /// # Examples
2817 /// ```
2818 /// use malachite_base::num::basic::traits::{One, Two};
2819 /// use malachite_float::Float;
2820 /// use std::cmp::Ordering::*;
2821 ///
2822 /// let (t, o) = (&Float::ONE).atan2_pi_prec_ref_val(Float::TWO, 10);
2823 /// assert_eq!(t.to_string(), "0.14771");
2824 /// assert_eq!(o, Greater);
2825 /// ```
2826 #[inline]
2827 #[allow(clippy::needless_pass_by_value)]
2828 pub fn atan2_pi_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
2829 self.atan2_with_period_prec_ref_val(other, 2, prec)
2830 }
2831
2832 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2833 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
2834 /// precision. The [`Float`]s are both taken by reference. An [`Ordering`] is also returned,
2835 /// indicating whether the rounded angle is less than, equal to, or greater than the exact
2836 /// angle. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
2837 /// `NaN` it also returns `Equal`.
2838 ///
2839 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2840 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2841 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2842 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2843 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2844 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2845 ///
2846 /// # Panics
2847 /// Panics if `prec` is zero.
2848 ///
2849 /// # Examples
2850 /// ```
2851 /// use malachite_base::num::basic::traits::{One, Two};
2852 /// use malachite_float::Float;
2853 /// use std::cmp::Ordering::*;
2854 ///
2855 /// let (t, o) = (&Float::ONE).atan2_pi_prec_ref_ref(&Float::TWO, 10);
2856 /// assert_eq!(t.to_string(), "0.14771");
2857 /// assert_eq!(o, Greater);
2858 /// ```
2859 #[inline]
2860 pub fn atan2_pi_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
2861 self.atan2_with_period_prec_ref_ref(other, 2, prec)
2862 }
2863
2864 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2865 /// positive $x$-axis in half-turns, rounding the result to the specified rounding mode. The
2866 /// [`Float`]s are both taken by value. An [`Ordering`] is also returned, indicating whether the
2867 /// rounded angle is less than, equal to, or greater than the exact angle. Although `NaN`s are
2868 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2869 /// `Equal`.
2870 ///
2871 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2872 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2873 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2874 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2875 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2876 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2877 ///
2878 /// # Panics
2879 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
2880 /// the inputs.
2881 ///
2882 /// # Examples
2883 /// ```
2884 /// use malachite_base::rounding_modes::RoundingMode::*;
2885 /// use malachite_float::Float;
2886 /// use std::cmp::Ordering::*;
2887 ///
2888 /// let (t, o) = Float::from(0.3f64).atan2_pi_round(Float::from(0.4f64), Floor);
2889 /// assert_eq!(t.to_string(), "0.20483276469913342");
2890 /// assert_eq!(o, Less);
2891 /// ```
2892 #[inline]
2893 #[allow(clippy::needless_pass_by_value)]
2894 pub fn atan2_pi_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2895 self.atan2_with_period_round(other, 2, rm)
2896 }
2897
2898 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2899 /// positive $x$-axis in half-turns, rounding the result to the specified rounding mode. The
2900 /// first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
2901 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
2902 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
2903 /// returns a `NaN` it also returns `Equal`.
2904 ///
2905 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2906 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2907 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2908 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2909 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2910 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2911 ///
2912 /// # Panics
2913 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
2914 /// the inputs.
2915 ///
2916 /// # Examples
2917 /// ```
2918 /// use malachite_base::rounding_modes::RoundingMode::*;
2919 /// use malachite_float::Float;
2920 /// use std::cmp::Ordering::*;
2921 ///
2922 /// let (t, o) = Float::from(0.3f64).atan2_pi_round_val_ref(&Float::from(0.4f64), Floor);
2923 /// assert_eq!(t.to_string(), "0.20483276469913342");
2924 /// assert_eq!(o, Less);
2925 /// ```
2926 #[inline]
2927 #[allow(clippy::needless_pass_by_value)]
2928 pub fn atan2_pi_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2929 self.atan2_with_period_round_val_ref(other, 2, rm)
2930 }
2931
2932 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2933 /// positive $x$-axis in half-turns, rounding the result to the specified rounding mode. The
2934 /// first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
2935 /// returned, indicating whether the rounded angle is less than, equal to, or greater than the
2936 /// exact angle. Although `NaN`s are not comparable to any [`Float`], whenever this function
2937 /// returns a `NaN` it also returns `Equal`.
2938 ///
2939 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2940 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2941 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2942 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2943 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2944 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2945 ///
2946 /// # Panics
2947 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
2948 /// the inputs.
2949 ///
2950 /// # Examples
2951 /// ```
2952 /// use malachite_base::rounding_modes::RoundingMode::*;
2953 /// use malachite_float::Float;
2954 /// use std::cmp::Ordering::*;
2955 ///
2956 /// let (t, o) = (&Float::from(0.3f64)).atan2_pi_round_ref_val(Float::from(0.4f64), Floor);
2957 /// assert_eq!(t.to_string(), "0.20483276469913342");
2958 /// assert_eq!(o, Less);
2959 /// ```
2960 #[inline]
2961 #[allow(clippy::needless_pass_by_value)]
2962 pub fn atan2_pi_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2963 self.atan2_with_period_round_ref_val(other, 2, rm)
2964 }
2965
2966 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
2967 /// positive $x$-axis in half-turns, rounding the result to the specified rounding mode. The
2968 /// [`Float`]s are both taken by reference. An [`Ordering`] is also returned, indicating whether
2969 /// the rounded angle is less than, equal to, or greater than the exact angle. Although `NaN`s
2970 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2971 /// `Equal`.
2972 ///
2973 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
2974 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
2975 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
2976 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
2977 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
2978 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
2979 ///
2980 /// # Panics
2981 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
2982 /// the inputs.
2983 ///
2984 /// # Examples
2985 /// ```
2986 /// use malachite_base::rounding_modes::RoundingMode::*;
2987 /// use malachite_float::Float;
2988 /// use std::cmp::Ordering::*;
2989 ///
2990 /// let (t, o) = (&Float::from(0.3f64)).atan2_pi_round_ref_ref(&Float::from(0.4f64), Floor);
2991 /// assert_eq!(t.to_string(), "0.20483276469913342");
2992 /// assert_eq!(o, Less);
2993 /// ```
2994 #[inline]
2995 pub fn atan2_pi_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2996 self.atan2_with_period_round_ref_ref(other, 2, rm)
2997 }
2998
2999 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3000 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
3001 /// specified rounding mode. The first [`Float`] is replaced by the result, and the second is
3002 /// taken by value. An [`Ordering`] is returned, indicating whether the rounded angle is less
3003 /// than, equal to, or greater than the exact angle.
3004 ///
3005 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
3006 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
3007 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
3008 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
3009 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
3010 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
3011 ///
3012 /// # Panics
3013 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
3014 /// with the given precision.
3015 ///
3016 /// # Examples
3017 /// ```
3018 /// use malachite_base::num::basic::traits::{One, Two};
3019 /// use malachite_base::rounding_modes::RoundingMode::*;
3020 /// use malachite_float::Float;
3021 /// use std::cmp::Ordering::*;
3022 ///
3023 /// let mut y = Float::ONE;
3024 /// assert_eq!(y.atan2_pi_prec_round_assign(Float::TWO, 10, Floor), Less);
3025 /// assert_eq!(y.to_string(), "0.14746");
3026 /// ```
3027 #[inline]
3028 #[allow(clippy::needless_pass_by_value)]
3029 pub fn atan2_pi_prec_round_assign(
3030 &mut self,
3031 other: Self,
3032 prec: u64,
3033 rm: RoundingMode,
3034 ) -> Ordering {
3035 self.atan2_with_period_prec_round_assign(other, 2, prec, rm)
3036 }
3037
3038 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3039 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
3040 /// specified rounding mode. The first [`Float`] is replaced by the result, and the second is
3041 /// taken by reference. An [`Ordering`] is returned, indicating whether the rounded angle is
3042 /// less than, equal to, or greater than the exact angle.
3043 ///
3044 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
3045 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
3046 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
3047 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
3048 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
3049 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
3050 ///
3051 /// # Panics
3052 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
3053 /// with the given precision.
3054 ///
3055 /// # Examples
3056 /// ```
3057 /// use malachite_base::num::basic::traits::{One, Two};
3058 /// use malachite_base::rounding_modes::RoundingMode::*;
3059 /// use malachite_float::Float;
3060 /// use std::cmp::Ordering::*;
3061 ///
3062 /// let mut y = Float::ONE;
3063 /// assert_eq!(
3064 /// y.atan2_pi_prec_round_assign_ref(&Float::TWO, 10, Floor),
3065 /// Less
3066 /// );
3067 /// assert_eq!(y.to_string(), "0.14746");
3068 /// ```
3069 #[inline]
3070 pub fn atan2_pi_prec_round_assign_ref(
3071 &mut self,
3072 other: &Self,
3073 prec: u64,
3074 rm: RoundingMode,
3075 ) -> Ordering {
3076 self.atan2_with_period_prec_round_assign_ref(other, 2, prec, rm)
3077 }
3078
3079 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3080 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
3081 /// precision. The first [`Float`] is replaced by the result, and the second is taken by value.
3082 /// An [`Ordering`] is returned, indicating whether the rounded angle is less than, equal to, or
3083 /// greater than the exact angle.
3084 ///
3085 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
3086 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
3087 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
3088 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
3089 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
3090 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
3091 ///
3092 /// # Panics
3093 /// Panics if `prec` is zero.
3094 ///
3095 /// # Examples
3096 /// ```
3097 /// use malachite_base::num::basic::traits::{One, Two};
3098 /// use malachite_float::Float;
3099 /// use std::cmp::Ordering::*;
3100 ///
3101 /// let mut y = Float::ONE;
3102 /// assert_eq!(y.atan2_pi_prec_assign(Float::TWO, 10), Greater);
3103 /// assert_eq!(y.to_string(), "0.14771");
3104 /// ```
3105 #[inline]
3106 #[allow(clippy::needless_pass_by_value)]
3107 pub fn atan2_pi_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
3108 self.atan2_with_period_prec_assign(other, 2, prec)
3109 }
3110
3111 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3112 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
3113 /// precision. The first [`Float`] is replaced by the result, and the second is taken by
3114 /// reference. An [`Ordering`] is returned, indicating whether the rounded angle is less than,
3115 /// equal to, or greater than the exact angle.
3116 ///
3117 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
3118 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
3119 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
3120 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
3121 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
3122 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
3123 ///
3124 /// # Panics
3125 /// Panics if `prec` is zero.
3126 ///
3127 /// # Examples
3128 /// ```
3129 /// use malachite_base::num::basic::traits::{One, Two};
3130 /// use malachite_float::Float;
3131 /// use std::cmp::Ordering::*;
3132 ///
3133 /// let mut y = Float::ONE;
3134 /// assert_eq!(y.atan2_pi_prec_assign_ref(&Float::TWO, 10), Greater);
3135 /// assert_eq!(y.to_string(), "0.14771");
3136 /// ```
3137 #[inline]
3138 pub fn atan2_pi_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
3139 self.atan2_with_period_prec_assign_ref(other, 2, prec)
3140 }
3141
3142 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3143 /// positive $x$-axis in half-turns, rounding the result to the specified rounding mode. The
3144 /// first [`Float`] is replaced by the result, and the second is taken by value. An [`Ordering`]
3145 /// is returned, indicating whether the rounded angle is less than, equal to, or greater than
3146 /// the exact angle.
3147 ///
3148 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
3149 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
3150 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
3151 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
3152 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
3153 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
3154 ///
3155 /// # Panics
3156 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
3157 /// the inputs.
3158 ///
3159 /// # Examples
3160 /// ```
3161 /// use malachite_base::num::basic::traits::{One, Two};
3162 /// use malachite_base::rounding_modes::RoundingMode::*;
3163 /// use malachite_float::Float;
3164 /// use std::cmp::Ordering::*;
3165 ///
3166 /// let mut y = Float::ONE;
3167 /// assert_eq!(y.atan2_pi_round_assign(Float::TWO, Floor), Less);
3168 /// assert_eq!(y.to_string(), "0.12");
3169 /// ```
3170 #[inline]
3171 #[allow(clippy::needless_pass_by_value)]
3172 pub fn atan2_pi_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
3173 self.atan2_with_period_round_assign(other, 2, rm)
3174 }
3175
3176 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3177 /// positive $x$-axis in half-turns, rounding the result to the specified rounding mode. The
3178 /// first [`Float`] is replaced by the result, and the second is taken by reference. An
3179 /// [`Ordering`] is returned, indicating whether the rounded angle is less than, equal to, or
3180 /// greater than the exact angle.
3181 ///
3182 /// This is `atan2_with_period` with a period of 2: see [`Float::atan2_with_period_prec_round`]
3183 /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
3184 /// infinite $y$ gives $\pm1/4$ against $+\infty$ and $\pm3/4$ against $-\infty$, and $\pm1/2$
3185 /// against a finite $x$; a zero $y$ gives $\pm0.0$ for a positive-signed $x$ and $\pm1$ for a
3186 /// negative-signed one; a zero $x$ gives $\pm1/2$; and the quadrant diagonals give $\pm1/4$ and
3187 /// $\pm3/4$. All of those are exact at every precision except $\pm3/4$, which needs two bits.
3188 ///
3189 /// # Panics
3190 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
3191 /// the inputs.
3192 ///
3193 /// # Examples
3194 /// ```
3195 /// use malachite_base::num::basic::traits::{One, Two};
3196 /// use malachite_base::rounding_modes::RoundingMode::*;
3197 /// use malachite_float::Float;
3198 /// use std::cmp::Ordering::*;
3199 ///
3200 /// let mut y = Float::ONE;
3201 /// assert_eq!(y.atan2_pi_round_assign_ref(&Float::TWO, Floor), Less);
3202 /// assert_eq!(y.to_string(), "0.12");
3203 /// ```
3204 #[inline]
3205 pub fn atan2_pi_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
3206 self.atan2_with_period_round_assign_ref(other, 2, rm)
3207 }
3208
3209 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3210 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
3211 /// specified rounding mode and returning the result as a [`Float`]. The [`Rational`]s are both
3212 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded angle is
3213 /// less than, equal to, or greater than the exact angle.
3214 ///
3215 /// This is `atan2_with_period_rational` with a period of 2: see
3216 /// [`Float::atan2_with_period_rational_prec_round`] for the error bounds, the special cases,
3217 /// underflow, and the complexity, with $u = 2$. A zero $y$ gives $0.0$ for a nonnegative $x$
3218 /// and $1$ for a negative one, a zero $x$ gives $\pm1/2$ with the sign of $y$, and the quadrant
3219 /// diagonals give $\pm1/4$ and $\pm3/4$. All of those are exact at every precision except
3220 /// $\pm3/4$, which needs two bits.
3221 ///
3222 /// # Panics
3223 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
3224 /// with the given precision.
3225 ///
3226 /// # Examples
3227 /// ```
3228 /// use malachite_base::num::basic::traits::One;
3229 /// use malachite_base::rounding_modes::RoundingMode::*;
3230 /// use malachite_float::Float;
3231 /// use malachite_q::Rational;
3232 /// use std::cmp::Ordering::*;
3233 ///
3234 /// // the first quadrant's diagonal is a quarter turn
3235 /// let (t, o) = Float::atan2_pi_rational_prec_round(Rational::ONE, Rational::ONE, 10, Exact);
3236 /// assert_eq!(t.to_string(), "0.25000");
3237 /// assert_eq!(o, Equal);
3238 ///
3239 /// let (t, o) =
3240 /// Float::atan2_pi_rational_prec_round(Rational::from(3), Rational::from(4), 10, Floor);
3241 /// assert_eq!(t.to_string(), "0.20459");
3242 /// assert_eq!(o, Less);
3243 /// ```
3244 #[inline]
3245 #[allow(clippy::needless_pass_by_value)]
3246 pub fn atan2_pi_rational_prec_round(
3247 y: Rational,
3248 x: Rational,
3249 prec: u64,
3250 rm: RoundingMode,
3251 ) -> (Self, Ordering) {
3252 Self::atan2_with_period_rational_prec_round(y, x, 2, prec, rm)
3253 }
3254
3255 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3256 /// positive $x$-axis in half-turns, rounding the result to the specified precision and with the
3257 /// specified rounding mode and returning the result as a [`Float`]. The [`Rational`]s are both
3258 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded angle
3259 /// is less than, equal to, or greater than the exact angle.
3260 ///
3261 /// This is `atan2_with_period_rational` with a period of 2: see
3262 /// [`Float::atan2_with_period_rational_prec_round`] for the error bounds, the special cases,
3263 /// underflow, and the complexity, with $u = 2$. A zero $y$ gives $0.0$ for a nonnegative $x$
3264 /// and $1$ for a negative one, a zero $x$ gives $\pm1/2$ with the sign of $y$, and the quadrant
3265 /// diagonals give $\pm1/4$ and $\pm3/4$. All of those are exact at every precision except
3266 /// $\pm3/4$, which needs two bits.
3267 ///
3268 /// # Panics
3269 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
3270 /// with the given precision.
3271 ///
3272 /// # Examples
3273 /// ```
3274 /// use malachite_base::rounding_modes::RoundingMode::*;
3275 /// use malachite_float::Float;
3276 /// use malachite_q::Rational;
3277 /// use std::cmp::Ordering::*;
3278 ///
3279 /// let (t, o) = Float::atan2_pi_rational_prec_round_ref(
3280 /// &Rational::from(3),
3281 /// &Rational::from(4),
3282 /// 10,
3283 /// Ceiling,
3284 /// );
3285 /// assert_eq!(t.to_string(), "0.20483");
3286 /// assert_eq!(o, Greater);
3287 /// ```
3288 #[inline]
3289 pub fn atan2_pi_rational_prec_round_ref(
3290 y: &Rational,
3291 x: &Rational,
3292 prec: u64,
3293 rm: RoundingMode,
3294 ) -> (Self, Ordering) {
3295 Self::atan2_with_period_rational_prec_round_ref(y, x, 2, prec, rm)
3296 }
3297
3298 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3299 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
3300 /// precision and returning the result as a [`Float`]. The [`Rational`]s are both taken by
3301 /// value. An [`Ordering`] is also returned, indicating whether the rounded angle is less than,
3302 /// equal to, or greater than the exact angle.
3303 ///
3304 /// This is `atan2_with_period_rational` with a period of 2: see
3305 /// [`Float::atan2_with_period_rational_prec_round`] for the error bounds, the special cases,
3306 /// underflow, and the complexity, with $u = 2$. A zero $y$ gives $0.0$ for a nonnegative $x$
3307 /// and $1$ for a negative one, a zero $x$ gives $\pm1/2$ with the sign of $y$, and the quadrant
3308 /// diagonals give $\pm1/4$ and $\pm3/4$. All of those are exact at every precision except
3309 /// $\pm3/4$, which needs two bits.
3310 ///
3311 /// # Panics
3312 /// Panics if `prec` is zero.
3313 ///
3314 /// # Examples
3315 /// ```
3316 /// use malachite_float::Float;
3317 /// use malachite_q::Rational;
3318 /// use std::cmp::Ordering::*;
3319 ///
3320 /// let (t, o) = Float::atan2_pi_rational_prec(Rational::from(3), Rational::from(4), 53);
3321 /// assert_eq!(t.to_string(), "0.20483276469913345");
3322 /// assert_eq!(o, Less);
3323 /// ```
3324 #[inline]
3325 #[allow(clippy::needless_pass_by_value)]
3326 pub fn atan2_pi_rational_prec(y: Rational, x: Rational, prec: u64) -> (Self, Ordering) {
3327 Self::atan2_with_period_rational_prec(y, x, 2, prec)
3328 }
3329
3330 /// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3331 /// positive $x$-axis in half-turns, rounding the result to the nearest value of the specified
3332 /// precision and returning the result as a [`Float`]. The [`Rational`]s are both taken by
3333 /// reference. An [`Ordering`] is also returned, indicating whether the rounded angle is less
3334 /// than, equal to, or greater than the exact angle.
3335 ///
3336 /// This is `atan2_with_period_rational` with a period of 2: see
3337 /// [`Float::atan2_with_period_rational_prec_round`] for the error bounds, the special cases,
3338 /// underflow, and the complexity, with $u = 2$. A zero $y$ gives $0.0$ for a nonnegative $x$
3339 /// and $1$ for a negative one, a zero $x$ gives $\pm1/2$ with the sign of $y$, and the quadrant
3340 /// diagonals give $\pm1/4$ and $\pm3/4$. All of those are exact at every precision except
3341 /// $\pm3/4$, which needs two bits.
3342 ///
3343 /// # Panics
3344 /// Panics if `prec` is zero.
3345 ///
3346 /// # Examples
3347 /// ```
3348 /// use malachite_float::Float;
3349 /// use malachite_q::Rational;
3350 /// use std::cmp::Ordering::*;
3351 ///
3352 /// let (t, o) = Float::atan2_pi_rational_prec_ref(&Rational::from(3), &Rational::from(4), 53);
3353 /// assert_eq!(t.to_string(), "0.20483276469913345");
3354 /// assert_eq!(o, Less);
3355 /// ```
3356 #[inline]
3357 pub fn atan2_pi_rational_prec_ref(y: &Rational, x: &Rational, prec: u64) -> (Self, Ordering) {
3358 Self::atan2_with_period_rational_prec_ref(y, x, 2, prec)
3359 }
3360}
3361
3362impl Atan2<Self> for Float {
3363 type Output = Self;
3364
3365 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
3366 /// positive $x$-axis, taking both [`Float`]s by value.
3367 ///
3368 /// The precision of the output is the maximum of the precisions of the inputs, and the result
3369 /// is rounded to nearest. See [`Float::atan2_prec_round`] for the error bounds, the special
3370 /// cases, underflow, and the complexity.
3371 ///
3372 /// # Examples
3373 /// ```
3374 /// use malachite_base::num::arithmetic::traits::Atan2;
3375 /// use malachite_float::Float;
3376 ///
3377 /// assert_eq!(
3378 /// Float::from(0.3f64).atan2(Float::from(0.4f64)).to_string(),
3379 /// "0.64350110879328437"
3380 /// );
3381 /// ```
3382 #[inline]
3383 fn atan2(self, other: Self) -> Self {
3384 self.atan2_round_ref_ref(&other, Nearest).0
3385 }
3386}
3387
3388impl Atan2<&Self> for Float {
3389 type Output = Self;
3390
3391 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
3392 /// positive $x$-axis, taking the first [`Float`] by value and the second by reference.
3393 ///
3394 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
3395 /// complexity.
3396 ///
3397 /// # Examples
3398 /// ```
3399 /// use malachite_base::num::arithmetic::traits::Atan2;
3400 /// use malachite_float::Float;
3401 ///
3402 /// assert_eq!(
3403 /// Float::from(0.3f64).atan2(&Float::from(0.4f64)).to_string(),
3404 /// "0.64350110879328437"
3405 /// );
3406 /// ```
3407 #[inline]
3408 fn atan2(self, other: &Self) -> Self {
3409 self.atan2_round_ref_ref(other, Nearest).0
3410 }
3411}
3412
3413impl Atan2<Float> for &Float {
3414 type Output = Float;
3415
3416 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
3417 /// positive $x$-axis, taking the first [`Float`] by reference and the second by value.
3418 ///
3419 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
3420 /// complexity.
3421 ///
3422 /// # Examples
3423 /// ```
3424 /// use malachite_base::num::arithmetic::traits::Atan2;
3425 /// use malachite_float::Float;
3426 ///
3427 /// assert_eq!(
3428 /// (&Float::from(0.3f64))
3429 /// .atan2(Float::from(0.4f64))
3430 /// .to_string(),
3431 /// "0.64350110879328437"
3432 /// );
3433 /// ```
3434 #[inline]
3435 fn atan2(self, other: Float) -> Float {
3436 self.atan2_round_ref_ref(&other, Nearest).0
3437 }
3438}
3439
3440impl Atan2<&Float> for &Float {
3441 type Output = Float;
3442
3443 /// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the
3444 /// positive $x$-axis, taking both [`Float`]s by reference.
3445 ///
3446 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
3447 /// complexity.
3448 ///
3449 /// # Examples
3450 /// ```
3451 /// use malachite_base::num::arithmetic::traits::Atan2;
3452 /// use malachite_float::Float;
3453 ///
3454 /// assert_eq!(
3455 /// (&Float::from(0.3f64))
3456 /// .atan2(&Float::from(0.4f64))
3457 /// .to_string(),
3458 /// "0.64350110879328437"
3459 /// );
3460 /// ```
3461 #[inline]
3462 fn atan2(self, other: &Float) -> Float {
3463 self.atan2_round_ref_ref(other, Nearest).0
3464 }
3465}
3466
3467impl Atan2Assign<Self> for Float {
3468 /// Replaces a [`Float`] $y$ with $\operatorname{atan2}(y,x)$, taking $x$ by value.
3469 ///
3470 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
3471 /// complexity.
3472 ///
3473 /// # Examples
3474 /// ```
3475 /// use malachite_base::num::arithmetic::traits::Atan2Assign;
3476 /// use malachite_float::Float;
3477 ///
3478 /// let mut y = Float::from(0.3f64);
3479 /// y.atan2_assign(Float::from(0.4f64));
3480 /// assert_eq!(y.to_string(), "0.64350110879328437");
3481 /// ```
3482 #[inline]
3483 fn atan2_assign(&mut self, other: Self) {
3484 self.atan2_round_assign_ref(&other, Nearest);
3485 }
3486}
3487
3488impl Atan2Assign<&Self> for Float {
3489 /// Replaces a [`Float`] $y$ with $\operatorname{atan2}(y,x)$, taking $x$ by reference.
3490 ///
3491 /// See [`Float::atan2_prec_round`] for the error bounds, the special cases, underflow, and the
3492 /// complexity.
3493 ///
3494 /// # Examples
3495 /// ```
3496 /// use malachite_base::num::arithmetic::traits::Atan2Assign;
3497 /// use malachite_float::Float;
3498 ///
3499 /// let mut y = Float::from(0.3f64);
3500 /// y.atan2_assign(&Float::from(0.4f64));
3501 /// assert_eq!(y.to_string(), "0.64350110879328437");
3502 /// ```
3503 #[inline]
3504 fn atan2_assign(&mut self, other: &Self) {
3505 self.atan2_round_assign_ref(other, Nearest);
3506 }
3507}
3508
3509/// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the positive
3510/// $x$-axis, for primitive floats.
3511///
3512/// $$
3513/// f(y,x) = \operatorname{atan2}(y,x)+\varepsilon,
3514/// $$
3515/// where $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atan2}(y,x)|\rfloor-p}$ and $p$ is the
3516/// precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]); the special cases
3517/// below are exact.
3518///
3519/// Special cases, in which the sign of a zero argument selects the quadrant:
3520/// - $f(\text{NaN},x)=f(y,\text{NaN})=\text{NaN}$
3521/// - $f(\pm0.0,x)=\pm0.0$ if $x$ is positive or $+0.0$, and $\pm\pi$ if $x$ is negative or $-0.0$
3522/// - $f(y,\pm0.0)=\pm\pi/2$, with the sign of $y$, for nonzero $y$
3523/// - $f(\pm\infty,x)=\pm\pi/2$ for finite $x$, $\pm\pi/4$ for $+\infty$, and $\pm3\pi/4$ for
3524/// $-\infty$
3525/// - $f(y,+\infty)=\pm0.0$ and $f(y,-\infty)=\pm\pi$, with the sign of $y$, for finite nonzero $y$
3526///
3527/// Overflow is not possible, since $|\operatorname{atan2}(y,x)| \leq \pi$. The result is subnormal,
3528/// or zero, only for a positive $x$ with $|y/x|$ subnormal or smaller.
3529///
3530/// # Worst-case complexity
3531/// Constant time and additional memory.
3532///
3533/// # Examples
3534/// ```
3535/// use malachite_base::num::float::NiceFloat;
3536/// use malachite_float::float::arithmetic::atan2::primitive_float_atan2;
3537///
3538/// assert!(primitive_float_atan2(f32::NAN, 1.0).is_nan());
3539/// assert_eq!(
3540/// NiceFloat(primitive_float_atan2(1.0f32, 1.0)),
3541/// NiceFloat(0.7853982)
3542/// );
3543/// assert_eq!(
3544/// NiceFloat(primitive_float_atan2(1.0f64, 1.0)),
3545/// NiceFloat(0.7853981633974483)
3546/// );
3547/// // a negative x with a zero y is half a turn
3548/// assert_eq!(
3549/// NiceFloat(primitive_float_atan2(0.0f64, -1.0)),
3550/// NiceFloat(3.141592653589793)
3551/// );
3552/// assert_eq!(
3553/// NiceFloat(primitive_float_atan2(-0.0f64, -1.0)),
3554/// NiceFloat(-3.141592653589793)
3555/// );
3556/// ```
3557#[inline]
3558#[allow(clippy::type_repetition_in_bounds)]
3559pub fn primitive_float_atan2<T: PrimitiveFloat>(y: T, x: T) -> T
3560where
3561 Float: From<T> + PartialOrd<T>,
3562 for<'a> T: ExactFrom<&'a Float>,
3563{
3564 emulate_float_float_to_float_fn(|y, x, prec| y.atan2_prec_ref_ref(&x, prec), y, x)
3565}
3566
3567/// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the positive
3568/// $x$-axis, for [`Rational`]s, returning the result as a primitive float.
3569///
3570/// $$
3571/// f(y,x) = \operatorname{atan2}(y,x)+\varepsilon,
3572/// $$
3573/// where $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atan2}(y,x)|\rfloor-p}$ and $p$ is the
3574/// precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]); the zero case below
3575/// is exact.
3576///
3577/// Special cases:
3578/// - $f(0,x)=0.0$ if $x \geq 0$, and $\pi$ if $x < 0$
3579/// - $f(y,0)=\pm\pi/2$, with the sign of $y$, for nonzero $y$
3580///
3581/// Overflow is not possible, since $|\operatorname{atan2}(y,x)| \leq \pi$. The result is subnormal,
3582/// or zero, only for a positive $x$ with $|y/x|$ subnormal or smaller.
3583///
3584/// # Worst-case complexity
3585/// $T(m) = O(m \log m \log\log m)$
3586///
3587/// $M(m) = O(m \log m)$
3588///
3589/// where $T$ is time, $M$ is additional memory, and $m$ is `max(y.significant_bits(),
3590/// x.significant_bits())`.
3591///
3592/// # Examples
3593/// ```
3594/// use malachite_base::num::basic::traits::{NegativeOne, Zero};
3595/// use malachite_base::num::float::NiceFloat;
3596/// use malachite_float::float::arithmetic::atan2::primitive_float_atan2_rational;
3597/// use malachite_q::Rational;
3598///
3599/// assert_eq!(
3600/// NiceFloat(primitive_float_atan2_rational::<f64>(
3601/// &Rational::from(3),
3602/// &Rational::from(4)
3603/// )),
3604/// NiceFloat(0.6435011087932844)
3605/// );
3606/// assert_eq!(
3607/// NiceFloat(primitive_float_atan2_rational::<f32>(
3608/// &Rational::from(3),
3609/// &Rational::from(4)
3610/// )),
3611/// NiceFloat(0.6435011)
3612/// );
3613/// // a negative x with a zero y is half a turn
3614/// assert_eq!(
3615/// NiceFloat(primitive_float_atan2_rational::<f64>(
3616/// &Rational::ZERO,
3617/// &Rational::NEGATIVE_ONE
3618/// )),
3619/// NiceFloat(3.141592653589793)
3620/// );
3621/// ```
3622#[inline]
3623#[allow(clippy::type_repetition_in_bounds)]
3624pub fn primitive_float_atan2_rational<T: PrimitiveFloat>(y: &Rational, x: &Rational) -> T
3625where
3626 Float: PartialOrd<T>,
3627 for<'a> T: ExactFrom<&'a Float>,
3628{
3629 emulate_rational_rational_to_float_fn(Float::atan2_rational_prec_ref, y, x)
3630}
3631
3632/// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from the
3633/// positive $x$-axis in $u$ths of a turn (so that `u = 360` gives degrees), for primitive floats.
3634///
3635/// $$
3636/// f(y,x,u) = \operatorname{atan2}(y,x)u/(2\pi)+\varepsilon,
3637/// $$
3638/// where $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atan2}(y,x)u/(2\pi)|\rfloor-p}$ and $p$
3639/// is the precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]); the special
3640/// cases below are exact when the output can hold them.
3641///
3642/// Special cases, in which the sign of a zero argument selects the quadrant:
3643/// - $f(\text{NaN},x,u)=f(y,\text{NaN},u)=\text{NaN}$
3644/// - $f(\pm\infty,+\infty,u)=\pm u/8$ and $f(\pm\infty,-\infty,u)=\pm3u/8$
3645/// - $f(\pm\infty,x,u)=\pm u/4$ for finite $x$
3646/// - $f(y,+\infty,u)=\pm0.0$ and $f(y,-\infty,u)=\pm u/2$, with the sign of $y$
3647/// - $f(\pm0.0,x,u)=\pm0.0$ if $x$ is positive or $+0.0$, and $\pm u/2$ otherwise
3648/// - $f(y,\pm0.0,u)=\pm u/4$, with the sign of $y$, for nonzero $y$
3649/// - $f(\pm x,x,u)=\pm u/8$ for positive $x$, and $\pm3u/8$ for negative $x$
3650/// - $f(y,x,0)=\pm0.0$, with the sign of $y$
3651///
3652/// Overflow is not possible, since $|f(y,x,u)| \leq u/2 < 2^{63}$. The result is subnormal, or
3653/// zero, only for a positive $x$ with $|y/x|$ tiny and $u$ small.
3654///
3655/// # Worst-case complexity
3656/// Constant time and additional memory.
3657///
3658/// # Examples
3659/// ```
3660/// use malachite_base::num::float::NiceFloat;
3661/// use malachite_float::float::arithmetic::atan2::primitive_float_atan2_with_period;
3662///
3663/// assert!(primitive_float_atan2_with_period(f32::NAN, 1.0, 360).is_nan());
3664/// // the first quadrant's diagonal is an eighth of a turn
3665/// assert_eq!(
3666/// NiceFloat(primitive_float_atan2_with_period(1.0f32, 1.0, 360)),
3667/// NiceFloat(45.0)
3668/// );
3669/// // the second quadrant's diagonal is three eighths
3670/// assert_eq!(
3671/// NiceFloat(primitive_float_atan2_with_period(1.0f32, -1.0, 360)),
3672/// NiceFloat(135.0)
3673/// );
3674/// assert_eq!(
3675/// NiceFloat(primitive_float_atan2_with_period(3.0f64, 4.0, 360)),
3676/// NiceFloat(36.86989764584402)
3677/// );
3678/// // a negative x with a zero y is half a turn
3679/// assert_eq!(
3680/// NiceFloat(primitive_float_atan2_with_period(0.0f64, -1.0, 360)),
3681/// NiceFloat(180.0)
3682/// );
3683/// ```
3684#[inline]
3685#[allow(clippy::type_repetition_in_bounds)]
3686pub fn primitive_float_atan2_with_period<T: PrimitiveFloat>(y: T, x: T, u: u64) -> T
3687where
3688 Float: From<T> + PartialOrd<T>,
3689 for<'a> T: ExactFrom<&'a Float>,
3690{
3691 emulate_float_float_to_float_fn(
3692 |y, x, prec| y.atan2_with_period_prec_ref_ref(&x, u, prec),
3693 y,
3694 x,
3695 )
3696}
3697
3698/// Computes $\operatorname{atan2}(y,x)u/(2\pi)$, the angle of the point $(x,y)$ measured from the
3699/// positive $x$-axis in $u$ths of a turn (so that `u = 360` gives degrees), for [`Rational`]s,
3700/// returning the result as a primitive float.
3701///
3702/// $$
3703/// f(y,x,u) = \operatorname{atan2}(y,x)u/(2\pi)+\varepsilon,
3704/// $$
3705/// where $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atan2}(y,x)u/(2\pi)|\rfloor-p}$ and $p$
3706/// is the precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]); the special
3707/// cases below are exact when the output can hold them.
3708///
3709/// Special cases:
3710/// - $f(0,x,u)=0.0$ if $x \geq 0$, and $u/2$ if $x < 0$
3711/// - $f(y,0,u)=\pm u/4$, with the sign of $y$, for nonzero $y$
3712/// - $f(\pm x,x,u)=\pm u/8$ for positive $x$, and $\pm3u/8$ for negative $x$
3713/// - $f(y,x,0)=0.0$
3714///
3715/// Overflow is not possible, since $|f(y,x,u)| \leq u/2 < 2^{63}$. The result is subnormal, or
3716/// zero, only for a positive $x$ with $|y/x|$ tiny and $u$ small.
3717///
3718/// # Worst-case complexity
3719/// $T(m) = O(m \log m \log\log m)$
3720///
3721/// $M(m) = O(m \log m)$
3722///
3723/// where $T$ is time, $M$ is additional memory, and $m$ is `max(y.significant_bits(),
3724/// x.significant_bits())`.
3725///
3726/// # Examples
3727/// ```
3728/// use malachite_base::num::basic::traits::{NegativeOne, Zero};
3729/// use malachite_base::num::float::NiceFloat;
3730/// use malachite_float::float::arithmetic::atan2::primitive_float_atan2_with_period_rational;
3731/// use malachite_q::Rational;
3732///
3733/// assert_eq!(
3734/// NiceFloat(primitive_float_atan2_with_period_rational::<f64>(
3735/// &Rational::from(3),
3736/// &Rational::from(4),
3737/// 360
3738/// )),
3739/// NiceFloat(36.86989764584402)
3740/// );
3741/// assert_eq!(
3742/// NiceFloat(primitive_float_atan2_with_period_rational::<f32>(
3743/// &Rational::from(3),
3744/// &Rational::from(4),
3745/// 360
3746/// )),
3747/// NiceFloat(36.869896)
3748/// );
3749/// // a negative x with a zero y is half a turn
3750/// assert_eq!(
3751/// NiceFloat(primitive_float_atan2_with_period_rational::<f64>(
3752/// &Rational::ZERO,
3753/// &Rational::NEGATIVE_ONE,
3754/// 360
3755/// )),
3756/// NiceFloat(180.0)
3757/// );
3758/// ```
3759#[inline]
3760#[allow(clippy::type_repetition_in_bounds)]
3761pub fn primitive_float_atan2_with_period_rational<T: PrimitiveFloat>(
3762 y: &Rational,
3763 x: &Rational,
3764 u: u64,
3765) -> T
3766where
3767 Float: PartialOrd<T>,
3768 for<'a> T: ExactFrom<&'a Float>,
3769{
3770 emulate_rational_rational_to_float_fn(
3771 |y, x, prec| Float::atan2_with_period_rational_prec_ref(y, x, u, prec),
3772 y,
3773 x,
3774 )
3775}
3776
3777/// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3778/// positive $x$-axis in half-turns, for primitive floats.
3779///
3780/// This is `primitive_float_atan2_with_period` with a period of 2: see
3781/// [`primitive_float_atan2_with_period`] for the error bound and the special cases, with $u = 2$.
3782///
3783/// # Worst-case complexity
3784/// Constant time and additional memory.
3785///
3786/// # Examples
3787/// ```
3788/// use malachite_base::num::float::NiceFloat;
3789/// use malachite_float::float::arithmetic::atan2::primitive_float_atan2_pi;
3790///
3791/// assert!(primitive_float_atan2_pi(f32::NAN, 1.0).is_nan());
3792/// // the first quadrant's diagonal is a quarter turn
3793/// assert_eq!(
3794/// NiceFloat(primitive_float_atan2_pi(1.0f32, 1.0)),
3795/// NiceFloat(0.25)
3796/// );
3797/// // the second quadrant's is three quarters
3798/// assert_eq!(
3799/// NiceFloat(primitive_float_atan2_pi(1.0f32, -1.0)),
3800/// NiceFloat(0.75)
3801/// );
3802/// assert_eq!(
3803/// NiceFloat(primitive_float_atan2_pi(3.0f64, 4.0)),
3804/// NiceFloat(0.20483276469913345)
3805/// );
3806/// ```
3807#[inline]
3808#[allow(clippy::type_repetition_in_bounds)]
3809pub fn primitive_float_atan2_pi<T: PrimitiveFloat>(y: T, x: T) -> T
3810where
3811 Float: From<T> + PartialOrd<T>,
3812 for<'a> T: ExactFrom<&'a Float>,
3813{
3814 primitive_float_atan2_with_period(y, x, 2)
3815}
3816
3817/// Computes $\operatorname{atan2}(y,x)/\pi$, the angle of the point $(x,y)$ measured from the
3818/// positive $x$-axis in half-turns, for [`Rational`]s, returning the result as a primitive float.
3819///
3820/// This is `primitive_float_atan2_with_period_rational` with a period of 2: see
3821/// [`primitive_float_atan2_with_period_rational`] for the error bound and the special cases, with
3822/// $u = 2$.
3823///
3824/// # Worst-case complexity
3825/// $T(m) = O(m \log m \log\log m)$
3826///
3827/// $M(m) = O(m \log m)$
3828///
3829/// where $T$ is time, $M$ is additional memory, and $m$ is `max(y.significant_bits(),
3830/// x.significant_bits())`.
3831///
3832/// # Examples
3833/// ```
3834/// use malachite_base::num::basic::traits::{NegativeOne, Zero};
3835/// use malachite_base::num::float::NiceFloat;
3836/// use malachite_float::float::arithmetic::atan2::primitive_float_atan2_pi_rational;
3837/// use malachite_q::Rational;
3838///
3839/// assert_eq!(
3840/// NiceFloat(primitive_float_atan2_pi_rational::<f64>(
3841/// &Rational::from(3),
3842/// &Rational::from(4)
3843/// )),
3844/// NiceFloat(0.20483276469913345)
3845/// );
3846/// // a negative x with a zero y is half a turn
3847/// assert_eq!(
3848/// NiceFloat(primitive_float_atan2_pi_rational::<f64>(
3849/// &Rational::ZERO,
3850/// &Rational::NEGATIVE_ONE
3851/// )),
3852/// NiceFloat(1.0)
3853/// );
3854/// ```
3855#[inline]
3856#[allow(clippy::type_repetition_in_bounds)]
3857pub fn primitive_float_atan2_pi_rational<T: PrimitiveFloat>(y: &Rational, x: &Rational) -> T
3858where
3859 Float: PartialOrd<T>,
3860 for<'a> T: ExactFrom<&'a Float>,
3861{
3862 primitive_float_atan2_with_period_rational(y, x, 2)
3863}