use crate::Float;
use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
use crate::float::MAX_EXPONENT_I64;
use crate::float::arithmetic::acsc::signed_half_pi;
use crate::float::arithmetic::atan::{
arc_with_period_scale, atan_rational_helper, scaled_unsigned,
};
use crate::float::arithmetic::round_near_x::{round_from_above, value_is_tie};
use crate::float::arithmetic::sin::{SCALE, SCALE_I64, SCALED_INPUT_EXPONENT};
use crate::{emulate_float_to_float_fn, emulate_rational_to_float_fn};
use core::cmp::Ordering::{self, Equal, Greater, Less};
use malachite_base::num::arithmetic::traits::{
Abs, Acot, AcotAssign, CeilingLogBase2, IsPowerOf2, NegAssign, PowerOf2, Reciprocal,
};
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::{NaN as NaNTrait, NegativeZero, Zero as ZeroTrait};
use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use malachite_nz::natural::arithmetic::float::round::float_can_round;
use malachite_nz::platform::Limb;
use malachite_q::Rational;
fn acot_abs_prec_round(x: &Float, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
let exp_x = i64::from(x.get_exponent().unwrap());
let xp = x.abs();
if exp_x << 1 > MAX_EXPONENT_I64
|| exp_x << 1 > i64::exact_from(prec + x.get_prec().unwrap()) + 4
{
let tie = rm == Nearest && {
let (wide, o_wide) = xp.reciprocal_prec_ref(prec + 1);
value_is_tie(&wide, o_wide, prec)
};
let (t, o) = xp.reciprocal_prec_round(prec, rm);
return round_from_above(t, o, tie, rm);
}
let big = exp_x >= 1;
let mut w = prec + prec.ceiling_log_base_2() + 10;
let mut increment = Limb::WIDTH;
loop {
let t = if big {
xp.reciprocal_prec_ref(w).0.atan_prec(w).0
} else {
(Float::pi_prec(w).0 >> 1u32)
.sub_prec(xp.atan_prec_ref(w).0, w)
.0
};
if float_can_round(t.significand_ref().unwrap(), w - 4, prec, rm) {
return Float::from_float_prec_round(t, prec, rm);
}
w += increment;
increment = w >> 1;
}
}
fn acot_prec_round_normal_ref(x: &Float, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
assert_ne!(rm, Exact, "Inexact acot");
let negative = *x < 0u32;
let rm_abs = if negative { -rm } else { rm };
let (t, o) = if x.partial_cmp_abs(&1u32).unwrap() == Equal {
let (pi, o) = Float::pi_prec_round(prec, rm_abs);
(pi >> 2u32, o)
} else {
acot_abs_prec_round(x, prec, rm_abs)
};
if negative { (-t, o.reverse()) } else { (t, o) }
}
pub(crate) fn acot_rational_helper(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
assert_ne!(rm, Exact, "Inexact acot_rational");
let negative = *x < 0u32;
let rm_abs = if negative { -rm } else { rm };
let xp = x.abs();
let (t, o) = match xp.partial_cmp(&1u32).unwrap() {
Equal => {
let (pi, o) = Float::pi_prec_round(prec, rm_abs);
(pi >> 2u32, o)
}
Greater => Float::atan_rational_prec_round((&xp).reciprocal(), prec, rm_abs),
Less => {
let mut w = prec + prec.ceiling_log_base_2() + 10;
let mut increment = Limb::WIDTH;
loop {
let t = (Float::pi_prec(w).0 >> 1u32)
.sub_prec(atan_rational_helper(&xp, w, Nearest).0, w)
.0;
if float_can_round(t.significand_ref().unwrap(), w - 4, prec, rm_abs) {
break Float::from_float_prec_round(t, prec, rm_abs);
}
w += increment;
increment = w >> 1;
}
}
};
if negative { (-t, o.reverse()) } else { (t, o) }
}
fn acot_with_period_prec_round_normal_ref(
x: &Float,
u: u64,
prec: u64,
rm: RoundingMode,
) -> (Float, Ordering) {
let positive = *x > 0u32;
let exp_x = i64::from(x.get_exponent().unwrap());
if exp_x == 1 && x.significand_ref().unwrap().is_power_of_2() {
return scaled_unsigned(u, 3, positive, prec, rm);
}
assert_ne!(rm, Exact, "Inexact acot_with_period");
if exp_x <= -64 && exp_x <= -i64::exact_from(prec) - 3 {
let w = if prec <= 63 { 65 } else { prec + 2 };
let mut t = Float::from_unsigned_prec_round(u, w, Exact).0;
t.decrement();
t >>= 2u32;
if !positive {
t.neg_assign();
}
return Float::from_float_prec_round(t, prec, rm);
}
arc_with_period_scale(
|w| x.acot_prec_round_ref(w, Up).0 << SCALE,
u,
positive,
prec,
rm,
)
}
pub(crate) fn acot_with_period_rational_helper(
x: &Rational,
u: u64,
prec: u64,
rm: RoundingMode,
) -> (Float, Ordering) {
let positive = *x > 0u32;
let exp_x = x.floor_log_base_2_abs() + 1;
if x.denominator_ref() == &1u32 && x.numerator_ref() == &1u32 {
return scaled_unsigned(u, 3, positive, prec, rm);
}
assert_ne!(rm, Exact, "Inexact acot_with_period_rational");
if exp_x <= -64 && exp_x <= -i64::exact_from(prec) - 3 {
let w = if prec <= 63 { 65 } else { prec + 2 };
let mut t = Float::from_unsigned_prec_round(u, w, Exact).0;
t.decrement();
t >>= 2u32;
if !positive {
t.neg_assign();
}
return Float::from_float_prec_round(t, prec, rm);
}
if 1 - exp_x <= SCALED_INPUT_EXPONENT {
let scaled = Rational::power_of_2(SCALE_I64) / x;
return arc_with_period_scale(
|w| Float::from_rational_prec_round_ref(&scaled, w, Up).0,
u,
positive,
prec,
rm,
);
}
arc_with_period_scale(
|w| acot_rational_helper(x, w, Up).0 << SCALE,
u,
positive,
prec,
rm,
)
}
impl Float {
#[inline]
pub fn acot_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
self.acot_prec_round_ref(prec, rm)
}
pub fn acot_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
assert_ne!(prec, 0);
match &self.0 {
NaN => (Self::NAN, Equal),
Infinity { sign } => (
if *sign {
Self::ZERO
} else {
Self::NEGATIVE_ZERO
},
Equal,
),
Zero { sign } => {
assert_ne!(rm, Exact, "Inexact acot");
signed_half_pi(!*sign, prec, rm)
}
Finite { .. } => acot_prec_round_normal_ref(self, prec, rm),
}
}
#[inline]
pub fn acot_prec(self, prec: u64) -> (Self, Ordering) {
self.acot_prec_round(prec, Nearest)
}
#[inline]
pub fn acot_prec_ref(&self, prec: u64) -> (Self, Ordering) {
self.acot_prec_round_ref(prec, Nearest)
}
#[inline]
pub fn acot_round(self, rm: RoundingMode) -> (Self, Ordering) {
let prec = self.significant_bits();
self.acot_prec_round(prec, rm)
}
#[inline]
pub fn acot_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
self.acot_prec_round_ref(self.significant_bits(), rm)
}
#[inline]
pub fn acot_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
let (s, o) = self.acot_prec_round_ref(prec, rm);
*self = s;
o
}
#[inline]
pub fn acot_prec_assign(&mut self, prec: u64) -> Ordering {
self.acot_prec_round_assign(prec, Nearest)
}
#[inline]
pub fn acot_round_assign(&mut self, rm: RoundingMode) -> Ordering {
self.acot_prec_round_assign(self.significant_bits(), rm)
}
#[inline]
#[allow(clippy::needless_pass_by_value)]
pub fn acot_rational_prec_round(x: Rational, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
Self::acot_rational_prec_round_ref(&x, prec, rm)
}
pub fn acot_rational_prec_round_ref(
x: &Rational,
prec: u64,
rm: RoundingMode,
) -> (Self, Ordering) {
assert_ne!(prec, 0);
if *x == 0u32 {
assert_ne!(rm, Exact, "Inexact acot_rational");
return signed_half_pi(false, prec, rm);
}
acot_rational_helper(x, prec, rm)
}
#[inline]
pub fn acot_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
Self::acot_rational_prec_round(x, prec, Nearest)
}
#[inline]
pub fn acot_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
Self::acot_rational_prec_round_ref(x, prec, Nearest)
}
#[inline]
pub fn acot_with_period_prec_round(
self,
u: u64,
prec: u64,
rm: RoundingMode,
) -> (Self, Ordering) {
self.acot_with_period_prec_round_ref(u, prec, rm)
}
pub fn acot_with_period_prec_round_ref(
&self,
u: u64,
prec: u64,
rm: RoundingMode,
) -> (Self, Ordering) {
assert_ne!(prec, 0);
match &self.0 {
NaN => (Self::NAN, Equal),
Infinity { sign } => (
if *sign {
Self::ZERO
} else {
Self::NEGATIVE_ZERO
},
Equal,
),
Zero { sign } => {
if u == 0 {
(
if *sign {
Self::ZERO
} else {
Self::NEGATIVE_ZERO
},
Equal,
)
} else {
scaled_unsigned(u, 2, *sign, prec, rm)
}
}
Finite { sign, .. } => {
if u == 0 {
(
if *sign {
Self::ZERO
} else {
Self::NEGATIVE_ZERO
},
Equal,
)
} else {
acot_with_period_prec_round_normal_ref(self, u, prec, rm)
}
}
}
}
#[inline]
pub fn acot_with_period_prec(self, u: u64, prec: u64) -> (Self, Ordering) {
self.acot_with_period_prec_round(u, prec, Nearest)
}
#[inline]
pub fn acot_with_period_prec_ref(&self, u: u64, prec: u64) -> (Self, Ordering) {
self.acot_with_period_prec_round_ref(u, prec, Nearest)
}
#[inline]
pub fn acot_with_period_round(self, u: u64, rm: RoundingMode) -> (Self, Ordering) {
let prec = self.significant_bits();
self.acot_with_period_prec_round(u, prec, rm)
}
#[inline]
pub fn acot_with_period_round_ref(&self, u: u64, rm: RoundingMode) -> (Self, Ordering) {
self.acot_with_period_prec_round_ref(u, self.significant_bits(), rm)
}
#[inline]
pub fn acot_with_period(self, u: u64) -> Self {
let prec = self.significant_bits();
self.acot_with_period_prec(u, prec).0
}
#[inline]
pub fn acot_with_period_ref(&self, u: u64) -> Self {
self.acot_with_period_prec_ref(u, self.significant_bits()).0
}
#[inline]
pub fn acot_with_period_prec_round_assign(
&mut self,
u: u64,
prec: u64,
rm: RoundingMode,
) -> Ordering {
let (s, o) = self.acot_with_period_prec_round_ref(u, prec, rm);
*self = s;
o
}
#[inline]
pub fn acot_with_period_prec_assign(&mut self, u: u64, prec: u64) -> Ordering {
self.acot_with_period_prec_round_assign(u, prec, Nearest)
}
#[inline]
pub fn acot_with_period_round_assign(&mut self, u: u64, rm: RoundingMode) -> Ordering {
self.acot_with_period_prec_round_assign(u, self.significant_bits(), rm)
}
#[inline]
pub fn acot_with_period_assign(&mut self, u: u64) {
let prec = self.significant_bits();
self.acot_with_period_prec_assign(u, prec);
}
#[inline]
#[allow(clippy::needless_pass_by_value)]
pub fn acot_with_period_rational_prec_round(
x: Rational,
u: u64,
prec: u64,
rm: RoundingMode,
) -> (Self, Ordering) {
Self::acot_with_period_rational_prec_round_ref(&x, u, prec, rm)
}
pub fn acot_with_period_rational_prec_round_ref(
x: &Rational,
u: u64,
prec: u64,
rm: RoundingMode,
) -> (Self, Ordering) {
assert_ne!(prec, 0);
if u == 0 {
return (
if *x < 0u32 {
Self::NEGATIVE_ZERO
} else {
Self::ZERO
},
Equal,
);
}
if *x == 0u32 {
return scaled_unsigned(u, 2, true, prec, rm);
}
acot_with_period_rational_helper(x, u, prec, rm)
}
#[inline]
pub fn acot_with_period_rational_prec(x: Rational, u: u64, prec: u64) -> (Self, Ordering) {
Self::acot_with_period_rational_prec_round(x, u, prec, Nearest)
}
#[inline]
pub fn acot_with_period_rational_prec_ref(x: &Rational, u: u64, prec: u64) -> (Self, Ordering) {
Self::acot_with_period_rational_prec_round_ref(x, u, prec, Nearest)
}
#[inline]
pub fn acot_pi_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
self.acot_with_period_prec_round(2, prec, rm)
}
#[inline]
pub fn acot_pi_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
self.acot_with_period_prec_round_ref(2, prec, rm)
}
#[inline]
pub fn acot_pi_prec(self, prec: u64) -> (Self, Ordering) {
self.acot_with_period_prec(2, prec)
}
#[inline]
pub fn acot_pi_prec_ref(&self, prec: u64) -> (Self, Ordering) {
self.acot_with_period_prec_ref(2, prec)
}
#[inline]
pub fn acot_pi_round(self, rm: RoundingMode) -> (Self, Ordering) {
self.acot_with_period_round(2, rm)
}
#[inline]
pub fn acot_pi_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
self.acot_with_period_round_ref(2, rm)
}
#[inline]
pub fn acot_pi(self) -> Self {
self.acot_with_period(2)
}
#[inline]
pub fn acot_pi_ref(&self) -> Self {
self.acot_with_period_ref(2)
}
#[inline]
pub fn acot_pi_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
self.acot_with_period_prec_round_assign(2, prec, rm)
}
#[inline]
pub fn acot_pi_prec_assign(&mut self, prec: u64) -> Ordering {
self.acot_with_period_prec_assign(2, prec)
}
#[inline]
pub fn acot_pi_round_assign(&mut self, rm: RoundingMode) -> Ordering {
self.acot_with_period_round_assign(2, rm)
}
#[inline]
pub fn acot_pi_assign(&mut self) {
self.acot_with_period_assign(2);
}
#[inline]
pub fn acot_pi_rational_prec_round(
x: Rational,
prec: u64,
rm: RoundingMode,
) -> (Self, Ordering) {
Self::acot_with_period_rational_prec_round(x, 2, prec, rm)
}
#[inline]
pub fn acot_pi_rational_prec_round_ref(
x: &Rational,
prec: u64,
rm: RoundingMode,
) -> (Self, Ordering) {
Self::acot_with_period_rational_prec_round_ref(x, 2, prec, rm)
}
#[inline]
pub fn acot_pi_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
Self::acot_with_period_rational_prec(x, 2, prec)
}
#[inline]
pub fn acot_pi_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
Self::acot_with_period_rational_prec_ref(x, 2, prec)
}
}
impl Acot for Float {
type Output = Self;
#[inline]
fn acot(self) -> Self {
let prec = self.significant_bits();
self.acot_prec(prec).0
}
}
impl Acot for &Float {
type Output = Float;
#[inline]
fn acot(self) -> Float {
self.acot_prec_ref(self.significant_bits()).0
}
}
impl AcotAssign for Float {
#[inline]
fn acot_assign(&mut self) {
let prec = self.significant_bits();
self.acot_prec_assign(prec);
}
}
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_acot<T: PrimitiveFloat>(x: T) -> T
where
Float: From<T> + PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
emulate_float_to_float_fn(Float::acot_prec, x)
}
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_acot_rational<T: PrimitiveFloat>(x: &Rational) -> T
where
Float: PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float>,
{
emulate_rational_to_float_fn(Float::acot_rational_prec_ref, x)
}
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_acot_with_period<T: PrimitiveFloat>(x: T, u: u64) -> T
where
Float: From<T> + PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
emulate_float_to_float_fn(|x, prec| Float::acot_with_period_prec(x, u, prec), x)
}
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_acot_with_period_rational<T: PrimitiveFloat>(x: &Rational, u: u64) -> T
where
Float: PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float>,
{
emulate_rational_to_float_fn(
|x, prec| Float::acot_with_period_rational_prec_ref(x, u, prec),
x,
)
}
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_acot_pi<T: PrimitiveFloat>(x: T) -> T
where
Float: From<T> + PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
primitive_float_acot_with_period(x, 2)
}
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_acot_pi_rational<T: PrimitiveFloat>(x: &Rational) -> T
where
Float: PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float>,
{
primitive_float_acot_with_period_rational(x, 2)
}