Add exact arithmetic implementation and tests
- Introduced exact arithmetic with support for large integers and fractions in `exact.gd`, `big_integer.gd`, and `exact_value.gd`. - Implemented a comprehensive test suite in `test_exact_math.gd` to validate arithmetic operations against expected results. - Added a README in `math` directory to document the exact arithmetic system and its usage. - Ensured that all arithmetic operations maintain precision without floating-point errors, using a custom representation for large numbers. - Included methods for encoding and decoding exact values for serialization.
This commit is contained in:
+22
-21
@@ -1,4 +1,5 @@
|
||||
extends SceneTree
|
||||
const E = preload("res://world/owniverse/math/exact.gd")
|
||||
|
||||
var failures: int = 0
|
||||
var checks: int = 0
|
||||
@@ -7,41 +8,41 @@ func _initialize() -> void:
|
||||
call_deferred("run")
|
||||
|
||||
func check(value: bool, message: String) -> void:
|
||||
checks += 1
|
||||
if not value:
|
||||
failures += 1
|
||||
checks = E.add(checks, 1)
|
||||
if (not value):
|
||||
failures = E.add(failures, 1)
|
||||
push_error(message)
|
||||
|
||||
func run() -> void:
|
||||
var actions = load("res://ui/actions/actions.tscn").instantiate()
|
||||
root.add_child(actions)
|
||||
for maximum in [0.0, 0.1, 0.99, 1.0, 1.9, 10.0, 100.0, 123.0, 1000.0, 10000.0, 1e9, 1.1e9, 1e150]:
|
||||
for maximum in [0, E.of("0.1"), E.of("0.99"), 1, E.of("1.9"), 10, 100, 123, 1000, 10000, E.of("1e9"), E.of("1.1e9"), E.of("1e150")]:
|
||||
actions.set_actions("test", maximum)
|
||||
var labels: Array = []
|
||||
var values: Array = []
|
||||
for i in 5:
|
||||
var value: float = actions.action_values[i]
|
||||
var value: Variant = actions.action_values[i]
|
||||
var text: String = actions.action_labels[i].text
|
||||
check(value == floorf(value) and value <= floorf(maximum), "Whole affordable request")
|
||||
if value > 0.0:
|
||||
check(not values.has(value) and not labels.has(text), "Unique requests and displayed labels")
|
||||
check((E.eq(value, E.floor_value(value)) and E.le(value, E.floor_value(maximum))), "Whole affordable request")
|
||||
if E.gt(value, 0):
|
||||
check(((not values.has(value)) and (not labels.has(text))), "Unique requests and displayed labels")
|
||||
values.append(value)
|
||||
labels.append(text)
|
||||
else:
|
||||
check(text == "" and actions.action_labels[i].get_parent().get_node("TextureButton").disabled, "Unused button blank and disabled")
|
||||
if maximum >= 1.0: check(values.back() == floorf(maximum), "Maximum retained")
|
||||
actions.set_actions("test", 100.0)
|
||||
check(actions.action_values.values() == [1.0, 10.0, 100.0, 0.0, 0.0], "Exact power of ten not duplicated")
|
||||
actions.set_actions("SMBH", 25.0, func(_request): return 25.0)
|
||||
check(actions.action_values.values() == [25.0, 0.0, 0.0, 0.0, 0.0], "Identical SMBH previews collapse to one button")
|
||||
actions.set_actions("step", 123.0, func(request): return floorf(request / 5.0) * 5.0)
|
||||
check(actions.action_values.values() == [10.0, 100.0, 123.0, 0.0, 0.0], "Unproducible small requests omitted")
|
||||
check(actions.action_labels[2].text == "120", "Recipe preview displayed while original request retained")
|
||||
check((E.eq(text, "") and actions.action_labels[i].get_parent().get_node("TextureButton").disabled), "Unused button blank and disabled")
|
||||
if E.ge(maximum, 1): check(E.eq(values.back(), E.floor_value(maximum)), "Maximum retained")
|
||||
actions.set_actions("test", 100)
|
||||
check(E.eq(actions.action_values.values(), [1, 10, 100, 0, 0]), "Exact power of ten not duplicated")
|
||||
actions.set_actions("SMBH", 25, func ( _request ) : return 25)
|
||||
check(E.eq(actions.action_values.values(), [25, 0, 0, 0, 0]), "Identical SMBH previews collapse to one button")
|
||||
actions.set_actions("step", 123, func ( request ) : return E.multiply(E.floor_value(E.divide(request, 5)), 5))
|
||||
check(E.eq(actions.action_values.values(), [10, 100, 123, 0, 0]), "Unproducible small requests omitted")
|
||||
check(E.eq(actions.action_labels[2].text, "120"), "Recipe preview displayed while original request retained")
|
||||
var emitted: Array = []
|
||||
actions.action_pressed.connect(func(_page, amount): emitted.append(amount))
|
||||
actions.action_pressed.connect(func ( _page , amount ) : emitted.append(amount))
|
||||
actions._on_action_button_pressed(2)
|
||||
actions._on_action_button_pressed(4)
|
||||
check(emitted == [123.0], "Only active button emits its matching request")
|
||||
check(E.eq(emitted, [123]), "Only active button emits its matching request")
|
||||
actions.queue_free()
|
||||
print("Action values: %d checks, %d failures" % [checks, failures])
|
||||
quit(0 if failures == 0 else 1)
|
||||
print(("Action values: %d checks, %d failures" % [checks, failures]))
|
||||
quit((0 if E.eq(failures, 0) else 1))
|
||||
|
||||
Reference in New Issue
Block a user