Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naming of well known constants in C#, VB code (Math.PI instead of 3.1415926535897931) #517

Closed
KvanTTT opened this issue Nov 26, 2014 · 5 comments · Fixed by #1350
Closed
Assignees
Labels
C# Decompiler The decompiler engine itself New Feature
Milestone

Comments

@KvanTTT
Copy link

KvanTTT commented Nov 26, 2014

It would be good to convert double numbers to well know constants (such as Math.PI, Math.E) and their combinations (Math.PI * 2).

I mean decompile IL to:

if (x > Math.PI)
    x -= Math.PI * 2;

instead of:

if (x > 3.1415926535897931)
    x -= 6.2831853071795862;
@PoroCYon
Copy link

This can get quite hard, because constants (and constant expressions, like PI * 2) are replaced by their value by the compiler, and the value of a constant expression that has a constant in it (like PI * 2 again) is hard to decompose back into the expression.

@KvanTTT
Copy link
Author

KvanTTT commented Nov 27, 2014

@PoroCYon, I mean small part of most used combinations which can be added or calculated manually.

I found following Math.PI combinations using with github "Advanced search".

So, taking above result into consideration, I suppose to calculate pi coefficient by following way: c = Math.PI / constant. If we getting "good" value (equal exactly to 1.0, 2.0, 0.5, 1/180 and so on), we simply replacing it with symbolic expression (Math.PI, Math.PI * 2, Math.PI / 2, Math.PI / 180 and so on).

Also descried above algorithm should be applied for constant Math.E.

I'll try to implement this feature.

@KvanTTT
Copy link
Author

KvanTTT commented Dec 2, 2014

I analyzed most popular .net decompilers (JustDecompile, dotPeek, .NET Reflector) and found that only dotPeek supports this feature.

@dee-see
Copy link

dee-see commented Dec 29, 2014

@KvanTTT Did you notice if dotPeek really decompiled that or if it was using Microsoft's PDB servers (which is a built-in feature of dotPeek)?

@PoroCYon
Copy link

@dee-see dotPeek actually does do this.
Consider the following code:

int i = 255;

dotPeek (unlike others, obviously) decompiled it as:

int i = byte.MaxValue;

@siegfriedpammer siegfriedpammer added this to the 3.0 milestone Nov 12, 2017
@siegfriedpammer siegfriedpammer added C# Decompiler The decompiler engine itself New Feature labels Nov 12, 2017
@siegfriedpammer siegfriedpammer self-assigned this Nov 12, 2017
@siegfriedpammer siegfriedpammer removed this from the 3.0 milestone Nov 25, 2017
@siegfriedpammer siegfriedpammer added this to the 4.0rc1 milestone Dec 19, 2018
siegfriedpammer added a commit that referenced this issue Dec 21, 2018
…sions containing Math.PI and Math.E as factors.
siegfriedpammer added a commit that referenced this issue Jan 4, 2019
…tty-print

Pretty print floating point literals; fix #517; fix #1314
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 10, 2020
ElektroKill added a commit to dnSpyEx/ILSpy that referenced this issue Jul 13, 2021
…iterals-pretty-print

Pretty print floating point literals; fix icsharpcode#517; fix icsharpcode#1314
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C# Decompiler The decompiler engine itself New Feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants