diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-04-01 23:26:58 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-04-01 23:31:23 +0200 |
| commit | e1ecb8cabe2661c639fbdfe1ebcdbb0263dac8e7 (patch) | |
| tree | 1ee9ce953bce3c098452dae4a424d95c9aa23a0b /library/src/compute | |
| parent | 7ef687ec53b5477ae556f7e67ce9e6309de3341f (diff) | |
Simplify `pow` function
Diffstat (limited to 'library/src/compute')
| -rw-r--r-- | library/src/compute/calc.rs | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs index ba01c18e..ccc6e7b1 100644 --- a/library/src/compute/calc.rs +++ b/library/src/compute/calc.rs @@ -90,28 +90,24 @@ pub fn pow( /// The exponent of the power. Must be non-negative. exponent: Spanned<Num>, ) -> Value { - let exponent_value = match exponent.v { - Num::Int(i) if i > u32::MAX as i64 => { - bail!(exponent.span, "exponent too large"); + let Spanned { v: exp, span } = exponent; + match exp { + _ if exp.float() == 0.0 && base.float() == 0.0 => { + bail!(args.span, "zero to the power of zero is undefined") } - Num::Int(i) => exponent.v, - Num::Float(f) if f.is_normal() || f == 0 as f64 => exponent.v, - _ => { - bail!( - exponent.span, - "exponent must be normal (non-NaN, non-infinite, non-subnormal)" - ); + Num::Int(i) if i32::try_from(i).is_err() => { + bail!(span, "exponent is too large") + } + Num::Float(f) if !f.is_normal() && f != 0.0 => { + bail!(span, "exponent may not be NaN, infinite, or subnormal") } + _ => {} }; - if exponent_value.float() == 0 as f64 && base.float() == 0 as f64 { - return bail!(exponent.span, "zero to the power of zero is undefined"); - } - - match (base, exponent_value) { + match (base, exp) { (Num::Int(a), Num::Int(b)) if b >= 0 => Value::Int(a.pow(b as u32)), (a, Num::Int(b)) => Value::Float(a.float().powi(b as i32)), - (a, b) => Value::Float(f64::powf(a.float(), b.float())), + (a, b) => Value::Float(a.float().powf(b.float())), } } |
