0
votes

As an academic exercise, I am trying to achieve an arbitrary precision (call it AP) system in google app engine using python and the NDB storage. I know that there are AP libraries out there so we can skip that.

I am curious how one might store the numbers from an AP library in a way that they could be query-able with greater than, less than, and equal operators. Ideally, this could be done using a custom property class within the NDB model, but since the NDB model uses Python and Python does not support AP numbers natively, I am not sure this is possible. If you guys agree, then we can move on to talking about storage generally - SQL, MongoDB, NDB, etc.

As I understand them, AP systems often separate the AP number into coefficients (in an array?), an exponent, and a sign. If we stored these, is there a combination of filters on these fields that could achieve a greater than, less than, and equals operations? Is it even possible to store the coefficient if it is indeed stored as an array?

Thanks in advance!

2

2 Answers

0
votes

Here is one possible solution. First off, I need to use the Decimal object, no AP library needed it sounds like. To store it, I could convert it to a string using one of the class's methods. For instance, get a tuple from it, and convert to a string. Then, I could store that string in a NDB TextProperty and use an additional ComputedProperty to convert that string to Decimal.

However, I am not really sure how that ComputedProperty would be stored in NDB and if it would really be AP and thus useful to query against. The docs say "The computed value is written to the Datastore so that it can be queried...". But is it stored as an actual decimal? Or an AP number?

0
votes

You need to use a sub-classed property: https://cloud.google.com/appengine/docs/python/ndb/subclassprop

In your case, either as a string (then cast it back to a Decimal in your _from_base_type) or as two integers (an integer representing the whole number, and the dividing exponent to return the number (e.g. 0.123 as 123 and 3 as 123/10^3 == 0.123).

A ComputedProperty cannot store data types outside the normal ones.