Skip to content

Commit

Permalink
fix: avoid truncation when serializing floats
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed May 7, 2024
1 parent b42a162 commit c7d8570
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
5 changes: 3 additions & 2 deletions mecha/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def string_to_number(string: str) -> Union[int, float]:
def number_to_string(number: Union[int, float]) -> str:
"""Helper for converting numbers to string and removing scientific notation."""
value = str(number)
if "e" in value:
value = f"{number:.20f}".rstrip("0")
if (index := value.find("e")) != -1:
exponent = int(value[index + 1 :])
value = f"{number:.{max(index - 1 - (value[1] == '.') - exponent, 1)}f}"
return value


Expand Down
1 change: 1 addition & 0 deletions tests/resources/command_examples.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,4 @@ particle minecraft:block{block_state: {Name: "minecraft:redstone_lamp", Properti
clear @s *[custom_data~{gm4_metallurgy: {stored_shamir: "lumos"}}, damage | !max_item_stack=1]
execute if predicate {condition: weather_check,raining: true}
particle minecraft:block{block_state: {Name: "minecraft:redstone_lamp",Properties: {lit: "true"}}}
execute store result storage test:test path double 0.0000000000009094947017729282 run say hello
50 changes: 48 additions & 2 deletions tests/snapshots/parse__command_examples__0.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<class 'mecha.ast.AstRoot'>
location: SourceLocation(pos=0, lineno=1, colno=1)
end_location: SourceLocation(pos=9780, lineno=232, colno=1)
end_location: SourceLocation(pos=9876, lineno=233, colno=1)
commands:
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=14, lineno=2, colno=1)
Expand Down Expand Up @@ -8441,4 +8441,50 @@
<class 'mecha.ast.AstNbtValue'>
location: SourceLocation(pos=9770, lineno=231, colno=90)
end_location: SourceLocation(pos=9776, lineno=231, colno=96)
value: String('true')
value: String('true')
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=9780, lineno=232, colno=1)
end_location: SourceLocation(pos=9875, lineno=232, colno=96)
identifier: 'execute:subcommand'
arguments:
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=9788, lineno=232, colno=9)
end_location: SourceLocation(pos=9875, lineno=232, colno=96)
identifier: 'execute:store:result:storage:target:path:double:scale:subcommand'
arguments:
<class 'mecha.ast.AstResourceLocation'>
location: SourceLocation(pos=9809, lineno=232, colno=30)
end_location: SourceLocation(pos=9818, lineno=232, colno=39)
is_tag: False
namespace: 'test'
path: 'test'
<class 'mecha.ast.AstNbtPath'>
location: SourceLocation(pos=9819, lineno=232, colno=40)
end_location: SourceLocation(pos=9823, lineno=232, colno=44)
components:
<class 'mecha.ast.AstNbtPathKey'>
location: SourceLocation(pos=9819, lineno=232, colno=40)
end_location: SourceLocation(pos=9823, lineno=232, colno=44)
value: 'path'
<class 'mecha.ast.AstNumber'>
location: SourceLocation(pos=9831, lineno=232, colno=52)
end_location: SourceLocation(pos=9861, lineno=232, colno=82)
value: 9.094947017729282e-13
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=9862, lineno=232, colno=83)
end_location: SourceLocation(pos=9875, lineno=232, colno=96)
identifier: 'execute:run:subcommand'
arguments:
<class 'mecha.ast.AstCommand'>
location: SourceLocation(pos=9866, lineno=232, colno=87)
end_location: SourceLocation(pos=9875, lineno=232, colno=96)
identifier: 'say:message'
arguments:
<class 'mecha.ast.AstMessage'>
location: SourceLocation(pos=9870, lineno=232, colno=91)
end_location: SourceLocation(pos=9875, lineno=232, colno=96)
fragments:
<class 'mecha.ast.AstMessageText'>
location: SourceLocation(pos=9870, lineno=232, colno=91)
end_location: SourceLocation(pos=9875, lineno=232, colno=96)
value: 'hello'
1 change: 1 addition & 0 deletions tests/snapshots/parse__command_examples__1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,4 @@ particle minecraft:block{block_state: {Name: "minecraft:redstone_lamp", Properti
clear @s *[custom_data~{gm4_metallurgy: {stored_shamir: "lumos"}}, damage | !max_item_stack=1]
execute if predicate {condition: "weather_check", raining: 1b}
particle minecraft:block{block_state: {Name: "minecraft:redstone_lamp", Properties: {lit: "true"}}}
execute store result storage test:test path double 0.0000000000009094947017729282 run say hello

0 comments on commit c7d8570

Please sign in to comment.