From ecf65fbaccbb4bbd9b19359eff0a37e412f19604 Mon Sep 17 00:00:00 2001 From: Kevin Zheng Date: Sat, 23 Dec 2023 20:56:39 -0800 Subject: [PATCH] Clamp after AdjustMoles() (#22907) Clamping is needed because x - x can be negative with floating point numbers. If we don't clamp here, the caller always has to call GetMoles(), clamp, then SetMoles(), which makes this function not very useful. --- Content.Server/Atmos/GasMixture.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Content.Server/Atmos/GasMixture.cs b/Content.Server/Atmos/GasMixture.cs index 1547c259e6..d49d1b78c1 100644 --- a/Content.Server/Atmos/GasMixture.cs +++ b/Content.Server/Atmos/GasMixture.cs @@ -120,12 +120,9 @@ namespace Content.Server.Atmos if (!float.IsFinite(quantity)) throw new ArgumentException($"Invalid quantity \"{quantity}\" specified!", nameof(quantity)); - Moles[gasId] += quantity; - - var moles = Moles[gasId]; - - if (!float.IsFinite(moles) || float.IsNegative(moles)) - throw new Exception($"Invalid mole quantity \"{moles}\" in gas Id {gasId} after adjusting moles with \"{quantity}\"!"); + // Clamping is needed because x - x can be negative with floating point numbers. If we don't + // clamp here, the caller always has to call GetMoles(), clamp, then SetMoles(). + Moles[gasId] = MathF.Max(Moles[gasId] + quantity, 0); } } -- 2.51.2