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:
+20
-19
@@ -1,4 +1,5 @@
|
||||
extends SceneTree
|
||||
const E = preload("res://world/owniverse/math/exact.gd")
|
||||
|
||||
var checks: int = 0
|
||||
var failures: int = 0
|
||||
@@ -7,9 +8,9 @@ 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:
|
||||
@@ -22,39 +23,39 @@ func run() -> void:
|
||||
await process_frame
|
||||
stage.stage_page_scroller.scroll_horizontal = stage.PAGE_WIDTH
|
||||
await process_frame
|
||||
stage.owniverse.state.amount.H = 1000.0
|
||||
stage.owniverse.state.amount.RD = 2.0
|
||||
stage.owniverse.state.amount.H = 1000
|
||||
stage.owniverse.state.amount.RD = 2
|
||||
stage.owniverse.presentation.refresh(stage.owniverse.state, stage.owniverse.catalog)
|
||||
stage.set_owniverse_item_value()
|
||||
var hydrogen = stage.owniverse_items.H
|
||||
var dwarf = stage.owniverse_items.RD
|
||||
check(hydrogen.description.text == "1 000", "Pending quantities reach controls")
|
||||
check(dwarf.description.text == "2", "Other page receives its current quantity")
|
||||
check(E.eq(hydrogen.description.text, "1 000"), "Pending quantities reach controls")
|
||||
check(E.eq(dwarf.description.text, "2"), "Other page receives its current quantity")
|
||||
check(stage.owniverse.presentation.pending_amounts.is_empty(), "Published queue is consumed by Stage")
|
||||
stage.owniverse.presentation.deferred_updates = true
|
||||
stage.owniverse.state.amount.H = 2000.0
|
||||
stage.owniverse.state.amount.H = 2000
|
||||
stage.owniverse.presentation.refresh(stage.owniverse.state, stage.owniverse.catalog)
|
||||
check(stage.owniverse.presentation.dirty and stage.owniverse.presentation.pending_amounts.is_empty(), "Deferred refresh marks dirty without republishing")
|
||||
check((stage.owniverse.presentation.dirty and stage.owniverse.presentation.pending_amounts.is_empty()), "Deferred refresh marks dirty without republishing")
|
||||
stage.set_owniverse_item_value()
|
||||
check(hydrogen.description.text == "1 000", "Labels retain last published quantity")
|
||||
check(E.eq(hydrogen.description.text, "1 000"), "Labels retain last published quantity")
|
||||
stage.owniverse.presentation.refresh(stage.owniverse.state, stage.owniverse.catalog, true)
|
||||
stage.set_owniverse_item_value()
|
||||
check(hydrogen.description.text == "2 000" and not stage.owniverse.presentation.dirty, "Forced refresh publishes latest state")
|
||||
check((E.eq(hydrogen.description.text, "2 000") and (not stage.owniverse.presentation.dirty)), "Forced refresh publishes latest state")
|
||||
stage.owniverse.presentation.deferred_updates = false
|
||||
stage.item_activated(hydrogen)
|
||||
stage.set_owniverse_item_value()
|
||||
check(hydrogen.description.text == root.get_node("Global").format_number(stage.owniverse.state.amount.H), "Player production publishes the new quantity")
|
||||
check(stage.owniverse.state.amount.H > 2000.0, "Production action actually executed")
|
||||
check(E.eq(hydrogen.description.text, root.get_node("Global").format_number(stage.owniverse.state.amount.H)), "Player production publishes the new quantity")
|
||||
check(E.gt(stage.owniverse.state.amount.H, 2000), "Production action actually executed")
|
||||
dwarf.visible = true
|
||||
stage.stage_page_scroller.scroll_horizontal = 2 * stage.PAGE_WIDTH
|
||||
stage.stage_page_scroller.scroll_horizontal = E.multiply(2, stage.PAGE_WIDTH)
|
||||
await process_frame
|
||||
stage._refresh_universe_ui()
|
||||
check(dwarf.description.text == "2", "Newly visible page receives cached latest values")
|
||||
stage.owniverse.state.time.year = 1234.5
|
||||
check(E.eq(dwarf.description.text, "2"), "Newly visible page receives cached latest values")
|
||||
stage.owniverse.state.time.year = E.of("1234.5")
|
||||
stage._refresh_universe_ui()
|
||||
check(stage.date_time.text == "1 234.5", "Time displays tenths with thousands separator")
|
||||
check(E.eq(stage.date_time.text, "1 234.5"), "Time displays tenths with thousands separator")
|
||||
for entity in stage.owniverse.catalog.entities.values(): entity.free()
|
||||
stage.owniverse.free()
|
||||
scene.free()
|
||||
print("UI updates: %d checks, %d failures" % [checks, failures])
|
||||
quit(0 if failures == 0 else 1)
|
||||
print(("UI updates: %d checks, %d failures" % [checks, failures]))
|
||||
quit((0 if E.eq(failures, 0) else 1))
|
||||
|
||||
Reference in New Issue
Block a user