Skip to content

Add an overide for the increment function to allow user to specify siginificant digits #7736

Open
@switch201

Description

@switch201

New Feature / Enhancement Checklist

  • I am not disclosing a vulnerability.
    I am not just asking a question.
    I have searched through existing issues.

Current Limitation

When using the parseObj.increment function, there is no way to account for floating point arithmatic errors. For example, if in the database I have a Quantity Column, and it has a value of 6.5 and I want to call `someObj.increment("Quantity", -4.1) instead of 2.4 being saved to that column 2.4000000000000004 is saved instead. From what I can tell there is no way to use the increment function and have it account for this. you have to do the math yourself and use set, which is not an option for us in thie case

Feature / Enhancement Description

make an override to the increment function that takes an additional parameter specifying the number of significant digits

Example Use Case

if in the database I have a Quantity Column, and it has a value of 6.5 and I want to call `someObj.increment("Quantity", -4.1) instead of 2.4 being saved to that column 2.4000000000000004 is saved instead. From what I can tell there is no way to use the increment function and have it account for this. you have to do the math yourself and use set, which is not an option for us in thie case

Alternatives / Workarounds

Using Set and the save would work, but given this column sees alot of traffic at once, it raises concerns of a race condition happening which can lead to an inacurate count which is why we use the increment function rather than set and save.

3rd Party References

Activity

parse-github-assistant

parse-github-assistant commented on Dec 6, 2021

@parse-github-assistant

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!
mtrezza

mtrezza commented on Dec 6, 2021

@mtrezza
Member

2.4000000000000004 is saved

I am not sure that this is something to be addressed in Parse Server. The increment operator is executed by the database engine and therefore also the calculation is done there. At least for MongoDB, I don't think the $inc op supports a "significant digit" argument - and why should it since it's not a multiplication (or division).

In any case, I suspect a different issue in your case, because a +- op should result in exactly 2.4 in your example, it's a simple binary subtraction. I suspect either the original value was not exactly 6.5 or the increment argument was not exactly -4.1.

switch201

switch201 commented on Dec 7, 2021

@switch201
Author

@mtrezza hit F12 and go to your console and type 6.5-4.1 and you will see.
image

At the base level its a JavaScript problem. and when I look into the implementation increment, it appears to call the IncrementOp function and going further down that simply does the addition/subtraction without rounding.

image

image

image

the issue is where I circled in the last screen shot. if running node js application, when this._amount = 6.5 and value is -4.1 then it will result with 2.400000000000004 being saved.

When using set and save we do our own rounding to avoid this, but in this case we need to use increment which is why we are stuck.

switch201

switch201 commented on Dec 7, 2021

@switch201
Author

I should point out thats not the only number combination that results in erroneous remainder. again this is 100% a limitation of javascript, but because of that limitation there should be an option to allow the user to round to a specified number.

mtrezza

mtrezza commented on Dec 7, 2021

@mtrezza
Member

make an override to the increment function that takes an additional parameter specifying the number of significant digits

A user-defined rounding parameter comes with potential issues. The priority needs to be data consistency between the Parse Object and the DB value. Whatever floating point precision is used for atomic operations on the DB side should be mirrored on the Parse Object side. If the value calculated by the DB engine and stored in the DB is 2.4000000000000004, then the same value should be set for the Parse Object, because the DB value is the single point of truth, apparent when you fetch the object from the DB.

This may depend on the DB engine (MongoDB, Postgres, etc) and even the engine version. From that it seems that it should be a database adapter config, or if necessary on the client side a Parse client SDK initialization parameter. Since the calculation occurs on the client side (Parse JS SDK), we will probably have to dig a bit deeper into this for a solution. I guess the first step would be to research the used floating point precision for increment operations in MongoDB and Postgres.

See the MongoDB docs for modeling monetary data as an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:questionSupport or code-level question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mtrezza@switch201

        Issue actions

          Add an overide for the increment function to allow user to specify siginificant digits · Issue #7736 · parse-community/parse-server