8000 Add NumberFormatStrategy for Floating Points. Add option for decimal Encoding/Decoding by SpectralDragon · Pull Request #452 · jpsim/Yams · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add NumberFormatStrategy for Floating Points. Add option for decimal Encoding/Decoding #452

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

SpectralDragon
Copy link
@SpectralDragon SpectralDragon commented May 26, 2025

Closes #279

I modified PR by zeionara - #374

For my project https://github.com/AdaEngine/AdaEngine needs a human readable Floating Points values and I want to fix exponential values in yaml files.

I've add a tests for Float and Double, and I hope we merge this request.

Thank you for your project!

Copy link
Collaborator
@lynchsft lynchsft left a comment

Choose a reason for hiding this comment

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

I think this enhancement would be much more useful, much more future-forward and much easier to maintain if we simply introduced an optional NumberFormatter (Foundation) to the Emitter Options.

@SpectralDragon
Copy link
Author
SpectralDragon commented May 27, 2025

@lynchsft

Not a problem, what do you think about maximumFractionDigitsNumber for Float/Double?

I think, I can take JSONDecoder behavior for that..

I will be back soon with a new commit.

@SpectralDragon
Copy link
Author

Unfortunately, I stuck with problem to formatting FloatingPoint.greatestFiniteMagnitude, because we should to use exponential value here, but NSNumberFormatter can't be configurated in this way.

I look into JSONEncoder and noticed that they use description property thats works as decimal converter with exponential values support.

If value is greatestFiniteMagnitude -> use exponential, otherwise use human readable decimal.

I update Represented.swift that will use that new rule and it's looks more readable and maintainable than my previous commits.

What do you think?

@SpectralDragon SpectralDragon requested a review from lynchsft May 27, 2025 10:49
sequenceStyle: encoder.sequenceStyle,
mappingStyle: encoder.mappingStyle,
newlineScalarStyle: encoder.newlineScalarStyle)
options: encoder.options)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes to passing the options struct. 👏

@@ -62,40 +62,40 @@ private func represent(_ value: Any) throws -> Node {
/// Type is representable as `Node.scalar`.
public protocol ScalarRepresentable: NodeRepresentable {
/// This value's `Node.scalar` representation.
func represented() -> Node.Scalar
func represented(options: Emitter.Options) -> Node.Scalar
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think that changing the ScalarRepresentable protocol is called for. It's a breaking change.
Isn't there another way?

Copy link
Author

Choose a reason for hiding this comment

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

Hmmm, I will double check this.

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.

Doubles end up in exponential notation
2 participants
0