1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
use num::{Bounded, Signed};
use std::{f32, f64};
use approx::{RelativeEq, UlpsEq};
use crate::general::{ComplexField, Lattice};
#[cfg(not(feature = "std"))]
use num::Float;
//#[cfg(feature = "decimal")]
//use decimal::d128;
#[allow(missing_docs)]
/// Trait shared by all reals.
///
/// Reals are equipped with functions that are commonly used on reals. The results of those
/// functions only have to be approximately equal to the actual theoretical values.
// FIXME: SubsetOf should be removed when specialization will be supported by rustc. This will
// allow a blanket impl: impl<T: Clone> SubsetOf<T> for T { ... }
// NOTE: make all types debuggable/'static/Any ? This seems essential for any kind of generic programming.
pub trait RealField:
ComplexField<RealField = Self>
+ RelativeEq<Epsilon = Self>
+ UlpsEq<Epsilon = Self>
+ Lattice
+ Signed
+ Bounded
{
// NOTE: a real must be bounded because, no matter the chosen representation, being `Copy` implies that it occupies a statically-known size, meaning that it must have min/max values.
fn is_sign_positive(self) -> bool;
fn is_sign_negative(self) -> bool;
fn max(self, other: Self) -> Self;
fn min(self, other: Self) -> Self;
fn atan2(self, other: Self) -> Self;
fn pi() -> Self;
fn two_pi() -> Self;
fn frac_pi_2() -> Self;
fn frac_pi_3() -> Self;
fn frac_pi_4() -> Self;
fn frac_pi_6() -> Self;
fn frac_pi_8() -> Self;
fn frac_1_pi() -> Self;
fn frac_2_pi() -> Self;
fn frac_2_sqrt_pi() -> Self;
fn e() -> Self;
fn log2_e() -> Self;
fn log10_e() -> Self;
fn ln_2() -> Self;
fn ln_10() -> Self;
}
macro_rules! impl_real(
($($T:ty, $M:ident, $libm: ident);*) => ($(
impl RealField for $T {
#[inline]
fn is_sign_positive(self) -> bool {
$M::is_sign_positive(self)
}
#[inline]
fn is_sign_negative(self) -> bool {
$M::is_sign_negative(self)
}
#[inline]
fn max(self, other: Self) -> Self {
$M::max(self, other)
}
#[inline]
fn min(self, other: Self) -> Self {
$M::min(self, other)
}
#[inline]
fn atan2(self, other: Self) -> Self {
$libm::atan2(self, other)
}
/// Archimedes' constant.
#[inline]
fn pi() -> Self {
$M::consts::PI
}
/// 2.0 * pi.
#[inline]
fn two_pi() -> Self {
$M::consts::PI + $M::consts::PI
}
/// pi / 2.0.
#[inline]
fn frac_pi_2() -> Self {
$M::consts::FRAC_PI_2
}
/// pi / 3.0.
#[inline]
fn frac_pi_3() -> Self {
$M::consts::FRAC_PI_3
}
/// pi / 4.0.
#[inline]
fn frac_pi_4() -> Self {
$M::consts::FRAC_PI_4
}
/// pi / 6.0.
#[inline]
fn frac_pi_6() -> Self {
$M::consts::FRAC_PI_6
}
/// pi / 8.0.
#[inline]
fn frac_pi_8() -> Self {
$M::consts::FRAC_PI_8
}
/// 1.0 / pi.
#[inline]
fn frac_1_pi() -> Self {
$M::consts::FRAC_1_PI
}
/// 2.0 / pi.
#[inline]
fn frac_2_pi() -> Self {
$M::consts::FRAC_2_PI
}
/// 2.0 / sqrt(pi).
#[inline]
fn frac_2_sqrt_pi() -> Self {
$M::consts::FRAC_2_SQRT_PI
}
/// Euler's number.
#[inline]
fn e() -> Self {
$M::consts::E
}
/// log2(e).
#[inline]
fn log2_e() -> Self {
$M::consts::LOG2_E
}
/// log10(e).
#[inline]
fn log10_e() -> Self {
$M::consts::LOG10_E
}
/// ln(2.0).
#[inline]
fn ln_2() -> Self {
$M::consts::LN_2
}
/// ln(10.0).
#[inline]
fn ln_10() -> Self {
$M::consts::LN_10
}
}
)*)
);
#[cfg(not(feature = "std"))]
impl_real!(f32,f32,Float; f64,f64,Float);
#[cfg(feature = "std")]
impl_real!(f32,f32,f32; f64,f64,f64);
//#[cfg(feature = "decimal")]
//impl_real!(d128, d128, d128);