From 07fc74aaf4ad139f9af8f1187a6bd97f5b8ff67a Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 29 Sep 2012 16:43:34 +0200 Subject: [PATCH] Add special case for negative zero in CSharpOutputVisitor. --- .../OutputVisitor/CSharpOutputVisitor.cs | 12 ++++++++++++ .../CSharp/CSharpOutputVisitorTests.cs | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs index 665e15e71a..db8335f686 100644 --- a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs +++ b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs @@ -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 } 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"; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs index 8b662c6233..d625d75f89 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs @@ -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)); + } } }