Weirdness with parsing of negative Durations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Weirdness with parsing of negative Durations

Drew Kutcharian
Hi All,

Why does Duration("-1 day") parse to -86399999999999 nanoseconds? Is this a bug? Why doesn't it get parsed to "-1 day".

scala> val d1 = Duration("1 d")
d1: scala.concurrent.duration.Duration = 1 day

scala> val d2 = Duration("-1 d")
d2: scala.concurrent.duration.Duration = -86399999999999 nanoseconds

scala> -d1
res2: scala.concurrent.duration.Duration = -1 days

scala> d1 == -d2
res1: Boolean = false

Thanks,

Drew

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Weirdness with parsing of negative Durations

Oliver Ruebenacker

     Hello,

  It is off just by one nanosecond, which is good enough for scala.concurrent purposes, since you can't control or monitor things down to the nanosecond anyway.

  Duration just wraps a Double, which has a precision of about 16 digits. Your example is a bit below that. It might be considered a minor bug, because for that case, it should just about work. You would not expect a guarantee for "-20 days" to work, because that would exceed Double precision.

     Best, Oliver

On Fri, Mar 17, 2017 at 7:54 PM, Drew <[hidden email]> wrote:
Hi All,

Why does Duration("-1 day") parse to -86399999999999 nanoseconds? Is this a bug? Why doesn't it get parsed to "-1 day".

scala> val d1 = Duration("1 d")
d1: scala.concurrent.duration.Duration = 1 day

scala> val d2 = Duration("-1 d")
d2: scala.concurrent.duration.Duration = -86399999999999 nanoseconds

scala> -d1
res2: scala.concurrent.duration.Duration = -1 days

scala> d1 == -d2
res1: Boolean = false

Thanks,

Drew

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Weirdness with parsing of negative Durations

Drew Kutcharian
Oliver, I'm sorry but you're answer is pretty off. FiniteDuration doesn't wrap a Double, it's defined as :

final class FiniteDuration(val length: Long, val unit: TimeUnit)

It's a Long and unit. There is also logic in Duration parsing to handle Double overflow.

- Drew

On Monday, March 20, 2017 at 1:38:57 PM UTC-7, Oliver Ruebenacker wrote:

     Hello,

  It is off just by one nanosecond, which is good enough for scala.concurrent purposes, since you can't control or monitor things down to the nanosecond anyway.

  Duration just wraps a Double, which has a precision of about 16 digits. Your example is a bit below that. It might be considered a minor bug, because for that case, it should just about work. You would not expect a guarantee for "-20 days" to work, because that would exceed Double precision.

     Best, Oliver

On Fri, Mar 17, 2017 at 7:54 PM, Drew <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="9EhX4CFLBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">dr...@...> wrote:
Hi All,

Why does Duration("-1 day") parse to -86399999999999 nanoseconds? Is this a bug? Why doesn't it get parsed to "-1 day".

scala> val d1 = Duration("1 d")
d1: scala.concurrent.duration.Duration = 1 day

scala> val d2 = Duration("-1 d")
d2: scala.concurrent.duration.Duration = -86399999999999 nanoseconds

scala> -d1
res2: scala.concurrent.duration.Duration = -1 days

scala> d1 == -d2
res1: Boolean = false

Thanks,

Drew

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="9EhX4CFLBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">scala-user+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, <a href="http://www.type2diabetesgenetics.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;">Diabetes Portal, <a href="http://www.broadinstitute.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;">Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Weirdness with parsing of negative Durations

Jasper-M
In reply to this post by Drew Kutcharian
The culprit is here: https://github.com/scala/scala/blob/v2.12.1/src/library/scala/concurrent/duration/Duration.scala#L128

Not sure what the reason is for adding 0.5.

Op zaterdag 18 maart 2017 00:54:19 UTC+1 schreef Drew:
Hi All,

Why does Duration("-1 day") parse to -86399999999999 nanoseconds? Is this a bug? Why doesn't it get parsed to "-1 day".

scala> val d1 = Duration("1 d")
d1: scala.concurrent.duration.Duration = 1 day

scala> val d2 = Duration("-1 d")
d2: scala.concurrent.duration.Duration = -86399999999999 nanoseconds

scala> -d1
res2: scala.concurrent.duration.Duration = -1 days

scala> d1 == -d2
res1: Boolean = false

Thanks,

Drew

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Weirdness with parsing of negative Durations

Oliver Ruebenacker
In reply to this post by Drew Kutcharian

     Hello,

  Apologies, I misread the docs and source code. It is first parsed to Double and then converted to Long.

  (nanos + 0.5).toLong appears to be an attempt to round to the nearest Long. It works for positive and positive only.

     Best, Oliver

On Mon, Mar 20, 2017 at 8:25 PM, Drew <[hidden email]> wrote:
Oliver, I'm sorry but you're answer is pretty off. FiniteDuration doesn't wrap a Double, it's defined as :

final class FiniteDuration(val length: Long, val unit: TimeUnit)

It's a Long and unit. There is also logic in Duration parsing to handle Double overflow.

- Drew

On Monday, March 20, 2017 at 1:38:57 PM UTC-7, Oliver Ruebenacker wrote:

     Hello,

  It is off just by one nanosecond, which is good enough for scala.concurrent purposes, since you can't control or monitor things down to the nanosecond anyway.

  Duration just wraps a Double, which has a precision of about 16 digits. Your example is a bit below that. It might be considered a minor bug, because for that case, it should just about work. You would not expect a guarantee for "-20 days" to work, because that would exceed Double precision.

     Best, Oliver

On Fri, Mar 17, 2017 at 7:54 PM, Drew <[hidden email]> wrote:
Hi All,

Why does Duration("-1 day") parse to -86399999999999 nanoseconds? Is this a bug? Why doesn't it get parsed to "-1 day".

scala> val d1 = Duration("1 d")
d1: scala.concurrent.duration.Duration = 1 day

scala> val d2 = Duration("-1 d")
d2: scala.concurrent.duration.Duration = -86399999999999 nanoseconds

scala> -d1
res2: scala.concurrent.duration.Duration = -1 days

scala> d1 == -d2
res1: Boolean = false

Thanks,

Drew

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Weirdness with parsing of negative Durations

Seth Tisue-3
Someone will open a ticket on this at issues.scala-lang.org, I hope? (Or simply submit a fix to https://github.com/scala/scala/pulls, not every fix needs a preceding ticket.)

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.