Actor: pin the onset's rounding; clamp an out-of-range onset instead of casting it

A case whose quotient sits just under an integer (0.29 s at dt 0.01 is
28.999999999999996 ticks) kills Round -> Floor. An onset whose tick count is
past the long range, +infinity included, is now one guard (>= long.MaxValue)
rather than a cast that is undefined across runtimes and could go negative on
one that wraps; huge finite onsets (1e300, 1e19) are tested.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 05:21:45 +01:00
commit e5a66bbc54
2 changed files with 22 additions and 2 deletions

View file

@ -62,8 +62,9 @@ namespace Lodur
{
_dwell[g] = (long)Math.Round(brain.Goals[g].MinDwellSeconds / tickSeconds);
// Rounded to whole ticks once, like the dwell, so the onset is the same number of ticks whatever the tick length does to a product.
var onset = brain.Goals[g].InvoluntarySeconds;
_onset[g] = double.IsPositiveInfinity(onset) ? long.MaxValue : (long)Math.Round(onset / tickSeconds);
// A quotient past the long range (+infinity included) is involuntary throughout: the cast of an out-of-range double is not defined across runtimes.
var onsetTicks = brain.Goals[g].InvoluntarySeconds / tickSeconds;
_onset[g] = onsetTicks >= long.MaxValue ? long.MaxValue : (long)Math.Round(onsetTicks);
}
_perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion);

View file

@ -368,6 +368,7 @@ public sealed class WillTests
[InlineData(1.0 / 120.0, 1.85, 222)] // 222 x (1/120) is just under 1.85 in floating point
[InlineData(1.0 / 60.0, 0.9, 54)]
[InlineData(0.01, 0.5, 50)]
[InlineData(0.01, 0.29, 29)] // 0.29 / 0.01 is 28.999999999999996: rounds to 29, floors to 28
public void The_involuntary_onset_is_the_requested_seconds_in_whole_ticks_whatever_the_tick_length(double dt, double onset, long ticks)
{
var brain = new Brain { Goals = new[] { new GoalSpec { Id = "g", Effort = 1.0, InvoluntarySeconds = onset, Behaviour = Pose } } };
@ -387,6 +388,24 @@ public sealed class WillTests
Assert.Equal(ticks, firstWeighted);
}
[Theory]
[InlineData(1e300)]
[InlineData(1e19)]
[InlineData(double.PositiveInfinity)]
public void An_onset_past_the_range_of_a_tick_count_is_involuntary_throughout_not_a_wrapped_negative(double onset)
{
var brain = new Brain { Goals = new[] { new GoalSpec { Id = "g", Effort = 1.0, InvoluntarySeconds = onset, Behaviour = Pose } } };
var actor = new Actor(0.01, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters { ResolveRecovery = 0.0 }, brain);
float[] p = { 0f }, strengths = new float[2];
var targets = new Quat[2];
for (var i = 0; i < 500; i++)
{
actor.Tick(p, p, p, System.Array.Empty<BoxStimulus>(), strengths, targets);
Assert.Equal(1.0, actor.Drive.Resolve);
Assert.Equal(1f, strengths[0]);
}
}
[Fact]
public void A_puppet_that_starts_in_a_goal_with_an_entry_resolve_begins_at_it()
{