Paul Woitaschek
12/04/2020, 7:24 AM(200.days + 5.nanoseconds - 200.days).toLongNanoseconds()
This evaluates to 4
, not 5
.Colton Idle
12/04/2020, 7:30 AMColton Idle
12/04/2020, 7:31 AMelizarov
12/04/2020, 7:32 AMPaul Woitaschek
12/04/2020, 7:32 AMLocalDateTime
Paul Woitaschek
12/04/2020, 7:32 AM@Test
fun plus() {
val localDateTime = localDateTime(
date = localDate(year = 2020, month = 1, dayOfMonth = 2),
time = localTime(hour = 1, minute = 2, second = 3, nanoOfSecond = 4)
)
localDateTime.plus(365.days + 2.days + 2.hours + 5.nanoseconds)
.shouldBe(
localDateTime(
date = localDate(2021, 1, 4),
time = localTime(1, 4, 3, 9)
)
)
}
Paul Woitaschek
12/04/2020, 7:33 AMexpect class LocalDateTime
that has an actual typalias
resolving java.time.LocalDateTime
elizarov
12/04/2020, 7:34 AMelizarov
12/04/2020, 7:35 AMrocketraman
12/04/2020, 7:54 AMDouble
. So you're basically just running into a FP rounding error when adding the double that represents 200 days with the double that represents 5 nanoseconds.Nir
12/04/2020, 1:16 PMPaul Woitaschek
12/04/2020, 1:17 PMNir
12/04/2020, 1:17 PMelizarov
12/04/2020, 1:18 PMelizarov
12/04/2020, 1:19 PMNir
12/04/2020, 1:21 PMelizarov
12/04/2020, 1:23 PMNir
12/04/2020, 1:28 PMelizarov
12/04/2020, 1:28 PMNir
12/04/2020, 1:29 PMNir
12/04/2020, 1:30 PMelizarov
12/04/2020, 1:31 PMelizarov
12/04/2020, 1:36 PMNir
12/04/2020, 1:43 PMelizarov
12/04/2020, 1:47 PMelizarov
12/04/2020, 1:52 PMelizarov
12/04/2020, 1:55 PMInt
type is bad because is cannot compute 2 billion + 2 billions without overflowing does not gives much data even with a real-life use-case. An Int
type, just like everything in software engineering, is an example a pragmatic tradeoff.Nir
12/04/2020, 2:07 PMNir
12/04/2020, 2:07 PMNir
12/04/2020, 2:09 PMelizarov
12/04/2020, 2:12 PMInt
still occupies 4 bytes of memory and Long
8 byte. Replace every Int
with Long
inside a modern business app without thinking about and suddenly it consumes more memory for no gain. You also replace all of them with BigInteger
and it consumes way more CPU and memory. You still gained nothing of a substance. Do overflows happens and bugs occur because of them? They do! How often do they happen and does getting rid of them balances out? That's a tradeoff.Nir
12/04/2020, 2:16 PMPaul Woitaschek
12/04/2020, 2:16 PMPaul Woitaschek
12/04/2020, 2:17 PMNir
12/04/2020, 2:17 PMNir
12/04/2020, 2:19 PMPaul Woitaschek
12/04/2020, 2:19 PMNir
12/04/2020, 2:19 PMilya.gorbunov
12/04/2020, 2:22 PMPaul Woitaschek
12/04/2020, 2:23 PMPaul Woitaschek
12/04/2020, 2:24 PMPaul Woitaschek
12/04/2020, 2:26 PMelizarov
12/04/2020, 2:26 PMPaul Woitaschek
12/04/2020, 2:27 PMilya.gorbunov
12/04/2020, 2:27 PMI don't find it very artificial to multiple one second by a number first, and then add it, and continually multiply it by larger numbers@Nir If you operate with whole seconds, Duration provides the exact representation for +-146 years range
Nir
12/04/2020, 2:29 PMNir
12/04/2020, 2:29 PMelizarov
12/04/2020, 2:29 PMNir
12/04/2020, 2:30 PMNir
12/04/2020, 2:30 PMelizarov
12/04/2020, 2:31 PMNir
12/04/2020, 2:32 PMprinln(picoseconds: duration.nanoseconds.toDouble() / 1000)
elizarov
12/04/2020, 2:32 PMNir
12/04/2020, 2:32 PMelizarov
12/04/2020, 2:33 PMNir
12/04/2020, 2:33 PMelizarov
12/04/2020, 2:33 PMNir
12/04/2020, 2:33 PMelizarov
12/04/2020, 2:33 PMNir
12/04/2020, 2:33 PMNir
12/04/2020, 2:34 PMNir
12/04/2020, 2:35 PMilya.gorbunov
12/04/2020, 2:40 PMNir
12/04/2020, 2:41 PMWhat kind of business problem is that when you need to add such year-scale durations to a local date time?
Nir
12/04/2020, 2:42 PMilya.gorbunov
12/04/2020, 2:45 PMNir
12/04/2020, 2:45 PMNir
12/04/2020, 2:46 PMNir
12/04/2020, 2:47 PMlouiscad
12/05/2020, 12:13 PMDuration
of kotlin.time:
Introduce BigDuration
(following naming of BigInteger
) that uses only integers and supports arbitrary precision, at the expense of memory consumption. Then it's up to the programmers to pick the right for the use case they have at hand, hoping they never need to hold zillions of BigDuration
instances in memory.
I think a BigDuration
would make a lot of sense since the loss of precision of Duration
arises as the duration increases (e.g. you lose seconds precision after over 146 years long duration, ~200 days for nanoseconds, and probably sooner for picoseconds or tinier).
This, plus proper documentation in Duration
doc about its limits and in BigDuration
about its memory and CPU impact would probably make a lot of sense and make Kotlin cover all the use cases about durations.
Then, we could get IDE warnings when mixing small precision with big durations when it can be checked, as is the case for the original snippet in this thread where you have 5 nanos and 200 days mixed. Actually, the possible loss of precision could be checked statically if only constants are used (though adding parameters would make things a little more complex and require to extract sub-expressions that can be analyzed with actual values).