3

In MongoDB, I can do the following to represent infinity:

db.foo.insert({'max': Number.POSITIVE_INFINITY});

How can I store Infinity in a Document defined like this:

class Foo(Document):
    min = DecimalField()
    max = DecimalField()
    price = DecimalField()

The following fails:

foo = Foo(min=100, max=float("inf"))

With:

InvalidOperation: quantize with one INF

@BlakesSeven recommended the FloatField instead, which I verified that it works. However, I'm using this field for financial purposes, so I'm not sure how safe it is.

Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
  • Interesting. What about a [FloatField](http://docs.mongoengine.org/apireference.html#mongoengine.fields.FloatField)? Same thing? – Blakes Seven Feb 14 '16 at 11:26
  • @BlakesSeven It actually works for `FloatField`. I am however using this field to store currency values, would that have any negative effect? – Matthew Moisen Feb 15 '16 at 00:17
  • Well see that is not what sort of value "Infinity" is, which is why I put it to you like that. `DecimalField` may well have a `"."` in it, but it really isn't a "Float". "Infinity" is a floating point number. Hence the distinction. If you expect to store both, then you are going to need some other layer for presentation. Not that using floating point numbers for currency is a very good idea. IMHO. – Blakes Seven Feb 15 '16 at 02:22
  • @BlakesSeven Pardon, I'm not actually storing a currency value here; I meant to say I'm using this in a financial application. If the quantity is > `min` and <= `max`, then multiply the `price` by the quantity. For example, `(0, 100, $10)`, and `(100, INF, $8)`. Although I doubt a user would specify 100.123456789 for either `min` or `max`, in theory they could, which is why I would hesitate to use a `FloatField`. – Matthew Moisen Feb 15 '16 at 02:36
  • Infinity is a float. That much should be clear. If you don't want a float then use a "ridiculously large number" instead. – Blakes Seven Feb 15 '16 at 02:45
  • @BlakesSeven Would you like to post that as an answer? It seems like the best alternative. – Matthew Moisen Feb 15 '16 at 06:30
  • using the largest system float seems reasonable as here: http://stackoverflow.com/a/3477332/3271558 – Steve Rossiter Feb 15 '16 at 17:51

0 Answers0