Collection has only one document :
{
"_id" : 1.0,
"data" : [10.42, 40.07, 98.565, 69.8]
}
Make and run such aggregate:
db.test_data.aggregate(
[
{ "$addFields" : {
"sum1" : { "$sum" : "$data" },
"sum2" : {
"$reduce" : {
"input" : "$data",
"initialValue" : 0.0,
"in" : { "$add" : ["$$value","$$this"]}
}
}
}
}
]
);
Results:
{
"_id" : 1.0,
"data" : [
10.42,
40.07,
98.565,
69.8
],
"sum1" : 218.855,
"sum2" : 218.85500000000002
}
It is clear that the internal representation of floating numbers can result in sum2,
but why are the results different at all?
Comments?