From e8f7dbab0556a985416cec03ca949ab4be92043e Mon Sep 17 00:00:00 2001 From: Tom Herman Date: Mon, 19 Aug 2024 15:49:04 +0300 Subject: [PATCH] fix possible zero divisions --- flipjump/interpretter/debugging/macro_usage_graph.py | 9 +++++---- flipjump/interpretter/fjm_run.py | 8 ++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/flipjump/interpretter/debugging/macro_usage_graph.py b/flipjump/interpretter/debugging/macro_usage_graph.py index 58e898e..6eae30b 100644 --- a/flipjump/interpretter/debugging/macro_usage_graph.py +++ b/flipjump/interpretter/debugging/macro_usage_graph.py @@ -21,7 +21,7 @@ def _prepare_first_and_second_level_significant_macros( if len(k_split) != 2: continue parent, name = k_split - if float(v) / macro_code_size[parent] < child_significance_min_thresh: + if macro_code_size[parent] == 0 or float(v) / macro_code_size[parent] < child_significance_min_thresh: continue if len(name.split(':')) == 4: # if it's a rep continue @@ -60,9 +60,10 @@ def _show_macro_usage_graph(chosen_macros: List[Tuple[str, int]]) -> None: ordered_chosen_macros = sorted(chosen_macros, key=lambda name_count: name_count[1], reverse=True) total_ops = sum([count for name, count in chosen_macros]) - print('\n\n\nThe most used macros are:\n') - for macro_name, ops_count in ordered_chosen_macros: - print(f' {macro_name}: {ops_count:,} ops ({ops_count / total_ops:.2%})') + if total_ops > 0: + print('\n\n\nThe most used macros are:\n') + for macro_name, ops_count in ordered_chosen_macros: + print(f' {macro_name}: {ops_count:,} ops ({ops_count / total_ops:.2%})') print( "\n\n* The statistics can be displayed in an interactive graph - " "that feature requires the plotly python library. *\n" diff --git a/flipjump/interpretter/fjm_run.py b/flipjump/interpretter/fjm_run.py index 73c00e0..45ee794 100644 --- a/flipjump/interpretter/fjm_run.py +++ b/flipjump/interpretter/fjm_run.py @@ -47,8 +47,12 @@ def print( @param output_to_print: if specified and terminated not by looping - print the given output. """ - flips_percentage = self.flip_counter / self.op_counter * 100 - jumps_percentage = self.jump_counter / self.op_counter * 100 + if self.op_counter: + flips_percentage = self.flip_counter / self.op_counter * 100 + jumps_percentage = self.jump_counter / self.op_counter * 100 + else: + flips_percentage = 0 + jumps_percentage = 0 last_ops_str = '' output_str = ''