use crate::dd::{DdAcc, DD};
use crate::od::{OdAcc, OD};
use crate::qd::{QdAcc, QD};
use crate::recip::{ratio_parts, recip_parts};
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Neg, Sub};
pub mod mutate {
pub const FROM_F64: u32 = 1;
pub const PLAIN_CAUCHY: u32 = 2;
pub const DD_ACC_NO_ERRORS: u32 = 3;
pub const N: u32 = 3;
pub const NAMES: [&str; 4] = [
"none",
"real: from_f64 one unit up",
"real: cauchy by the plain sum of products",
"real: DdAcc without the products' error terms",
];
#[cfg(feature = "mutate")]
thread_local!(static ID: std::cell::Cell<u32> = const { std::cell::Cell::new(0) });
#[cfg(feature = "mutate")]
pub fn set(id: u32) {
ID.with(|m| m.set(id));
}
#[cfg(feature = "mutate")]
#[inline(always)]
pub fn on(id: u32) -> bool {
ID.with(|m| m.get() == id)
}
#[cfg(not(feature = "mutate"))]
#[inline(always)]
pub fn on(_id: u32) -> bool {
false
}
}
pub trait Real:
Copy
+ Debug
+ Send
+ Sync
+ 'static
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ Neg<Output = Self>
{
const NAME: &'static str;
const PARTS: usize;
const ZERO: Self;
const ONE: Self;
const ROUNDOFF: f64;
const UNIT: f64;
const EPSILON: f64;
fn from_f64(x: f64) -> Self;
fn to_f64(self) -> f64;
fn from_dd(x: DD) -> Self;
fn to_dd(self) -> DD;
fn mul_f(self, b: f64) -> Self;
fn is_finite(self) -> bool {
self.to_f64().is_finite()
}
fn recip(self) -> Self;
fn sqrt(self) -> Self;
fn exp(self) -> Self;
fn ln(self) -> Self;
fn recip_int(k: u64) -> Self;
fn from_ratio(p: i64, q: u64) -> Self;
type Acc: Copy + Debug + Send + Sync;
fn acc_zero() -> Self::Acc;
fn acc_add_prod(acc: Self::Acc, a: Self, b: Self) -> Self::Acc;
fn acc_finish(acc: Self::Acc) -> Self;
fn cauchy(a: &[Self], b: &[Self], k: usize) -> Self {
if mutate::on(mutate::PLAIN_CAUCHY) {
let mut s = a[0] * b[k];
for j in 1..=k {
s = s + a[j] * b[k - j];
}
return s;
}
let mut acc = Self::acc_zero();
for j in 0..=k {
acc = Self::acc_add_prod(acc, a[j], b[k - j]);
}
Self::acc_finish(acc)
}
fn parse(s: &str) -> Result<Self, String>;
fn to_sci(self, digits: usize) -> String;
}
impl Real for f64 {
const NAME: &'static str = "f64";
const PARTS: usize = 1;
const ZERO: f64 = 0.0;
const ONE: f64 = 1.0;
const ROUNDOFF: f64 = 1.1102230246251565e-16;
const UNIT: f64 = 2.220446049250313e-16;
const EPSILON: f64 = f64::EPSILON;
#[inline(always)]
fn from_f64(x: f64) -> f64 {
if mutate::on(mutate::FROM_F64) {
x * (1.0 + f64::EPSILON)
} else {
x
}
}
#[inline(always)]
fn to_f64(self) -> f64 {
self
}
#[inline(always)]
fn from_dd(x: DD) -> f64 {
x.to_f64()
}
#[inline(always)]
fn to_dd(self) -> DD {
DD::from_f64(self)
}
#[inline(always)]
fn mul_f(self, b: f64) -> f64 {
self * b
}
#[inline(always)]
fn recip(self) -> f64 {
1.0 / self
}
#[inline(always)]
fn sqrt(self) -> f64 {
f64::sqrt(self)
}
fn exp(self) -> f64 {
f64::exp(self)
}
fn ln(self) -> f64 {
f64::ln(self)
}
fn recip_int(k: u64) -> f64 {
recip_parts(k)[0]
}
fn from_ratio(p: i64, q: u64) -> f64 {
ratio_parts(p, q)[0]
}
type Acc = f64;
#[inline(always)]
fn acc_zero() -> f64 {
-0.0
}
#[inline(always)]
fn acc_add_prod(acc: f64, a: f64, b: f64) -> f64 {
acc + a * b
}
#[inline(always)]
fn acc_finish(acc: f64) -> f64 {
acc
}
fn parse(s: &str) -> Result<f64, String> {
s.trim().parse::<f64>().map_err(|e| format!("{e}"))
}
fn to_sci(self, digits: usize) -> String {
QD::from_f64(self).to_sci(digits.min(17))
}
}
macro_rules! real_multidouble {
($t:ty, $name:expr, $parts:expr, $roundoff:expr, $unit:expr, $eps:expr, $acc:ty, $from_dd:expr, $to_dd:expr,
$parse:expr, $sci:expr) => {
impl Real for $t {
const NAME: &'static str = $name;
const PARTS: usize = $parts;
const ZERO: $t = <$t>::ZERO;
const ONE: $t = <$t>::ONE;
const ROUNDOFF: f64 = $roundoff;
const UNIT: f64 = $unit;
const EPSILON: f64 = $eps;
#[inline(always)]
fn from_f64(x: f64) -> $t {
let r = <$t>::from_f64(x);
if mutate::on(mutate::FROM_F64) {
r.mul_f(1.0 + f64::EPSILON)
} else {
r
}
}
#[inline(always)]
fn to_f64(self) -> f64 {
<$t>::to_f64(self)
}
#[inline(always)]
fn from_dd(x: DD) -> $t {
($from_dd)(x)
}
#[inline(always)]
fn to_dd(self) -> DD {
($to_dd)(self)
}
#[inline(always)]
fn mul_f(self, b: f64) -> $t {
<$t>::mul_f(self, b)
}
#[inline(always)]
fn recip(self) -> $t {
<$t>::recip(self)
}
#[inline(always)]
fn sqrt(self) -> $t {
<$t>::sqrt(self)
}
fn exp(self) -> $t {
<$t>::exp(self)
}
fn ln(self) -> $t {
<$t>::ln(self)
}
fn recip_int(k: u64) -> $t {
<$t>::recip_int(k)
}
fn from_ratio(p: i64, q: u64) -> $t {
<$t>::from_ratio(p, q)
}
type Acc = $acc;
#[inline(always)]
fn acc_zero() -> $acc {
<$acc>::zero()
}
#[inline(always)]
fn acc_add_prod(acc: $acc, a: $t, b: $t) -> $acc {
acc.add_prod(a, b)
}
#[inline(always)]
fn acc_finish(acc: $acc) -> $t {
acc.finish()
}
fn parse(s: &str) -> Result<$t, String> {
($parse)(s)
}
fn to_sci(self, digits: usize) -> String {
($sci)(self, digits)
}
}
};
}
fn dd_of_qd(q: QD) -> DD {
if q.x[0].is_finite() {
q.to_dd()
} else {
DD { hi: q.x[0], lo: 0.0 }
}
}
real_multidouble!(
DD,
"dd",
2,
1.232595164407831e-32,
4.930380657631324e-32,
DD::EPS,
DdAcc,
|x: DD| x,
|x: DD| x,
|s: &str| QD::parse(s).map(dd_of_qd),
|x: DD, digits: usize| QD::from_dd(x).to_sci(digits.min(33))
);
real_multidouble!(
QD,
"qd",
4,
1.504632769052528e-64,
2.4308653429145085e-63,
QD::EPS,
QdAcc,
QD::from_dd,
QD::to_dd,
QD::parse,
|x: QD, digits: usize| x.to_sci(digits.min(64))
);
real_multidouble!(
OD,
"od",
8,
2.3e-128,
5.909106315382871e-126,
OD::EPS,
OdAcc,
OD::from_dd,
OD::to_dd,
OD::parse,
|x: OD, digits: usize| x.to_sci(digits.min(128))
);