Discussion:
Does $inc round numbers?
Mitch Pirtle
2010-09-10 15:42:08 UTC
Permalink
Hi fellow Mongonians,

I had a document with a value that looked like this:

"value" : 57.519999999999996

I did an increment of 30 like this:

db.thingies.update({"_id":"74795"}, {$inc : {"value" : 30}})

And then the resulting value was this:

"value" : 87.52

Although this was handy as I wanted to do that myself, I find it a tad
disconcerting to have the database do that for me. This was done in
the shell BTW.

Is that standard behavior for $inc, or do I need to file a tracker item?

-- Mitch
Joshua Kehn
2010-09-10 15:44:29 UTC
Permalink
Mitch-

I'm not sure if it is the standard behaviour, but I do know that JavaScript engines have had some funny rounding issues.

One thing to note is that the value might have been obtained by saying 115.04 / 2 -> store, which might lead to a slight inaccuracy in the displaying value. When you increment it it fixes itself.

Regards,

-Josh
____________________________________
Joshua Kehn | Josh.Kehn-***@public.gmane.org
http://joshuakehn.com
Post by Mitch Pirtle
Hi fellow Mongonians,
"value" : 57.519999999999996
db.thingies.update({"_id":"74795"}, {$inc : {"value" : 30}})
"value" : 87.52
Although this was handy as I wanted to do that myself, I find it a tad
disconcerting to have the database do that for me. This was done in
the shell BTW.
Is that standard behavior for $inc, or do I need to file a tracker item?
-- Mitch
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
Michael Dirolf
2010-09-10 15:44:12 UTC
Permalink
We use IEEE 754 floating point numbers (like basically every
programming language). What you're seeing is the normal rounding error
involved w/ floats, not the database doing anything weird. There's an
entry on this in the PyMongo FAQ:
http://api.mongodb.org/python/1.8.1%2B/faq.html#i-m-saving-9-99-but-when-i-query-my-document-contains-9-9900000000000002-what-s-going-on-here
Post by Mitch Pirtle
Hi fellow Mongonians,
   "value" : 57.519999999999996
  db.thingies.update({"_id":"74795"}, {$inc : {"value" : 30}})
 "value" : 87.52
Although this was handy as I wanted to do that myself, I find it a tad
disconcerting to have the database do that for me. This was done in
the shell BTW.
Is that standard behavior for $inc, or do I need to file a tracker item?
-- Mitch
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
Mitch Pirtle
2010-09-10 16:22:57 UTC
Permalink
Ok, thanks for the explanation everyone! Just needed to know if it was
a personal problem or not :-)

-- Mitch
Post by Michael Dirolf
We use IEEE 754 floating point numbers (like basically every
programming language). What you're seeing is the normal rounding error
involved w/ floats, not the database doing anything weird. There's an
http://api.mongodb.org/python/1.8.1%2B/faq.html#i-m-saving-9-99-but-when-i-query-my-document-contains-9-9900000000000002-what-s-going-on-here
Post by Mitch Pirtle
Hi fellow Mongonians,
   "value" : 57.519999999999996
  db.thingies.update({"_id":"74795"}, {$inc : {"value" : 30}})
 "value" : 87.52
Although this was handy as I wanted to do that myself, I find it a tad
disconcerting to have the database do that for me. This was done in
the shell BTW.
Is that standard behavior for $inc, or do I need to file a tracker item?
-- Mitch
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to mongodb-user+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
Continue reading on narkive:
Loading...