I have a method that expects a double, and I want to store the UTC time and my variable that I am passing to this method is a long.
I am using:
Date now = new Date();
now.getTime()
to get the UTC time.
Can this value not fit into a double?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It will “fit”, in the sense that
doublecan represent anylongvalue, but there would be a loss of precision for dates far in the distant future (100,000’s of years from now).The IEEE 754 spec, which double uses, uses up to only 53 bits for the non-exponent part of the number. Since long is 64 bits, roughly speaking you’ll potentially lose precision if the
longvalue exceeds 53 bits.However, 53 bits can accurately represent the current epoch millisecond time
longvalue, which requires only 41 bits.A loss of precision will not occur until the epoch time exceeds 253, which won’t occur until
Oct 12 287396.Even at the worst case, 11 bits of “inaccuracy” would still translate to a time value with an accuracy of about ±1 second (
2 ^ 11 = 2048, which in milliseconds is a range of 2 seconds).In short, converting an epoch time to
doubleis OK.