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.
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);
}
}