use oxinum_core::OxiNumResult;
use super::constants;
use super::float::{BigFloat, RoundingMode};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FloatContext {
precision: u32,
rounding: RoundingMode,
}
impl FloatContext {
pub fn new(precision: u32) -> Self {
assert!(precision > 0, "FloatContext precision must be > 0");
Self {
precision,
rounding: RoundingMode::HalfEven,
}
}
#[must_use]
pub fn with_rounding(self, mode: RoundingMode) -> Self {
Self {
rounding: mode,
..self
}
}
pub fn precision(&self) -> u32 {
self.precision
}
pub fn rounding(&self) -> RoundingMode {
self.rounding
}
pub fn sqrt(&self, x: &BigFloat) -> OxiNumResult<BigFloat> {
x.sqrt(self.precision, self.rounding)
}
pub fn exp(&self, x: &BigFloat) -> OxiNumResult<BigFloat> {
x.exp(self.precision, self.rounding)
}
pub fn ln(&self, x: &BigFloat) -> OxiNumResult<BigFloat> {
x.ln(self.precision, self.rounding)
}
pub fn log(&self, x: &BigFloat, base: &BigFloat) -> OxiNumResult<BigFloat> {
x.log(base, self.precision, self.rounding)
}
pub fn pow(&self, x: &BigFloat, exp: &BigFloat) -> OxiNumResult<BigFloat> {
x.pow(exp, self.precision, self.rounding)
}
pub fn sin(&self, x: &BigFloat) -> OxiNumResult<BigFloat> {
x.sin(self.precision, self.rounding)
}
pub fn cos(&self, x: &BigFloat) -> OxiNumResult<BigFloat> {
x.cos(self.precision, self.rounding)
}
pub fn tan(&self, x: &BigFloat) -> OxiNumResult<BigFloat> {
x.tan(self.precision, self.rounding)
}
pub fn atan(&self, x: &BigFloat) -> OxiNumResult<BigFloat> {
x.atan(self.precision, self.rounding)
}
pub fn atan2(&self, y: &BigFloat, x: &BigFloat) -> OxiNumResult<BigFloat> {
y.atan2(x, self.precision, self.rounding)
}
pub fn pi(&self) -> OxiNumResult<BigFloat> {
constants::pi(self.precision)
}
pub fn e_const(&self) -> OxiNumResult<BigFloat> {
constants::e_const(self.precision)
}
pub fn ln2(&self) -> OxiNumResult<BigFloat> {
constants::ln2(self.precision)
}
}