From e1ecb8cabe2661c639fbdfe1ebcdbb0263dac8e7 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 1 Apr 2023 23:26:58 +0200 Subject: Simplify `pow` function --- library/src/compute/calc.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'library/src') 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, ) -> 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())), } } -- cgit v1.2.3