[SERVER-20975] Aggregation $sum has some odd rounding erorrs. Created: 16/Oct/15  Updated: 17/Oct/15  Resolved: 16/Oct/15

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 2.6.10, 3.2.0-rc0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: John Page Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

db.b.insert({a:1.1})
db.b.insert({a:0.1})
db.b.aggregate({$group:{_id:true,t:{$sum:"$a"}}})

Participants:

 Description   

If I use $sum to add some simple floating point numbers together in MongoDB 2.6 or 3.2 I see really odd rounding.

db.b.insert({a:1.1})
db.b.insert({a:0.1})
db.b.aggregate({$group:{_id:true,t:{$sum:"$a"}}})
{ "_id" : true, "t" : 1.2000000000000002 }

Same applies to 3.3 + 3.9 = 7.199999999999999



 Comments   
Comment by Charlie Swanson [ 16/Oct/15 ]

I don't think there's anything reasonable we can do about this. In the future, the new Decimal data type could be used if you wanted to represent decimals precisely.

Comment by Adam Midvidy [ 16/Oct/15 ]

This seems like expected IEEE-754 behavior: e.g. 3.3 + 3.9 will give you 7.19999999999 ... in Python or any other environment that uses binary floats.

Generated at Thu Feb 08 03:55:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.