Hi all, hoping for some help here. Dug through the couchbase documentation, stackoverflow, etc, still unclear about what are the limitations of storing numbers in couchbase. My main concern is storing decimals (3 decimal precision to be precise ).
I expected the storage to be some 64bit floating number, so I thought would hit the same limitations of doubles in Java. Basically both Javascript numbers and Java doubles follow (if my googling serves me) IEEE 754. My problem is that Couchbase somehow seems to store 0.99 precisely while Java canât represent the double well.
Long story short.
POJO:
@ToString
class BD {
public BigDecimal bdString = new BigDecimal(â0.99â);
public BigDecimal bdDouble = new BigDecimal(0.99);
public int i = 123124;
public String boo = âbooâ;
}
Code to write and read in Couchbase:
BD bdWrite = new BD(); String json = mapper.writeValueAsString(bdWrite); BigDecimal bdWriteString = bdWrite.bdString; BigDecimal bdWriteDouble = bdWrite.bdDouble; JsonDocument jsonDoc = JsonDocument.create("bigdecimal::1", JsonObject.fromJson(json)); bucket.upsert(jsonDoc); System.out.println("bdWrite: " + bdWrite); String id = "bigdecimal::1"; JsonDocument doc = bucket.get(id); BD bdRead = mapper.readValue(doc.content().toString(), BD.class); BigDecimal bdReadString = bdRead.bdString; BigDecimal bdReadDouble = bdRead.bdDouble; System.out.println("bdRead: " + bdRead); System.out.println("bdWrite: " + bdWrite + ", bdRead:" + bdRead + ", equal:" + bdWrite.bdDouble.equals(bdRead.bdDouble));
The doc in couchbase:
{
âiâ: 123124,
âbooâ: âbooâ,
âbdDoubleâ: 0.99,
âbdStringâ: 0.99
}
And the console output:
bdWrite: BD(bdString=0.99, bdDouble=0.9899999999999999911182158029987476766109466552734375, i=123124, boo=boo)
bdRead: BD(bdString=0.99, bdDouble=0.99, i=123124, boo=boo)
bdWrite: BD(bdString=0.99, bdDouble=0.9899999999999999911182158029987476766109466552734375, i=123124, boo=boo), bdRead:BD(bdString=0.99, bdDouble=0.99, i=123124, boo=boo), equal:false
Basically both the BigDecimal from double (immediately loss of precision) and the BigDecimal from String (no loss of precision) seem to be stored as 0.99 in Couchbase, and both are read back as 0.99. If Couchbase stores numbers as doubles in Java, should have seen a loss of precision in the storage (console) and reading back in BigDecimal. Instead it looks like some rounding is going onâŚ
Anyone can shed some light on whatâs going on? How come from the bdDouble=0.9899999999999999911182158029987476766109466552734375, couchbase stores 0.99 and returns it as such?
thanks.