Double and long serialization in java

I can save the number as long and double in HBase. Both of them accept 8 bytes in Java.

The advantage of using Double is that it provides a wider range for storing integers.

However, I think the Long range is also sufficient for my use.

Does anyone have any idea of ​​the performance of serializing and de-serializing Long vs Dobule? I'm interested in comparing them.

Thanks.

+4
source share
3 answers

If you are storing integers, use Long . Your claim that β€œThe advantage of using Double is that it provides a wider range for storing integers” is incorrect. Both are 64 bits long, but double should use some bits for the exponent, leaving fewer bits to represent the value. You can store large numbers in double , but you will lose precision.

In other words, for numbers larger than some upper bound, you can no longer store adjacent "integers" ... given an integer value above this threshold, the "next" possible double will be more than 1 more than the previous number.

for instance

 public class Test1 { public static void main(String[] args) throws Exception { long long1 = Long.MAX_VALUE - 100L; double dbl1 = long1; long long2 = long1+1; double dbl2 = dbl1+1; double dbl3 = dbl2+Math.ulp(dbl2); System.out.printf("%d %d\n%f %f %f", long1, long2, dbl1, dbl2, dbl3); } } 

It is output:

 9223372036854775707 9223372036854775708 9223372036854776000.000000 9223372036854776000.000000 9223372036854778000.000000 

note that

  • The double representation of Long.MAX_VALUE-100 makes NOT equal to the original value.
  • Adding 1 to the double view of Long.MAX_VALUE-100 has no effect
  • With this value, the difference between one double and the next possible double value is 2000.

Another way of saying this is that Long has an accuracy of up to 19 digits, and double has only 16-digit accuracy. A double can store numbers exceeding 16 digits, but due to truncation / rounding in the lower digits.

If you need accuracy over 19 digits, you should resort to BigInteger with the expected performance degradation.

+14
source

This seems like a wrong battle:

From Java Tutorial

The long data type is a 64-bit signed integer from two additions. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

This is pretty close to 19 significant digits

From Wikipedia

This gives 15 to 17 significant decimal digits.

Thus, despite the obvious "superiority", Double will serve you worse than Long. And I’m just guessing here, but intuitively I would say that serializing / deserializing floating point types are more expensive operations than the same operations on whole data types, but even if there are differences, they will be quite small in modern systems.

So, when working with integers, stick to Long.

+2
source

Without knowing specifically, I would suggest that both long and a double have the same serialization: take 64 bits and put them on the wire. Similarly, I would suggest that deserialization is just a matter of removing 64-bit wires and declaring that they are now long or double . Any 64 bits will represent a valid long or double (although not all will be a finite double), so there is no verification or extra work.

0
source

Source: https://habr.com/ru/post/1437481/


All Articles