Browse Source

Do not use a fractional representation, if it has less than 6 significant digits.

pull/1350/head
Siegfried Pammer 7 years ago
parent
commit
2413eef1ed
  1. 33
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

33
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -949,30 +949,24 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
ICompilation compilation = type.GetDefinition().Compilation; ICompilation compilation = type.GetDefinition().Compilation;
Expression expr = null; Expression expr = null;
string str;
if (isDouble) { if (isDouble) {
if (Math.Floor((double)constantValue) == (double)constantValue) { if (Math.Floor((double)constantValue) == (double)constantValue) {
expr = new PrimitiveExpression(constantValue); expr = new PrimitiveExpression(constantValue);
} }
str = ((double)constantValue).ToString("r");
} else { } else {
if (Math.Floor((float)constantValue) == (float)constantValue) { if (Math.Floor((float)constantValue) == (float)constantValue) {
expr = new PrimitiveExpression(constantValue); expr = new PrimitiveExpression(constantValue);
} }
}
if (expr == null) { str = ((float)constantValue).ToString("r");
(long num, long den) = isDouble
? FractionApprox((double)constantValue, MAX_DENOMINATOR)
: FractionApprox((float)constantValue, MAX_DENOMINATOR);
if (IsValidFraction(num, den) && IsEqual(num, den, constantValue, isDouble) && Math.Abs(num) != 1 && Math.Abs(den) != 1) {
var left = MakeConstant(type, num);
var right = MakeConstant(type, den);
return new BinaryOperatorExpression(left, BinaryOperatorType.Divide, right).WithoutILInstruction()
.WithRR(new ConstantResolveResult(type, constantValue));
}
} }
if (expr == null && UseSpecialConstants) { bool useFraction = (str.Length - (str.StartsWith("-", StringComparison.OrdinalIgnoreCase) ? 2 : 1) > 5);
if (useFraction && expr == null && UseSpecialConstants) {
IType mathType; IType mathType;
if (isDouble) if (isDouble)
mathType = compilation.FindType(typeof(Math)); mathType = compilation.FindType(typeof(Math));
@ -984,6 +978,19 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
?? TryExtractExpression(mathType, type, constantValue, "E", isDouble); ?? TryExtractExpression(mathType, type, constantValue, "E", isDouble);
} }
if (useFraction && expr == null) {
(long num, long den) = isDouble
? FractionApprox((double)constantValue, MAX_DENOMINATOR)
: FractionApprox((float)constantValue, MAX_DENOMINATOR);
if (IsValidFraction(num, den) && IsEqual(num, den, constantValue, isDouble) && Math.Abs(num) != 1 && Math.Abs(den) != 1) {
var left = MakeConstant(type, num);
var right = MakeConstant(type, den);
return new BinaryOperatorExpression(left, BinaryOperatorType.Divide, right).WithoutILInstruction()
.WithRR(new ConstantResolveResult(type, constantValue));
}
}
if (expr == null) if (expr == null)
expr = new PrimitiveExpression(constantValue); expr = new PrimitiveExpression(constantValue);

Loading…
Cancel
Save