diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.cs index d28027739..b72dafed0 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.cs @@ -67,6 +67,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public const decimal DecimalMaxValue = decimal.MaxValue; public const decimal DecimalMinValue = decimal.MinValue; + public const float Float_One = 1f; + public const double Double_One = 1.0; + public const float Float_Two = 2f; + public const double Double_Two = 2.0; + public const float Float_PI = (float)Math.PI; public const float Float_HalfOfPI = (float)Math.PI / 2f; public const float Float_QuarterOfPI = (float)Math.PI / 4f; diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.il index 28f429981..eee91fe32 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.il @@ -78,6 +78,10 @@ uint32, uint32) = ( 01 00 00 80 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 ) + .field public static literal float32 Float_One = float32(1.) + .field public static literal float64 Double_One = float64(1.) + .field public static literal float32 Float_Two = float32(2.) + .field public static literal float64 Double_Two = float64(2.) .field public static literal float32 Float_PI = float32(3.1415927) .field public static literal float32 Float_HalfOfPI = float32(1.5707964) .field public static literal float32 Float_QuarterOfPI = float32(0.78539819) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.il index ab77fc78b..cdeaea1d1 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.il @@ -78,6 +78,10 @@ uint32, uint32) = ( 01 00 00 80 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 ) + .field public static literal float32 Float_One = float32(1.) + .field public static literal float64 Double_One = float64(1.) + .field public static literal float32 Float_Two = float32(2.) + .field public static literal float64 Double_Two = float64(2.) .field public static literal float32 Float_PI = float32(3.1415927) .field public static literal float32 Float_HalfOfPI = float32(1.5707964) .field public static literal float32 Float_QuarterOfPI = float32(0.78539819) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.roslyn.il index 74a8d8c6a..e8bfc6b42 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.roslyn.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.opt.roslyn.il @@ -82,6 +82,10 @@ uint32, uint32) = ( 01 00 00 80 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 ) + .field public static literal float32 Float_One = float32(1.) + .field public static literal float64 Double_One = float64(1.) + .field public static literal float32 Float_Two = float32(2.) + .field public static literal float64 Double_Two = float64(2.) .field public static literal float32 Float_PI = float32(3.1415927) .field public static literal float32 Float_HalfOfPI = float32(1.5707964) .field public static literal float32 Float_QuarterOfPI = float32(0.78539819) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.roslyn.il index 81c844ce8..4d42d3495 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.roslyn.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/WellKnownConstants.roslyn.il @@ -82,6 +82,10 @@ uint32, uint32) = ( 01 00 00 80 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 ) + .field public static literal float32 Float_One = float32(1.) + .field public static literal float64 Double_One = float64(1.) + .field public static literal float32 Float_Two = float32(2.) + .field public static literal float64 Double_Two = float64(2.) .field public static literal float32 Float_PI = float32(3.1415927) .field public static literal float32 Float_HalfOfPI = float32(1.5707964) .field public static literal float32 Float_QuarterOfPI = float32(0.78539819) diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index fcff9fbc1..052095ae8 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -2270,9 +2270,14 @@ namespace ICSharpCode.Decompiler.CSharp container.Peek().Elements.Add(aie); container.Push(aie); } - astBuilder.UseSpecialConstants = !type.IsCSharpPrimitiveIntegerType(); - var val = Translate(value, typeHint: type).ConvertTo(type, this, allowImplicitConversion: true); - astBuilder.UseSpecialConstants = true; + TranslatedExpression val; + var old = astBuilder.UseSpecialConstants; + try { + astBuilder.UseSpecialConstants = !type.IsCSharpPrimitiveIntegerType(); + val = Translate(value, typeHint: type).ConvertTo(type, this, allowImplicitConversion: true); + } finally { + astBuilder.UseSpecialConstants = old; + } container.Peek().Elements.Add(val); elementResolveResults.Add(val.ResolveResult); while (container.Count > 0 && container.Peek().Elements.Count == dimensionSizes[container.Count - 1]) { diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs index 723c5a804..42e0311f3 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs @@ -937,11 +937,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax Expression expr = null; if (isDouble) { - if ((double)constantValue >= int.MaxValue || (double)constantValue <= int.MinValue) { + if (Math.Floor((double)constantValue) == (double)constantValue) { expr = new PrimitiveExpression(constantValue); } } else { - if ((float)constantValue >= int.MaxValue || (float)constantValue <= int.MinValue) { + if (Math.Floor((float)constantValue) == (float)constantValue) { expr = new PrimitiveExpression(constantValue); } }