Browse Source

Add special case for negative zero in CSharpOutputVisitor.

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
07fc74aaf4
  1. 12
      ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs
  2. 9
      ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs

12
ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -1057,6 +1057,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1057,6 +1057,12 @@ namespace ICSharpCode.NRefactory.CSharp
}
return;
}
if (f == 0 && 1 / f == float.NegativeInfinity) {
// negative zero is a special case
// (again, not a primitive expression, but it's better to handle
// the special case here than to do it in all code generators)
formatter.WriteToken("-");
}
formatter.WriteToken(f.ToString("R", NumberFormatInfo.InvariantInfo) + "f");
lastWritten = LastWritten.Other;
} else if (val is double) {
@ -1075,6 +1081,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1075,6 +1081,12 @@ namespace ICSharpCode.NRefactory.CSharp
}
return;
}
if (f == 0 && 1 / f == double.NegativeInfinity) {
// negative zero is a special case
// (again, not a primitive expression, but it's better to handle
// the special case here than to do it in all code generators)
formatter.WriteToken("-");
}
string number = f.ToString("R", NumberFormatInfo.InvariantInfo);
if (number.IndexOf('.') < 0 && number.IndexOf('E') < 0) {
number += ".0";

9
ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs

@ -138,5 +138,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -138,5 +138,14 @@ namespace ICSharpCode.NRefactory.CSharp
"case 3: {\n$int a = 3;\n$return a;\n}\n" +
"default:\n$break;\n}\n", type);
}
[Test]
public void ZeroLiterals()
{
AssertOutput("0.0", new PrimitiveExpression(0.0));
AssertOutput("-0.0", new PrimitiveExpression(-0.0));
AssertOutput("0f", new PrimitiveExpression(0f));
AssertOutput("-0f", new PrimitiveExpression(-0f));
}
}
}

Loading…
Cancel
Save