From ca84de1f7f387617f4a70d3f1fc9a4f500298ea3 Mon Sep 17 00:00:00 2001 From: ilonachan Date: Mon, 30 Sep 2024 22:17:05 +0200 Subject: [PATCH] fix(gds): float values in decompiled GDA scripts are now displayed rounded to the lowest number of decimal points that still produces identical byte data. --- formats/gds/gda.py | 3 ++- utils.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/formats/gds/gda.py b/formats/gds/gda.py index f82fdd4..fa92f3a 100644 --- a/formats/gds/gda.py +++ b/formats/gds/gda.py @@ -18,6 +18,7 @@ GDSConditionToken ) +from utils import round_perfect def read_gda(data: str, path: Optional[str] = None) -> GDSProgram: pass @@ -101,7 +102,7 @@ def write_simple(ctx: WriterContext, cmd: GDSInvocation): ctx.write(str(val)) elif arg in GDSValue.float: val: float = arg() - ctx.write(str(val)) + ctx.write(str(round_perfect(val))) elif arg in GDSValue.str or arg in GDSValue.longstr: val: str = arg() if param.type == "longstr": diff --git a/utils.py b/utils.py index c90d689..b572fd5 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,7 @@ from contextlib import contextmanager, suppress from typing import (Any, Callable, Generic, Mapping, TypeVar, Union, get_origin, get_type_hints) +import struct def cli_file_pairs( @@ -207,3 +208,16 @@ class NestedBreakException(Exception): pass with suppress(NestedBreakException): yield NestedBreakException + +def round_places(x: float, places: int=0) -> float: + return round(x * (10**places)) / (10**places) + +def round_perfect(x: float) -> float: + for i in range(1, 8): + y = round_places(x, i) + # If the rounded form is equivalent in bits to the full version, + # just use this simpler rounded version. + if struct.unpack("