From 29ff4080e7c0cc634fb67e96ef8d1474b133d45a Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 27 Sep 2026 18:53:06 +0000 Subject: [PATCH] The parse cache keeps a parse for each value of the downcasting setting FromString kept one cache for each value of ExplicitParsingOnly, but not of DowncastingEnabled, and that setting changes the parse: with it off, the 2 in x^2 is a decimal. A string parsed once under one setting came back under the other with the first parse's numbers. There is one cache for each pair of the two settings now. Closes #1513. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- Sources/AngouriMath/Convenience/MathS.cs | 16 +++++++++---- .../UnitTests/Convenience/FromStringTest.cs | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index 0aca8ad1b..26efaadbf 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -4179,15 +4179,23 @@ public static Entity FromString(string expr, bool useCache) "-1" => Integer.Create(-1), "+oo" => Real.PositiveInfinity, "-oo" => Real.NegativeInfinity, - _ => Settings.ExplicitParsingOnly.Value switch + // One cache per pair of the settings the parse reads: with the + // downcasting off a whole number parses as a decimal, so a string + // parsed under one setting is not the other's parse. + // https://github.com/asc-community/AngouriMath/issues/1513 + _ => (Settings.ExplicitParsingOnly.Value, Settings.DowncastingEnabled.Value) switch { - false => stringToEntityCache.GetValue(expr, _ => parsed.Value), - true => stringToEntityCacheExplicitOnly.GetValue(expr, _ => parsed.Value) - } + (false, true) => stringToEntityCache.GetValue(expr, _ => parsed.Value), + (true, true) => stringToEntityCacheExplicitOnly.GetValue(expr, _ => parsed.Value), + (false, false) => stringToEntityCacheNotDowncasting.GetValue(expr, _ => parsed.Value), + (true, false) => stringToEntityCacheExplicitOnlyNotDowncasting.GetValue(expr, _ => parsed.Value) + } } }; private static ConditionalWeakTable stringToEntityCacheExplicitOnly = new(); private static ConditionalWeakTable stringToEntityCache = new(); + private static ConditionalWeakTable stringToEntityCacheNotDowncasting = new(); + private static ConditionalWeakTable stringToEntityCacheExplicitOnlyNotDowncasting = new(); /// Converts a to an expression /// expression, for example, "2 * x + 3 + sqrt(x)" diff --git a/Sources/Tests/UnitTests/Convenience/FromStringTest.cs b/Sources/Tests/UnitTests/Convenience/FromStringTest.cs index bd147a2ab..5b5fa206e 100644 --- a/Sources/Tests/UnitTests/Convenience/FromStringTest.cs +++ b/Sources/Tests/UnitTests/Convenience/FromStringTest.cs @@ -452,6 +452,29 @@ public void ReturnsTheFollowing(string exprRaw, bool explicitOnly, string result ) .Should().Be(result) ); + + /// + /// The cache keeps a parse for each value of the downcasting setting, which changes the + /// parse: with it off, a whole number is a decimal. A string parsed first with the setting + /// on came back with it off as the first parse. + /// https://github.com/asc-community/AngouriMath/issues/1513 + /// + [Fact] + public void TheCacheKeepsAParseForEachDowncastingSetting() + { + // No other test parses this string, so no other test's cache entry answers it. + const string text = "x ^ 7 + 1513"; + var on = FromString(text); + Entity off, parsedOff; + using (Settings.DowncastingEnabled.Set(false)) + { + off = FromString(text); + parsedOff = FromString(text, useCache: false); + } + Assert.Equal(parsedOff, off); + Assert.Contains(off.Nodes, node => node is Real and not Rational); + Assert.DoesNotContain(on.Nodes, node => node is Real and not Rational); + } } }