![]() So when you take (or print) the decimal part of a float anything beyond the 16th digit is garbage. Perhaps the 16th digit is also right, but you can't know. The thing when working with 64bit floats is that the number of "trusted" digits is 15. Perhaps this is just a float rounding error. I am not brave enough to put it into production without confirmation that it's safe.Ĭoncerning the 64-bit float limits: Here are the results in Python 3's repl (pay attention to the 993 input and the 992 in the output): > 9_007_199_254_740_993.0īut perhaps I am reading that "limit" incorrectly. It certainly looks like a success, but is there any risk that my algorithm would be dangerous and break? 1_687_976_937_597_064_000 is the scientifically correct answer given by Wolfram Alpha's calculator.And yes, there's a slight, insignificant float rounding error but I don't mind. Here's a Python repl showing the process: > num = 1687976937.597064 It seems to work, but this technique looks so hacky. Lastly, I add together the two integers. ![]() After that, I truncate the "decimals float" into an integer to ensure that the result will be an integer.By multiplying a 0.X number by 1 billion, the resulting value should never be able to be higher than 1_999_999_999.XXXXX so it seems like this multiplication should be safe. But perhaps the decimals could still overflow somehow? Hopefully floats will just safely truncate the trailing decimals instead of overflowing. By leading with a zero, I ensure that the integer portion of the float will never overflow. And then extract just the decimals so that I get a new 0.XXXXXX float, and then multiply that by 1 billion.Since an integer is able to easily represent the result, I thought I could first convert the integer portion to an integer, and multiply by 1 billion.But a 64-bit float cannot hold the result and would overflow. So 64-bit signed integer can hold the result.And at that scale, there aren't enough bits to store any decimals at all (it's permanently. The goal is figuring out a way to safely reach this number. 1_687_976_937_597_064_000 is the integer result of the above timestamp multiplied by 1 billion.And doing a straight multiplication by 1 billion would overflow the 64-bit float. But there's 1 billion nanoseconds in 1 second. It's seconds with a few fractions as a float. I currently have a UNIX timestamp as a 64-bit float. ![]()
0 Comments
Leave a Reply. |