Std.Time: `Duration.toMilliseconds` rounds negative durations down (`-1ns` gives `-1ms`), unlike the other unit conversions
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- backend
Research direction
Read src/Std/Time/Duration.lean and compare Duration.toMilliseconds with the other divisions in that file and Nanosecond.Offset.toMilliseconds. Start by running the minimal Lean example from the issue to confirm the negative sub-millisecond result. Done means negative durations convert toward zero consistently with nanoseconds and the other unit conversions, with a regression test for the reported cases.
Written by the indexing model from the issue text.
Description
Prerequisites
- Check that your issue is not already filed: searched for
toMilliseconds,Duration negativeandStd.Time rounding. Related: #16119 (added the "rounds toward zero, like the unit conversions" wording). - Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
- Test your test case against the latest nightly release.
Description
Duration.toMilliseconds rounds a negative sub-millisecond part down, so it disagrees with converting the same value through nanoseconds:
import Std.Time
open Std.Time
def d : Duration := Duration.ofNanoseconds (-1)
#eval d.toMilliseconds
#eval d.toNanoseconds.toMilliseconds
#eval (Duration.ofNanoseconds 1).toMilliseconds
#eval (Timestamp.ofNanosecondsSinceUnixEpoch (-1)).toMillisecondsSinceUnixEpoch
lean DurMs.lean
-1
0
0
0
Larger values are off by one too: (Duration.ofNanoseconds (-1000001)).toMilliseconds is -2, and .toNanoseconds.toMilliseconds of the same duration is -1.
In src/Std/Time/Duration.lean, toMilliseconds uses duration.nano.ediv 1000000. The other divisions in that file use tdiv, and Nanosecond.Offset.toMilliseconds rounds toward zero. src/Std/Time/Internal/UnitVal.lean describes the unit conversions as "rounding toward zero".
Expected behavior: all four lines print 0.
Actual behavior: the first line prints -1.
Versions
4.37.0-nightly-2026-11-04 -1
4.36.0-rc2 -1
4.35.0 -1
These are on macOS arm64 (official toolchains via elan).
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask
them to add 👍 to it.
Found by an automated differential tester (ephyra); this report was drafted with AI assistance.
- Dominant language
- Lean
- Stars
- 10.3k
- Forks
- 1.1k
- Avg merge
- 19h 8m
- Merged PRs (30d)
- 421
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16681 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16305 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16299 ·
Maintainers usually reply within 1 day
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
leanprover/lean4#15683 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#15626 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
vllm-project/vllm-sail#398 ·
Maintainers usually reply within 1 day
-
kubernetes-ingestor: fetchCRDMapping() runs even when crossplane.enabled is false, logging a 403 ERROR every runPossibly taken @kc9ddi claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
TeraSky-OSS/backstage-plugins#228 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
nightscout/nocturne#4057 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
opendatalab/MinerU#5800 · 2 comments ·
Maintainers usually reply within 1 day