// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

use crate::Float;
use core::cmp::Ordering;
use malachite_base::rounding_modes::RoundingMode::{self, *};

impl Float {
    /// Returns an approximation of the base-10 logarithm of 2, with the given precision and rounded
    /// using the given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the
    /// rounded value is less than or greater than the exact value of the constant. (Since the
    /// constant is irrational, the rounded value is never equal to the exact value.)
    ///
    /// $$
    /// x = \log_{10} 2+\varepsilon.
    /// $$
    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p-1}$.
    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p-2}$.
    ///
    /// The constant is irrational and transcendental.
    ///
    /// The output has precision `prec`.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^2 \log\log n)$
    ///
    /// $M(n) = O(n (\log n)^2)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
    ///
    /// # Panics
    /// Panics if `prec` is zero or if `rm` is `Exact`.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (l, o) = Float::log_10_2_prec_round(100, Floor);
    /// assert_eq!(l.to_string(), "0.3010299956639811952137388947242");
    /// assert_eq!(o, Less);
    ///
    /// let (l, o) = Float::log_10_2_prec_round(100, Ceiling);
    /// assert_eq!(l.to_string(), "0.3010299956639811952137388947246");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn log_10_2_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
        Self::log_base_10_prec_round(const { Self::const_from_unsigned(2) }, prec, rm)
    }

    /// Returns an approximation of the base-10 logarithm of 2, with the given precision and rounded
    /// to the nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating
    /// whether the rounded value is less than or greater than the exact value of the constant.
    /// (Since the constant is irrational, the rounded value is never equal to the exact value.)
    ///
    /// $$
    /// x = \log_{10} 2+\varepsilon.
    /// $$
    /// - $|\varepsilon| < 2^{-p-2}$.
    ///
    /// The constant is irrational and transcendental.
    ///
    /// The output has precision `prec`.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^2 \log\log n)$
    ///
    /// $M(n) = O(n (\log n)^2)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (l, o) = Float::log_10_2_prec(1);
    /// assert_eq!(l.to_string(), "0.2");
    /// assert_eq!(o, Less);
    ///
    /// let (l, o) = Float::log_10_2_prec(10);
    /// assert_eq!(l.to_string(), "0.3013");
    /// assert_eq!(o, Greater);
    ///
    /// let (l, o) = Float::log_10_2_prec(100);
    /// assert_eq!(l.to_string(), "0.3010299956639811952137388947246");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn log_10_2_prec(prec: u64) -> (Self, Ordering) {
        Self::log_10_2_prec_round(prec, Nearest)
    }
}
