diff options
| author | Sébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com> | 2023-04-01 14:24:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-01 14:24:43 +0200 |
| commit | 9e69a7b161191992f336c09ec711a1e1c9768633 (patch) | |
| tree | 600e30ddc9a8695f500872c7f3ce36fdc93199af /library/src | |
| parent | 613c644f0479b2aae6be96fa389352883a2321c3 (diff) | |
Allows negative pow (#486)
Diffstat (limited to 'library/src')
| -rw-r--r-- | library/src/compute/calc.rs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs index 7fcc6131..d98d09b2 100644 --- a/library/src/compute/calc.rs +++ b/library/src/compute/calc.rs @@ -94,13 +94,18 @@ pub fn pow( Num::Int(i) if i > u32::MAX as i64 => { bail!(exponent.span, "exponent too large"); } - Num::Int(0..) => exponent.v, - Num::Float(f) if f >= 0.0 => exponent.v, + Num::Int(i) if i != 0 => exponent.v, + Num::Float(f) if f.is_normal() => exponent.v, _ => { - bail!(exponent.span, "exponent must be non-negative"); + bail!(exponent.span, "exponent must be normal (non-zero, non-NaN, non-infinite, non-subnormal)"); } }; - base.apply2(exponent, |a, b| a.pow(b as u32), f64::powf) + + match (base, exponent) { + (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())), + } } /// Calculate the square root of a number. |
