Skip to content

Commit

Permalink
fix(gds): float values in decompiled GDA scripts are now displayed ro…
Browse files Browse the repository at this point in the history
…unded to the lowest number of decimal points that still produces identical byte data.
  • Loading branch information
ilonachan committed Sep 30, 2024
1 parent 91aea8b commit ca84de1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion formats/gds/gda.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
GDSConditionToken
)

from utils import round_perfect

def read_gda(data: str, path: Optional[str] = None) -> GDSProgram:
pass
Expand Down Expand Up @@ -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":
Expand Down
14 changes: 14 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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("<f", struct.pack("<f", y))[0] == x:
return y
# We couldn't round without loss of information
return x

0 comments on commit ca84de1

Please sign in to comment.