CSharpWriter uses G17 to write doubles and G9 to write singles, for correct round-trip behavior (#235)

This commit is contained in:
Tom Minka 2020-04-08 10:44:34 +01:00 коммит произвёл GitHub
Родитель 27200bd649
Коммит 911c2d1444
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 8 добавлений и 8 удалений

Просмотреть файл

@ -569,7 +569,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
}
else
{
string s = d.ToString("R", CultureInfo.InvariantCulture);
string s = d.ToString("G17", CultureInfo.InvariantCulture);
sb.Append(s);
if (!s.Contains(".") && !s.Contains("E") && !s.Contains("e"))
sb.Append(".0");
@ -605,7 +605,7 @@ namespace Microsoft.ML.Probabilistic.Compiler
}
else
{
sb.Append(f.ToString("R", CultureInfo.InvariantCulture));
sb.Append(f.ToString("G9", CultureInfo.InvariantCulture));
sb.Append("F");
}
}

Просмотреть файл

@ -229,15 +229,15 @@ namespace Microsoft.ML.Probabilistic.Tests.CodeModel
Assert.Equal("0.0", WriteLiteralExpression(0.0));
Assert.Equal("(-1.0 * 0.0)", WriteLiteralExpression(-1.0 * 0.0)); // some build configurations result in -0.0 producing a positive zero, hence (-1.0 * 0.0)
Assert.Equal("42.0", WriteLiteralExpression(42.0000000));
Assert.Equal("3.14159265", WriteLiteralExpression(3.14159265));
Assert.Equal("-3.14159265", WriteLiteralExpression(-3.14159265));
Assert.Equal("3.1415926500000002", WriteLiteralExpression(3.14159265));
Assert.Equal("-3.1415926500000002", WriteLiteralExpression(-3.14159265));
Assert.Equal("1000000.0", WriteLiteralExpression(1e6));
Assert.Equal("1234567.89", WriteLiteralExpression(1.23456789E6));
Assert.Equal("1.23456789E+42", WriteLiteralExpression(1.23456789e+42));
Assert.Equal("1234567.8899999999", WriteLiteralExpression(1.23456789E6));
Assert.Equal("1.2345678900000001E+42", WriteLiteralExpression(1.23456789e+42));
Assert.Equal("1.23456789E-42", WriteLiteralExpression(1.23456789e-42));
Assert.Equal("-1000000.0", WriteLiteralExpression(-1e6));
Assert.Equal("-1234567.89", WriteLiteralExpression(-1.23456789E6));
Assert.Equal("-1.23456789E+42", WriteLiteralExpression(-1.23456789e+42));
Assert.Equal("-1234567.8899999999", WriteLiteralExpression(-1.23456789E6));
Assert.Equal("-1.2345678900000001E+42", WriteLiteralExpression(-1.23456789e+42));
Assert.Equal("-1.23456789E-42", WriteLiteralExpression(-1.23456789e-42));
Assert.Equal("double.PositiveInfinity", WriteLiteralExpression(double.PositiveInfinity));
Assert.Equal("double.NegativeInfinity", WriteLiteralExpression(double.NegativeInfinity));