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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
use dashu_int::{IBig, UBig};
use crate::{
error::{assert_finite, assert_finite_operands},
fbig::FBig,
helper_macros,
repr::{Context, Repr, Word},
round::{Round, Rounded},
};
use core::ops::{Mul, MulAssign};
impl<'l, 'r, R: Round, const B: Word> Mul<&'r FBig<R, B>> for &'l FBig<R, B> {
type Output = FBig<R, B>;
#[inline]
fn mul(self, rhs: &FBig<R, B>) -> Self::Output {
assert_finite_operands(&self.repr, &rhs.repr);
let context = Context::max(self.context, rhs.context);
let repr = Repr::new(
&self.repr.significand * &rhs.repr.significand,
self.repr.exponent + rhs.repr.exponent,
);
FBig::new(context.repr_round(repr).value(), context)
}
}
impl<'r, R: Round, const B: Word> Mul<&'r FBig<R, B>> for FBig<R, B> {
type Output = FBig<R, B>;
#[inline]
fn mul(self, rhs: &FBig<R, B>) -> Self::Output {
assert_finite_operands(&self.repr, &rhs.repr);
let context = Context::max(self.context, rhs.context);
let repr = Repr::new(
self.repr.significand * &rhs.repr.significand,
self.repr.exponent + rhs.repr.exponent,
);
FBig::new(context.repr_round(repr).value(), context)
}
}
impl<'l, R: Round, const B: Word> Mul<FBig<R, B>> for &'l FBig<R, B> {
type Output = FBig<R, B>;
#[inline]
fn mul(self, rhs: FBig<R, B>) -> Self::Output {
assert_finite_operands(&self.repr, &rhs.repr);
let context = Context::max(self.context, rhs.context);
let repr = Repr::new(
&self.repr.significand * rhs.repr.significand,
self.repr.exponent + rhs.repr.exponent,
);
FBig::new(context.repr_round(repr).value(), context)
}
}
impl<R: Round, const B: Word> Mul<FBig<R, B>> for FBig<R, B> {
type Output = FBig<R, B>;
#[inline]
fn mul(self, rhs: FBig<R, B>) -> Self::Output {
assert_finite_operands(&self.repr, &rhs.repr);
let context = Context::max(self.context, rhs.context);
let repr = Repr::new(
self.repr.significand * rhs.repr.significand,
self.repr.exponent + rhs.repr.exponent,
);
FBig::new(context.repr_round(repr).value(), context)
}
}
helper_macros::impl_binop_assign_by_taking!(impl MulAssign<Self>, mul_assign, mul);
macro_rules! impl_mul_primitive_with_fbig {
($($t:ty)*) => {$(
helper_macros::impl_binop_with_primitive!(impl Mul<$t>, mul);
helper_macros::impl_binop_assign_with_primitive!(impl MulAssign<$t>, mul_assign);
)*};
}
impl_mul_primitive_with_fbig!(u8 u16 u32 u64 u128 usize UBig i8 i16 i32 i64 i128 isize IBig);
impl<R: Round, const B: Word> FBig<R, B> {
/// Compute the square of this number (`self * self`)
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("-1.234")?;
/// assert_eq!(a.sqr(), DBig::from_str("1.523")?);
/// # Ok::<(), ParseError>(())
/// ```
#[inline]
pub fn sqr(&self) -> Self {
self.context.sqr(&self.repr).value()
}
/// Compute the cubic of this number (`self * self * self`)
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("-1.234")?;
/// assert_eq!(a.cubic(), DBig::from_str("-1.879")?);
/// # Ok::<(), ParseError>(())
/// ```
#[inline]
pub fn cubic(&self) -> Self {
self.context.cubic(&self.repr).value()
}
}
impl<R: Round> Context<R> {
/// Multiply two floating point numbers under this context.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// let a = DBig::from_str("-1.234")?;
/// let b = DBig::from_str("6.789")?;
/// assert_eq!(
/// context.mul(&a.repr(), &b.repr()),
/// Inexact(DBig::from_str("-8.4")?, SubOne)
/// );
/// # Ok::<(), ParseError>(())
/// ```
pub fn mul<const B: Word>(&self, lhs: &Repr<B>, rhs: &Repr<B>) -> Rounded<FBig<R, B>> {
assert_finite_operands(lhs, rhs);
// at most double the precision is required to get a correct result
// shrink the input operands if necessary
let max_precision = if self.is_limited() {
self.precision * 2
} else {
usize::MAX
};
let lhs_shrink;
let lhs_repr = if lhs.digits() > max_precision {
lhs_shrink = Context::<R>::new(max_precision).repr_round_ref(lhs).value();
&lhs_shrink
} else {
lhs
};
let rhs_shrink;
let rhs_repr = if rhs.digits() > max_precision {
rhs_shrink = Context::<R>::new(max_precision).repr_round_ref(rhs).value();
&rhs_shrink
} else {
rhs
};
let repr = Repr::new(
&lhs_repr.significand * &rhs_repr.significand,
lhs_repr.exponent + rhs_repr.exponent,
);
self.repr_round(repr).map(|v| FBig::new(v, *self))
}
/// Calculate the square of the floating point number under this context.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// let a = DBig::from_str("-1.234")?;
/// assert_eq!(context.sqr(&a.repr()), Inexact(DBig::from_str("1.5")?, NoOp));
/// # Ok::<(), ParseError>(())
/// ```
pub fn sqr<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>> {
assert_finite(f);
// shrink the input operands if necessary
let max_precision = if self.is_limited() {
self.precision * 2
} else {
usize::MAX
};
let f_shrink;
let f_repr = if f.digits() > max_precision {
f_shrink = Context::<R>::new(max_precision).repr_round_ref(f).value();
&f_shrink
} else {
f
};
let repr = Repr::new(f_repr.significand.sqr().into(), 2 * f_repr.exponent);
self.repr_round(repr).map(|v| FBig::new(v, *self))
}
/// Calculate the cubic of the floating point number under this context.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::{Context, round::{mode::HalfAway, Rounding::*}};
///
/// let context = Context::<HalfAway>::new(2);
/// let a = DBig::from_str("-1.234")?;
/// assert_eq!(context.cubic(&a.repr()), Inexact(DBig::from_str("-1.9")?, SubOne));
/// # Ok::<(), ParseError>(())
/// ```
pub fn cubic<const B: Word>(&self, f: &Repr<B>) -> Rounded<FBig<R, B>> {
assert_finite(f);
// shrink the input operands if necessary
let max_precision = if self.is_limited() {
self.precision * 3
} else {
usize::MAX
};
let f_shrink;
let f_repr = if f.digits() > max_precision {
f_shrink = Context::<R>::new(max_precision).repr_round_ref(f).value();
&f_shrink
} else {
f
};
let repr = Repr::new(f_repr.significand.cubic(), 3 * f_repr.exponent);
self.repr_round(repr).map(|v| FBig::new(v, *self))
}
}