[SERVER-29679] $inc generating some wrong value when incremented with negative decimal value Created: 16/Jun/17  Updated: 27/Oct/23  Resolved: 16/Jun/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.4.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Suraj Sawant Assignee: Mark Agarunov
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
Operating System: ALL
Steps To Reproduce:

1.Insert

db.test.insert({"qty":4.464})

2.check results

db.test.find()

3.increment the value with negative fractional value

db.test.update({},{$inc:{"qty":-0.608}})

4.check the output

db.test.find()

Actual Output: "qty" : 3.8560000000000003
Expected Output: "qty" : 3.856

Participants:

 Description   

While using $inc to decrement a value by some fractional value,ganerated output differs with expected output.



 Comments   
Comment by Mark Agarunov [ 16/Jun/17 ]

Hello sawantsuraj91@gmail.com,

Thank you for the report. What you are seeing appears to relative error be due to IEEE-754 Floating point math. Due to the way floating point numbers are represented, the actual values being added are binary approximations of the numbers inserted, which is why the output is not the exact expected value. Unfortunately this is an unavoidable property of floating point arithmetic.

Thanks,
Mark

Generated at Thu Feb 08 04:21:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.