<commit_before>package spire.math

import spire.syntax.nroot._

sealed trait CReal { x =>

  import CReal.{roundUp, Exact, Inexact}

  def apply(p: Int): SafeLong

  def toRational(p: Int): Rational = this match {
    case Exact(n) => n
    case _ => Rational(x(p), SafeLong.two.pow(p))
  }

  def toRational: Rational = toRational(CReal.bits)

  override def equals(y: Any): Boolean = y match {
    case y: CReal => x eqv y
    case y => toRational.equals(y)
  }

  def eqv(y: CReal): Boolean = (x, y) match {
    case (Exact(nx), Exact(ny)) => nx == ny
    case _ => (x - y)(CReal.bits).signum == 0
  }

  def compare(y: CReal): Int = (x, y) match {
    case (Exact(nx), Exact(ny)) => nx compare ny
    case _ => (x - y)(CReal.bits).signum
  }

  def min(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx min ny)
    case _ => CReal(p => x(p) min y(p))
  }

  def max(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx max ny)
    case _ => CReal(p => x(p) max y(p))
  }

  def abs(): CReal = this match {
    case Exact(n) => Exact(n.abs)
    case _ => CReal(p => x(p).abs)
  }

  def signum(): Int = this match {
    case Exact(n) => n.signum
    case _ => x(CReal.bits).signum
  }

  def unary_-(): CReal = this match {
    case Exact(n) => Exact(-n)
    case _ => CReal(p => -x(p))
  }

  def reciprocal(): CReal = {
    def findNonzero(i: Int): Int =
      if (SafeLong.three <= x(i).abs) i else findNonzero(i + 1)

    this match {
      case Exact(n) => Exact(n.reciprocal)
      case _ => CReal({p =>
        val s = findNonzero(0)
        roundUp(Rational(SafeLong.two.pow(2 * p + 2 * s + 2), x(p + 2 * s + 2)))
      })
    }
  }

  def +(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx + ny)
    case (Exact(Rational.zero), _) => y
    case (_, Exact(Rational.zero)) => x
    case _ => CReal(p => roundUp(Rational(x(p + 2) + y(p + 2), 4)))
  }

  def -(y: CReal): CReal = x + (-y)

  def *(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx * ny)
    case (Exact(Rational.zero), _) => CReal.zero
    case (_, Exact(Rational.zero)) => CReal.zero
    case (Exact(Rational.one), _) => y
    case (_, Exact(Rational.one)) => x
    case _ => CReal({p =>
      val x0 = x(0).abs + 2
      val y0 = y(0).abs + 2
      val sx = CReal.sizeInBase(x0, 2) + 3
      val sy = CReal.sizeInBase(y0, 2) + 3
      roundUp(Rational(x(p + sy) * y(p + sx), SafeLong.two.pow(p + sx + sy)))
    })
  }

  def pow(k: Int): CReal = {
    def loop(b: CReal, k: Int, extra: CReal): CReal =
      if (k == 1)
        b * extra
      else
        loop(b * b, k >>> 1, if ((k & 1) == 1) b * extra else extra)

    this match {
      case Exact(n) =>
        Exact(n.pow(k))
      case _ =>
        if (k < 0) {
          reciprocal.pow(-k)
        } else if (k == 0) {
          CReal.one
        } else if (k == 1) {
          this
        } else {
          loop(x, k - 1, x)
        }
    }
  }

  def /(y: CReal): CReal = x * y.reciprocal

  def %(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx % ny)
    case _ => CReal({ p => 
      val d = x / y
      val s = d(2)
      val d2 = if (s >= 0) d.floor else d.ceil
        (x - d2 * y)(p)
    })
  }

  def /~(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx /~ ny)
    case _ => CReal({ p =>
      val d = x / y
      val s = d(2)
      val d2 = if (s >= 0) d.floor else d.ceil
      d2(p)
    })
  }

  def gcd(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx gcd ny)
    case _ => CReal({ p =>
      val g = x.toRational(p) gcd y.toRational(p)
      roundUp(g * SafeLong.two.pow(p))
    })
  }

  def ceil(): CReal = x match {
    case Exact(n) => Exact(n.ceil)
    case _ => CReal({ p =>
      val n = x(p)
      val t = SafeLong.two.pow(p)
      val m = n % t
      if (m == 0) n else n + t - m
    })
  }

  def floor(): CReal = x match {
    case Exact(n) => Exact(n.floor)
    case _ => CReal({ p =>
      val n = x(p)
      val t = SafeLong.two.pow(p)
      val m = n % t
      n - m
    })
  }

  def round: CReal = x match {
    case Exact(n) => Exact(n.round)
    case _ => CReal({ p =>
      val n = x(p)
      val t = SafeLong.two.pow(p)
      val h = t / 2
      val m = n % t
      if (m < h) n - m else n - m + t
    })
  }

  def isWhole(): Boolean = x match {
    case Exact(n) =>
      n.isWhole
    case _ =>
      val n = x(CReal.bits)
      val t = SafeLong.two.pow(CReal.bits)
        (n % t) == 0
  }

  def sqrt(): CReal = CReal(p => x(p * 2).sqrt)
  def nroot(k: Int): CReal = CReal(p => x(p * k).nroot(k))<commit_msg>add empty parameter list to round<commit_after>package spire.math

import spire.syntax.nroot._

sealed trait CReal { x =>

  import CReal.{roundUp, Exact, Inexact}

  def apply(p: Int): SafeLong

  def toRational(p: Int): Rational = this match {
    case Exact(n) => n
    case _ => Rational(x(p), SafeLong.two.pow(p))
  }

  def toRational: Rational = toRational(CReal.bits)

  override def equals(y: Any): Boolean = y match {
    case y: CReal => x eqv y
    case y => toRational.equals(y)
  }

  def eqv(y: CReal): Boolean = (x, y) match {
    case (Exact(nx), Exact(ny)) => nx == ny
    case _ => (x - y)(CReal.bits).signum == 0
  }

  def compare(y: CReal): Int = (x, y) match {
    case (Exact(nx), Exact(ny)) => nx compare ny
    case _ => (x - y)(CReal.bits).signum
  }

  def min(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx min ny)
    case _ => CReal(p => x(p) min y(p))
  }

  def max(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx max ny)
    case _ => CReal(p => x(p) max y(p))
  }

  def abs(): CReal = this match {
    case Exact(n) => Exact(n.abs)
    case _ => CReal(p => x(p).abs)
  }

  def signum(): Int = this match {
    case Exact(n) => n.signum
    case _ => x(CReal.bits).signum
  }

  def unary_-(): CReal = this match {
    case Exact(n) => Exact(-n)
    case _ => CReal(p => -x(p))
  }

  def reciprocal(): CReal = {
    def findNonzero(i: Int): Int =
      if (SafeLong.three <= x(i).abs) i else findNonzero(i + 1)

    this match {
      case Exact(n) => Exact(n.reciprocal)
      case _ => CReal({p =>
        val s = findNonzero(0)
        roundUp(Rational(SafeLong.two.pow(2 * p + 2 * s + 2), x(p + 2 * s + 2)))
      })
    }
  }

  def +(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx + ny)
    case (Exact(Rational.zero), _) => y
    case (_, Exact(Rational.zero)) => x
    case _ => CReal(p => roundUp(Rational(x(p + 2) + y(p + 2), 4)))
  }

  def -(y: CReal): CReal = x + (-y)

  def *(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx * ny)
    case (Exact(Rational.zero), _) => CReal.zero
    case (_, Exact(Rational.zero)) => CReal.zero
    case (Exact(Rational.one), _) => y
    case (_, Exact(Rational.one)) => x
    case _ => CReal({p =>
      val x0 = x(0).abs + 2
      val y0 = y(0).abs + 2
      val sx = CReal.sizeInBase(x0, 2) + 3
      val sy = CReal.sizeInBase(y0, 2) + 3
      roundUp(Rational(x(p + sy) * y(p + sx), SafeLong.two.pow(p + sx + sy)))
    })
  }

  def pow(k: Int): CReal = {
    def loop(b: CReal, k: Int, extra: CReal): CReal =
      if (k == 1)
        b * extra
      else
        loop(b * b, k >>> 1, if ((k & 1) == 1) b * extra else extra)

    this match {
      case Exact(n) =>
        Exact(n.pow(k))
      case _ =>
        if (k < 0) {
          reciprocal.pow(-k)
        } else if (k == 0) {
          CReal.one
        } else if (k == 1) {
          this
        } else {
          loop(x, k - 1, x)
        }
    }
  }

  def /(y: CReal): CReal = x * y.reciprocal

  def %(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx % ny)
    case _ => CReal({ p => 
      val d = x / y
      val s = d(2)
      val d2 = if (s >= 0) d.floor else d.ceil
        (x - d2 * y)(p)
    })
  }

  def /~(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx /~ ny)
    case _ => CReal({ p =>
      val d = x / y
      val s = d(2)
      val d2 = if (s >= 0) d.floor else d.ceil
      d2(p)
    })
  }

  def gcd(y: CReal): CReal = (x, y) match {
    case (Exact(nx), Exact(ny)) => Exact(nx gcd ny)
    case _ => CReal({ p =>
      val g = x.toRational(p) gcd y.toRational(p)
      roundUp(g * SafeLong.two.pow(p))
    })
  }

  def ceil(): CReal = x match {
    case Exact(n) => Exact(n.ceil)
    case _ => CReal({ p =>
      val n = x(p)
      val t = SafeLong.two.pow(p)
      val m = n % t
      if (m == 0) n else n + t - m
    })
  }

  def floor(): CReal = x match {
    case Exact(n) => Exact(n.floor)
    case _ => CReal({ p =>
      val n = x(p)
      val t = SafeLong.two.pow(p)
      val m = n % t
      n - m
    })
  }

  def round(): CReal = x match {
    case Exact(n) => Exact(n.round)
    case _ => CReal({ p =>
      val n = x(p)
      val t = SafeLong.two.pow(p)
      val h = t / 2
      val m = n % t
      if (m < h) n - m else n - m + t
    })
  }

  def isWhole(): Boolean = x match {
    case Exact(n) =>
      n.isWhole
    case _ =>
      val n = x(CReal.bits)
      val t = SafeLong.two.pow(CReal.bits)
        (n % t) == 0
  }

  def sqrt(): CReal = CReal(p => x(p * 2).sqrt)
  def nroot(k: Int): CReal = CReal(p => x(p * k).nroot(k))
{'fraction_non_alphanumeric': 0.18423855165069222, 'fraction_numerical': 0.009158679446219382, 'mean_word_length': 1.9568639798488665, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5925186', 'n_tokens_mistral': 4022, 'n_tokens_neox': 3889, 'n_words': 1060}