Skip to content

Improve floating-point fraction and negative constant reconstruction - #4022

Open
SychicBoy wants to merge 2 commits into
icsharpcode:masterfrom
SychicBoy:fix-1459-floating-point-constants
Open

Improve floating-point fraction and negative constant reconstruction#4022
SychicBoy wants to merge 2 commits into
icsharpcode:masterfrom
SychicBoy:fix-1459-floating-point-constants

Conversation

@SychicBoy

Copy link
Copy Markdown

Fixes #1459

Summary

Improve floating-point constant reconstruction by preferring exact, recognizable normalization fractions where appropriate, while preserving simpler existing representations.

This also extends well-known constant detection to recognize negated constants such as -float.Epsilon and -double.Epsilon.

Details

Some normalized floating-point values are currently reconstructed into less recognizable forms. For example:

200f / 255f

may become:

40f / 51f

Although equivalent, the /255f representation better communicates common byte-normalization patterns.

This change adds a restricted set of preferred denominators based on powers of two and 2^n - 1 scales. Candidates are only selected when they reproduce the exact original float or double value.

The existing fraction approximation logic remains unchanged, and simpler fractions are preserved rather than expanded into larger equivalent representations.

Negative well-known constants are also recognized, allowing values such as:

-float.Epsilon
-double.Epsilon

to be emitted symbolically instead of as numeric literals.

Examples

Before:

0.88235295f
40f / 51f
0.5882353f

-1E-45f
-5E-324

After:

225f / 255f
200f / 255f
150f / 255f

-float.Epsilon
-double.Epsilon

Implementation notes

  • Preserves exact float and double values and existing simpler fraction representations.
  • Keeps the existing fraction approximation limits unchanged.
  • Restricts preferred fractions to a small set of common normalization scales.
  • Keeps existing canonical special constants authoritative.

Tests

Regression coverage has been added to the existing WellKnownConstants Pretty test fixture for:

  • /255f byte-normalized values
  • negative normalized values
  • double fractions
  • power-of-two K/M-scale denominators
  • preservation of simpler fractions
  • -float.Epsilon
  • -double.Epsilon

The added cases verify both the new behavior and that less-readable equivalent representations are not introduced.

@siegfriedpammer

Copy link
Copy Markdown
Member

This PR looks like it changes a lot of stuff... can you split all the changes into multiple commits, so that there is one small commit per change? Thanks!

@christophwille

Copy link
Copy Markdown
Member

https://github.com/icsharpcode/ILSpy/blob/master/CONTRIBUTING.md that awfully looks like a drive-by AI PR. Could you please elaborate why you picked that specific issue and given your Github timeline, how experienced you are with the internal of our decompiler engine? Letting your clanker implement an issue and post a PR is not how we work with contributors.

@SychicBoy

SychicBoy commented Aug 16, 2026

Copy link
Copy Markdown
Author

https://github.com/icsharpcode/ILSpy/blob/master/CONTRIBUTING.md that awfully looks like a drive-by AI PR. Could you please elaborate why you picked that specific issue and given your Github timeline, how experienced you are with the internal of our decompiler engine? Letting your clanker implement an issue and post a PR is not how we work with contributors.

Hi Christoph,

No, this is not a “drive-by AI PR.” English is not my native language, so I sometimes use AI to help me phrase my messages more clearly and in a more formal tone. That may be why the wording came across that way.

To clarify my approach to this PR:

I picked #1459 because it was marked Help Wanted and looked like something I could solve.

I am still new to ILSpy’s internals, but I have made an effort to understand the relevant code paths related to this change. I’m open to technical discussion and feedback on the implementation.

I appreciate maintainers being careful about contributions, and I hope this PR can be evaluated based on the actual code and discussion around it.

@SychicBoy
SychicBoy force-pushed the fix-1459-floating-point-constants branch from c3358db to 59361d9 Compare August 16, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFE: Fraction decompilation - more weight to common divisors?

3 participants