From 5c10bea7aa863a803901e717dbd5b58ff1d66f98 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sun, 26 Jan 2020 20:41:16 +0100 Subject: [PATCH] Math: Fix log/log10 return type. They used to always return complex numbers, instead of floats on float argument. --- plugins/Math/evaluator.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/plugins/Math/evaluator.py b/plugins/Math/evaluator.py index 9e7208ef1..257131934 100644 --- a/plugins/Math/evaluator.py +++ b/plugins/Math/evaluator.py @@ -61,11 +61,11 @@ BIN_OPS = { MATH_CONSTANTS = 'e inf nan pi tau'.split() SAFE_MATH_FUNCTIONS = ( 'acos acosh asin asinh atan atan2 atanh copysign cos cosh degrees erf ' - 'erfc exp expm1 fabs fmod frexp fsum gamma hypot ldexp lgamma log log10 ' + 'erfc exp expm1 fabs fmod frexp fsum gamma hypot ldexp lgamma ' 'log1p log2 modf pow radians remainder sin sinh tan tanh' ).split() SAFE_CMATH_FUNCTIONS = ( - 'acos acosh asin asinh atan atanh cos cosh exp inf infj log log10 ' + 'acos acosh asin asinh atan atanh cos cosh exp inf infj ' 'nanj phase polar rect sin sinh tan tanh tau' ).split() @@ -78,6 +78,18 @@ def _sqrt(x): else: return math.sqrt(x) +def _log(x): + if isinstance(x, complex) or x < 0: + return cmath.log(x) + else: + return math.log(x) + +def _log10(x): + if isinstance(x, complex) or x < 0: + return cmath.log10(x) + else: + return math.log10(x) + def _cbrt(x): return math.pow(x, 1.0/3) @@ -96,6 +108,8 @@ SAFE_ENV.update({ 'factorial': _factorial, 'sqrt': _sqrt, 'cbrt': _cbrt, + 'log': _log, + 'log10': _log10, 'ceil': lambda x: float(math.ceil(x)), 'floor': lambda x: float(math.floor(x)), })