Skip to content

Commit

Permalink
fix possible zero divisions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhea committed Aug 19, 2024
1 parent aa01b31 commit e8f7dba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 5 additions & 4 deletions flipjump/interpretter/debugging/macro_usage_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions flipjump/interpretter/fjm_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down

0 comments on commit e8f7dba

Please sign in to comment.