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