This commit is contained in:
@@ -6,6 +6,30 @@ namespace Just.PreciseMath.Tests;
|
||||
|
||||
public class DoubleDoubleFormattingTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("G0")]
|
||||
[InlineData("g0")]
|
||||
public void ZeroGeneralPrecisionUses32SignificantDigits(string format)
|
||||
{
|
||||
// Exact dyadic 1 + 2^-54, rounded in decimal to 32 significant digits.
|
||||
DoubleDouble value = DoubleDouble.FromComponents(1.0, Math.ScaleB(1.0, -54));
|
||||
const string expected = "1.0000000000000000555111512312578";
|
||||
value.ToString(format, CultureInfo.InvariantCulture).ShouldBe(expected);
|
||||
((IFormattable)value).ToString(format, CultureInfo.InvariantCulture).ShouldBe(expected);
|
||||
Span<char> destination = stackalloc char[64];
|
||||
value.TryFormat(destination, out int written, format, CultureInfo.InvariantCulture).ShouldBeTrue();
|
||||
destination[..written].ToString().ShouldBe(expected);
|
||||
|
||||
// A zero residual lets the independent BCL G32 formatter supply the oracle,
|
||||
// including the scientific-notation threshold and exponent letter case.
|
||||
string referenceFormat = format[0] == 'G' ? "G32" : "g32";
|
||||
foreach (double scalar in new double[] { 1e31, 1e32, 1e-5, -0.0, double.Epsilon, double.MaxValue })
|
||||
{
|
||||
new DoubleDouble(scalar).ToString(format, CultureInfo.InvariantCulture)
|
||||
.ShouldBe(scalar.ToString(referenceFormat, CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GeneralFormattingTrimsOnlyFractionalZerosAtMaximumPrecision()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user