I can store a number as a Long and Double in HBase. Both of them takes 8 bytes in Java.
Advantage of Using Double is that it gives a more wider range for storing Whole Numbers.
However, i think range of Long is also enough for my use.
Does anyone has any idea about the serialization and de-serialization performance of Long vs Dobule? I am interested in comparison between them.
Thanks.
The main difference between long and double in Java is that long is a data type that stores 64 bit two's complement integer while double is a data type that stores double prevision 64 bit IEEE 754 floating point. In brief, long is an integral type whereas double is a floating point type.
In an article on MSDN, it states that the double data type has a range of "-1.79769313486232e308 .. 1.79769313486232e308". Whereas the long data type only has a range of "-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807".
Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number. Both float and double use 1 bit for representing the sign of the number.
It is because SerialVersionUID is used to ensure that during deserialization the same class (that was used during serialize process) is loaded.
If you are storing integers, use Long
. Your statement that "Advantage of Using Double is that it gives a more wider range for storing Whole Numbers" is incorrect. Both are 64 bits long, but double
has to use some bits for the exponent, leaving fewer bits to represent the magnitude. You can store larger numbers in a double
but you will lose precision.
In other words, for numbers larger than some upper bound you can no longer store adjacent "whole numbers"... given an integer value above this threshold, the "next" possible double
will be more than 1 greater than the previous number.
For example
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);
}
}
This outputs:
9223372036854775707 9223372036854775708
9223372036854776000.000000 9223372036854776000.000000 9223372036854778000.000000
Note that
Another way of saying this is that long
has just under 19 digits precision, while double
has only 16 digits precision. Double can store numbers larger than 16 digits, but at the cost of truncation/rounding in the low-order digits.
If you need more than 19 digits precision you must resort to BigInteger
, with the expected decrease in performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With