couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jens Alfke <>
Subject Re: Fixed precision of floating point number not respected in views
Date Tue, 19 Feb 2013 17:33:31 GMT

On Feb 19, 2013, at 1:09 AM, Robert Newson <> wrote:

> If you want fixed precision, you'll need to store your numbers in
> strings and manipulate them that way too. A quick google in the past
> has shown a few "bignum" libraries for Javascript.

Another alternative, if you need a certain number of decimal digits of precision, is to premultiply
the numbers by a fixed power of ten. For example, it looks like the OP may need five digits
after the decimal point, so multiplying the input values by 10^5 would turn them into integers.
Then the value 151.17281 would be stored as 15117281, which of course can be represented precisely
in 64-bit floating point.

(The limitation here is that since 64-bit doubles have a 56-bit mantissa (IIRC), the largest
integer that can be represented precisely is about 10^16, corresponding to a pre-scaled input
value of 10^11.)

The obvious advantage over string encoding is that you can add, subtract and compare numbers
without having to do any conversion, and even if you do have to convert numbers (for multiplication,
say) the conversion is very fast compared to string<->number conversions.

As for alternative data formats, there are a lot of them around, and I don’t see that EDN
would have an advantage over any of the others that are better known and more widely adopted,
like YAML. Changing away from JSON would be a compatibility nightmare, anyway (speaking as
the author of a Couch-compatible replication engine.)

View raw message