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

Precision problems in primitive datatypes due to Javascript #12

Open
pedrobranco opened this issue Nov 3, 2016 · 9 comments
Open

Precision problems in primitive datatypes due to Javascript #12

pedrobranco opened this issue Nov 3, 2016 · 9 comments

Comments

@pedrobranco
Copy link

As of the XSD specification allows that primitive datatypes Decimal, Double, Float and Integer to have a precision with more than 15 significant digits (example is via totalDigits).

When we are using parseFloat here / here and parseInt here makes the parsed values lose precision (javascript does not support float/number representation with more than 15 digits).

@mitar
Copy link
Member

mitar commented Nov 3, 2016

True, but that is a problem of JSON representation in general, no? There is no standard way to represent more precise numbers.

@pedrobranco
Copy link
Author

True, but that is a problem of JSON representation in general, no?

It is not a JSON problem, but a Javascript problem.

There is no standard way to represent more precise numbers.

A lot of people use some modules to represent big numbers such as bignumber.js, but in this case, IMO we should return the value as string in every case that Javascript precision cannot represent the respective value.

@mitar
Copy link
Member

mitar commented Nov 4, 2016

So you are saying that we should check if totalDigits exists and if that is larger than 15, we should just keep it as a string?

Feel free to make a pull request for this.

@pedrobranco
Copy link
Author

pedrobranco commented Nov 7, 2016

So you are saying that we should check if totalDigits exists and if that is larger than 15, we should just keep it as a string?

Instead of checking totalDigits we should just check if the parsed float / number loses precision comparing to the original value (there are some simple libs that already check this), and if so we should return it as string, or maybe passing via options custom handlers for parseFloat / parseInt. WDYT?

Feel free to make a pull request for this.

Thanks, when possible I will submit a PR.

@mitar
Copy link
Member

mitar commented Nov 7, 2016

No, I do not like this approach. The main reason why I made this package was because I wanted that resulting JSON has the same schema always. Not that I have to check if once something is an array, and something not, because xml2js made it not an array because there was only one element.

In this way I would not like that something is a number and in another document from the same collection of inputs it is a string.

So I would be more for approach that if schema is saying that there can be many digits, then we always make strings. Users can change their schema if they want less precision.

@mitar
Copy link
Member

mitar commented Nov 7, 2016

or maybe passing via options custom handlers for parseFloat / parseInt

We could do something like this as a workaround. But probably it can also be done manually in a postprocessing step?

@pedrobranco
Copy link
Author

We could do something like this as a workaround.

Honestly, I would prefer passing the integerParser and floatParser functions via options with the default functions parseInt and parseFloat.

But probably it can also be done manually in a postprocessing step?

After the value is parsed with parseInt and parseFloat it cannot be recovered to its original state.

@mitar
Copy link
Member

mitar commented Nov 8, 2016

After the value is parsed with parseInt and parseFloat it cannot be recovered to its original state.

No, I mean, if there is totalDigits which are larger than what JSON can represent with float, then we keep it as a string. And if you want to have this processing of some values to float anyway, then you can postprocess the resulting object manually, instead of having to provide ways to hook into this step here.

@swftvsn
Copy link

swftvsn commented Oct 1, 2018

I would say that custom integer / float parsers are more than welcome, so we can plug in our own implementations, like big.js or similar. It is really confusing if the data type can vary, like be a number or string. It would be better if it is only one type. (And provide the option to use custom parsers.)

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

No branches or pull requests

3 participants