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

Add support for Float #5

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

Add support for Float #5

wants to merge 6 commits into from

Conversation

tomijaga
Copy link

Additions

  • Added #Floatto the JSON variant
  • Added a parser to convert text to Float
  • Added tests for show() and parse()

src/JSON.mo Outdated
@@ -23,6 +25,7 @@ module JSON {

public func show(json : JSON) : Text = switch (json) {
case (#Number(v)) { Int.toText(v); };
case (#Float(v)) { Float.format(#fix 2, v); };
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not lose precision in the textual representation.

Copy link
Author

Choose a reason for hiding this comment

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

I updated the code so it returns the exact format.

@tomijaga
Copy link
Author

tomijaga commented Dec 26, 2022

Floats parsed from the text representation do not match the Floats defined in Motoko, but they are nearly equal.

1.234e-4 != 1.234e-4

I will try to update the parser to use this lib to see if I get better results

@tomijaga
Copy link
Author

Also, I tried to implement the number fraction exponent in this #3, but I think there will be some confusion because motoko only recognizes the format as a Float.
Screen Shot 2022-12-25 at 1 51 47 PM

Copy link
Contributor

@q-uint q-uint left a comment

Choose a reason for hiding this comment

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

Hi @tomijaga, thanks for the changes!

I would like to keep the dependencies to a minimum, so the best option might be to revert to the original truncated floats...

test/Show.mo Outdated
Comment on lines 10 to 12
assert (JSON.show(#Float(-3.14)) == "-3.1400000000000001");
assert (JSON.show(#Float(1.234e-4)) == "0.00012339999999999999");
assert (JSON.show(#Float(43e-02)) == "0.42999999999999999");
Copy link
Contributor

Choose a reason for hiding this comment

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

Motoko does not seem to work well with precision.

Copy link
Contributor

@q-uint q-uint left a comment

Choose a reason for hiding this comment

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

Can you mention that a Float only has two decimals somewhere in the README?

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.

2 participants