Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Entity> stringToEntityCacheExplicitOnly = new();
private static ConditionalWeakTable<string, Entity> stringToEntityCache = new();
private static ConditionalWeakTable<string, Entity> stringToEntityCacheNotDowncasting = new();
private static ConditionalWeakTable<string, Entity> stringToEntityCacheExplicitOnlyNotDowncasting = new();

/// <summary>Converts a <see cref="string"/> to an expression</summary>
/// <param name="expr"><see cref="string"/> expression, for example, <code>"2 * x + 3 + sqrt(x)"</code></param>
Expand Down
23 changes: 23 additions & 0 deletions Sources/Tests/UnitTests/Convenience/FromStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,29 @@ public void ReturnsTheFollowing(string exprRaw, bool explicitOnly, string result
)
.Should().Be(result)
);

/// <summary>
/// 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
/// </summary>
[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);
}
}
}

Loading