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

Implement Number.prototype.toFixed() #35

Merged
merged 20 commits into from
Oct 3, 2023
Merged

Implement Number.prototype.toFixed() #35

merged 20 commits into from
Oct 3, 2023

Conversation

HalidOdat
Copy link
Member

@HalidOdat HalidOdat commented May 4, 2023

This Pull Request fixes/closes #32.

This PR implements a WIP implementation of Number.prototype.toFixed(). This is a port of the official https://github.com/ulfjack/ryu/ repository's printf %f implementation, modified to comply to the Number.prototype.toFixed() behaviour.

Changes:

  • Initial draft implementation of Number.prototype.toFixed()
  • Added some tests

TODO:

  • Figure out the max for Buffer size.
  • Shrink the lookup tables, they are too big
    • Partially generate them, and compute some at runtime. The tables after shrinking them down with the method below. I think they are small enough now, I think Ill leave this for alter implementing other float to string conversions Implement Number.prototype.toExponential() #31 and Implement Number.prototype.toPrecision() #33
    • We probably only need a subset of the tables, because anything greater than 1e21 uses the default format and precision is clamped [0, 100]. We might be able to trim it.
      • Reduce POW10_SPLIT from 1224 to 8 (29184 bytes) ~99.35% reduction
      • Reduce POW10_SPLIT_2from 3133 to 481 (63648 bytes) ~84.65% reduction
  • Add a bunch of tests
  • Put under a feature flag

@HalidOdat HalidOdat added the enhancement New feature or request label May 4, 2023
@HalidOdat HalidOdat marked this pull request as draft May 4, 2023 20:21
Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, I added some comments :)

tests/to_fixed.rs Outdated Show resolved Hide resolved
src/pretty/to_fixed/mod.rs Outdated Show resolved Hide resolved
src/pretty/to_fixed/mod.rs Outdated Show resolved Hide resolved
src/pretty/to_fixed/mod.rs Outdated Show resolved Hide resolved
src/pretty/to_fixed/mod.rs Outdated Show resolved Hide resolved
src/pretty/to_fixed/mod.rs Show resolved Hide resolved
@HalidOdat HalidOdat added this to the v1.0.0 milestone May 8, 2023
@HalidOdat HalidOdat requested a review from a team May 11, 2023 01:20
@HalidOdat
Copy link
Member Author

After a lot of testing this is ready for review/merge 🎉

@HalidOdat HalidOdat marked this pull request as ready for review May 11, 2023 02:42
Copy link
Member

@raskad raskad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can see this look good. I'm not gonna pretend I understand all of the math behind this, but the code looks understandable to me.

src/pretty/to_fixed/mod.rs Outdated Show resolved Hide resolved
src/pretty/to_fixed/mod.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
Copy link
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just added some missing newlines that I'll commit with the reviews.

.vscode/launch.json Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@jedel1043
Copy link
Member

Oh, we didn't configure the queue for this repo. Let me open a PR for it.

@jedel1043 jedel1043 added this pull request to the merge queue Oct 3, 2023
Merged via the queue into main with commit 2a6fc89 Oct 3, 2023
9 checks passed
@jedel1043 jedel1043 deleted the feature/to-fixed branch October 3, 2023 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Number.prototype.toFixed()
4 participants