Solution precision fixes (#25199)

* Add test for two chemistry issues

1. rounding issue with reaction processing when making chloral hydrate
2. reliable assert trip due to the ValidateSolution() heat capacity issue.

* Fix FixedPoint2 arithmetic

Fix internal floating point arithmetic in places where it could be avoided.

Fix incorrect rounding mode used in other places (it should always floor, like regular int arithmetic).

I had to add an explicit epsilon value for float -> FixedPoint2 because something like 1.05 is actually like 1.04999 and that'd cause it to be rounded down to 1.04.

This fixes reaction reagent processing in cases where the reagent inputs can't cleanly divide. Previously, when making 30u chloral hydrate by adding the chlorine in 10u increments you'd end up with 0.04 chlorine left over. This was caused by division in the reaction code rounding up in some cases. Changing division here to always round down fixes it.

* Attempt to fix heat capacity precision assert issues.

Fixes #22126

First, we just increase the tolerance of the assert. It was way too low.

Second, actually put a cap on float drift from one-off _heatCapacity changes.

* Fix float -> FixedPoint2 epsilon for negative number, fix tests.

* Fix DamageableTest

* Oh yeah I need to call CleanReturnAsync
This commit is contained in:
Pieter-Jan Briers
2024-02-17 00:54:27 +01:00
committed by GitHub
parent c7870882f6
commit 33611b7094
6 changed files with 232 additions and 49 deletions

View File

@@ -20,6 +20,9 @@ namespace Content.Shared.FixedPoint
public static FixedPoint2 Epsilon { get; } = new(1);
public static FixedPoint2 Zero { get; } = new(0);
// This value isn't picked by any proper testing, don't @ me.
private const float FloatEpsilon = 0.00001f;
#if DEBUG
static FixedPoint2()
{
@@ -47,7 +50,17 @@ namespace Content.Shared.FixedPoint
public static FixedPoint2 New(float value)
{
return new((int) MathF.Round(value * ShiftConstant, MidpointRounding.AwayFromZero));
return new((int) ApplyFloatEpsilon(value * ShiftConstant));
}
private static float ApplyFloatEpsilon(float value)
{
return value + FloatEpsilon * Math.Sign(value);
}
private static double ApplyFloatEpsilon(double value)
{
return value + FloatEpsilon * Math.Sign(value);
}
/// <summary>
@@ -60,17 +73,12 @@ namespace Content.Shared.FixedPoint
public static FixedPoint2 New(double value)
{
return new((int) Math.Round(value * ShiftConstant, MidpointRounding.AwayFromZero));
return new((int) ApplyFloatEpsilon(value * ShiftConstant));
}
public static FixedPoint2 New(string value)
{
return New(FloatFromString(value));
}
private static float FloatFromString(string value)
{
return float.Parse(value, CultureInfo.InvariantCulture);
return New(Parse.Float(value));
}
public static FixedPoint2 operator +(FixedPoint2 a) => a;
@@ -85,17 +93,17 @@ namespace Content.Shared.FixedPoint
public static FixedPoint2 operator *(FixedPoint2 a, FixedPoint2 b)
{
return new((int) MathF.Round(b.Value * a.Value / (float) ShiftConstant, MidpointRounding.AwayFromZero));
return new(b.Value * a.Value / ShiftConstant);
}
public static FixedPoint2 operator *(FixedPoint2 a, float b)
{
return new((int) MathF.Round(a.Value * b, MidpointRounding.AwayFromZero));
return new((int) ApplyFloatEpsilon(a.Value * b));
}
public static FixedPoint2 operator *(FixedPoint2 a, double b)
{
return new((int) Math.Round(a.Value * b, MidpointRounding.AwayFromZero));
return new((int) ApplyFloatEpsilon(a.Value * b));
}
public static FixedPoint2 operator *(FixedPoint2 a, int b)
@@ -105,12 +113,12 @@ namespace Content.Shared.FixedPoint
public static FixedPoint2 operator /(FixedPoint2 a, FixedPoint2 b)
{
return new((int) MathF.Round((ShiftConstant * a.Value) / (float) b.Value, MidpointRounding.AwayFromZero));
return new((int) (ShiftConstant * (long) a.Value / b.Value));
}
public static FixedPoint2 operator /(FixedPoint2 a, float b)
{
return new((int) MathF.Round(a.Value / b, MidpointRounding.AwayFromZero));
return new((int) ApplyFloatEpsilon(a.Value / b));
}
public static bool operator <=(FixedPoint2 a, int b)
@@ -185,7 +193,7 @@ namespace Content.Shared.FixedPoint
public readonly int Int()
{
return (int) ShiftDown();
return Value / ShiftConstant;
}
// Implicit operators ftw
@@ -266,7 +274,7 @@ namespace Content.Shared.FixedPoint
if (value == "MaxValue")
Value = int.MaxValue;
else
this = New(FloatFromString(value));
this = New(Parse.Float(value));
}
public override readonly string ToString() => $"{ShiftDown().ToString(CultureInfo.InvariantCulture)}";