#pragma once #include #include // Time-based rates (ADR-0099, ratified direction ADR-0089). A biological or environmental // rate is stored per second, in its integer base unit (pJ, fg, um), and converted to a // per-tick amount at the fixed simulation tick. Conversion keeps no state: an amount that // does not divide evenly is rounded in the direction that never favours the organism // (costs up, capacities down), so the error is below one base unit per tick. namespace evo::accounting { inline constexpr std::uint64_t microseconds_per_second = 1'000'000; inline constexpr std::uint64_t max_tick_microseconds = microseconds_per_second; // down: a capacity, limit or speed (never more than the rate allows). // up: a cost (never less than the rate demands). enum class Rounding : std::uint8_t { down, up }; enum class RateError : std::uint8_t { invalid_tick, overflow, inexact }; // per_second * tick_microseconds / 1 s, rounded as named. The tick is 1 us .. 1 s. [[nodiscard]] std::expected per_tick(std::uint64_t per_second, std::uint64_t tick_microseconds, Rounding rounding) noexcept; // Whether per_tick needs no rounding at this tick. [[nodiscard]] bool exact_per_tick(std::uint64_t per_second, std::uint64_t tick_microseconds) noexcept; // The migration's inverse: the per-second rate whose per-tick amount at this tick is // exactly `per_tick_amount`. A value that has no exact per-second form is refused // (`inexact`), never rounded: migrating must not change behaviour at the recorded tick. [[nodiscard]] std::expected per_second_from_per_tick(std::uint64_t per_tick_amount, std::uint64_t tick_microseconds) noexcept; } // namespace evo::accounting