From d9892ac855bb778b0dcfc9cccaa61aecb13b8a15 Mon Sep 17 00:00:00 2001 From: Jean Dubois Date: Wed, 4 Sep 2024 10:32:27 +0100 Subject: [PATCH] fix crash --- CHANGELOG.md | 5 +++++ examples/calculator.noug | 8 ++++---- lib_/math_.py | 9 ++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bda469c..20be13d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,11 +9,16 @@ Since 0.19.0-beta, we use [this changelog format](https://keepachangelog.com). I ## upcoming ### Fixed +* Fixed a crash in `math.log` on base=1 #### Build scripts * Build scripts now copy `CHANGELOG.md`. * `build.bat` is now more verbose. +### Calculator +#### Fixed +* Fixed a crash with `log` on base=1 + ## 1.1.0 (2024-08-13) ### Added diff --git a/examples/calculator.noug b/examples/calculator.noug index 8ce7f9a..32cc8b0 100644 --- a/examples/calculator.noug +++ b/examples/calculator.noug @@ -111,7 +111,7 @@ while:main_loop True then else var calcul = input() end - + if calcul in ["exit", "exit()", "done", "quit", "q"] then break if calcul == "" then continue if calcul in ["clear", "cls"] then; clear(); continue; end @@ -234,6 +234,8 @@ while:main_loop True then replace(tokens, -1, "st") elif char == "o" and len(tokens) > 0 and tokens(-1) == "st" then replace(tokens, -1, "sto") + elif char == "g" and len(tokens) > 0 and tokens(-1) == "lo" then + replace(tokens, -1, "log") elif char == "g" then append(tokens, char) elif char == "t" and len(tokens) > 0 and tokens(-1) == "gs" then @@ -256,8 +258,6 @@ while:main_loop True then append(tokens, char) elif char == "o" and len(tokens) > 0 and tokens(-1) == "l" then replace(tokens, -1, "lo") - elif char == "g" and len(tokens) > 0 and tokens(-1) == "lo" then - replace(tokens, -1, "log") elif char == "n" and len(tokens) > 0 and tokens(-1) == "l" then replace(tokens, -1, "ln") elif char == "i" then @@ -324,7 +324,7 @@ while:main_loop True then elif token == "log" and len(stack) > 1 and is_num(stack(-1)) and is_num(stack(-2)) then var number = stack(-2) var base = stack(-1) - if number <= 0 or base <= 0 then + if number <= 0 or base <= 1 then var error_msg = "Error: math domain error with logarithm" print_error(error_msg, verbose) var stack = list(old_stack) diff --git a/lib_/math_.py b/lib_/math_.py index 807de52..0f33987 100644 --- a/lib_/math_.py +++ b/lib_/math_.py @@ -41,7 +41,7 @@ def copy(self): """Return a copy of self""" copy = Math(self.name) return self.set_context_and_pos_to_a_copy(copy) - + def is_eq(self, other: Value): return isinstance(other, Math) and self.name == other.name @@ -479,6 +479,13 @@ def execute_math_log(self, exec_context: Context): exec_context, origin_file="lib_.math_.Math.execute_math_log" )) + except ZeroDivisionError as e: + return RTResult().failure(RTArithmeticError( + self.pos_start, self.pos_end, + "Python ZeroDivisionError: {e}", + exec_context, + origin_file="lib_.math_.Math.execute_math_log" + )) return RTResult().success(value_to_return)