I currently have a long timestamp measured in units of 100ns elapsed since January 1st, 1900. I need to convert it to milliseconds.

I have the choice of either multiplying by 0.0001 or dividing by 10_000. Although at first glance they sound the same, the former would actually cause an implicit cast to a double - the latter would of course result in another long with the remainder truncated.

Which would yield a better result? Obviously double is an imprecise type that introduces errors due to the use of a mantissa, floating radix, and exponent, but would that error be less or more than the error from performing the simple integer division? Or would the error be demonstrably negligible?

To give an example of one of my values, here is one of the timestamps: 38348440316924872 .

I'm specifically referring to C#, but this question should be general to computer science.


1 Answer 1


I think the answer to your question really depends on what you want to achieve, namely:

  • Do you need the measure to be more precise than milliseconds?

If the answer to the first question is yes:

  • I think double is a reasonable choice. In the range of the number you gave the worse error I got is with 38348440316924876 where if multiplied with 0.0001 gave me 3834844031692.4883.
  • A even better solution might be to use a fixed point number where in the underling data structure stores the 100ns steps and abstracts in a way that it feels like working with milliseconds.

If the answer to the question is no:

  • I would just divide by 10000, if you want the value to be rounded and not truncated I would just apply this formula $\frac{x + 5000}{10000}$ to get a rounded value.

Edit: I did not really answer your question, but as you can see the error with double is much smaller than the one you would be getting with long

  • $\begingroup$ I need error to be less than +-0.5ms. $\endgroup$
    – Bassinator
    Jul 9, 2021 at 21:19
  • 2
    $\begingroup$ The formula x + 5000 / 10000 guarantees an error smaller than 0.5. I am pretty sure you would be fine with double on the range of the number you gave but bigger the number gets the bigger the error also becomes. $\endgroup$ Jul 9, 2021 at 21:24
  • $\begingroup$ Do you mind sharing how you came to that conclusion re: guaranteeing a given error. $\endgroup$
    – Bassinator
    Jul 9, 2021 at 21:34
  • 1
    $\begingroup$ Yeah sure. If you divide by 10000 you are guaranteed that the truncated value adds a negative value (in steps of 100ns) between -0 and -9999. If you add 5000 to the original value then the value added is between 5000 and -4999. That in milliseconds is +-0.5ms. $\endgroup$ Jul 9, 2021 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.