Sub ConvertMillisecondsToString(t As Long) As String
Dim hours, minutes, seconds As Int
hours = t / DateTime.TicksPerHour
minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
Return $"$1.0{hours}:$2.0{minutes}:$2.0{seconds}"$
End Sub
I believe this has its limitation up to 480 hours. Anything beyond will result in error.
The issue will be that the expression is being calculated using signed 32-bit ints by default, since there is no term within the expression that tells the compiler that a larger numeric type is required.
((864000 * 3) * 1000) = 2592000000 (decimal) = 0b10011010011111101100100000000000 (binary) which has the high bit (bit 31) set which makes it a negative number, since Ints in B4A/B4J/Java are signed 32-bit numbers.
Me too. I'm trying to find a way to force expression evaluation to use 64-bit Long rather than 32-bit Int. You can sort-of do it by adding ".0" to constants (or at least the innermost constant) which makes it a Double, but then you are working with 56 bits of precision rather than 63, so that could be an issue for large numbers.
(deleted various ideas, after realising...)
surely those built in constants, like DateTime.TicksPerSecond, are Longs, in which case this should work correctly:
B4X:
Dim b As Long
b = 864000 * 3 * 1000 'calculated using Int operations
Log(b)
b = 864000 * 3 * DateTime.TicksPerSecond 'calculated using Long operations (cast/forced by TicksPerSecond being Long)
Log(b)
Closer to why it is happening and how to avoid it. One of the numbers on the right side must be a long to force the whole calculation to be long. If you absolutely want to prevent overflows, then make the first number the long, since several int multiplications before the long can still cause an overflow.
Closer to why it is happening and how to avoid it. One of the numbers on the right side must be a long to force the whole calculation to be long. If you absolutely want to prevent overflows, then make the first number the long, since several int multiplications before the long can still cause an overflow.