Bug 15720 – iota(long.max, long.min, step) does not work properly

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-02-24T18:37:55Z
Last change time
2018-01-05T13:28:51Z
Assigned to
No Owner
Creator
hsteoh

Comments

Comment #0 by hsteoh — 2016-02-24T18:37:55Z
Code: ------ import std.range; import std.stdio; void main() { writeln(iota(long.max, long.min, -1).length); writeln(iota(long.max, long.min, -2).length); writeln(iota(long.max, long.min, -3).length); } ------ Output: ------ 18446744073709551615 0 1 ------ Expected output: ------ 18446744073709551615 9223372036854775807 6148914691236517205 ------
Comment #1 by astrothayne — 2016-03-20T22:49:30Z
This happens because the operation to compute the length is ``` unsigned((start - stop) / -step) ``` but `long.max - long.min` overflows the long and results in -1. The reason it works for a step of 1 is that -1 is cast to a ulong, which results in the maximum value of ulong (18446744073709551615). Maybe this could be fixed by casting to the unsigned type before dividing by the step.
Comment #2 by schveiguy — 2017-05-14T23:24:41Z
This might be fixed with the PR written for issue 16246 as the way the end condition is checked has changed (and length calc). Once it's merged, please re-check.
Comment #3 by andrei — 2017-05-16T17:25:44Z
Comment #4 by github-bugzilla — 2017-05-16T20:13:37Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/49ee158a9e5887ad835dc0f04c0309adf22ff965 Fix Issue 15720 - iota(long.max, long.min, step) does not work properly https://github.com/dlang/phobos/commit/138d9b91284bba8da3ceb9e5da2f52fea85db1c9 Merge pull request #5397 from andralex/15720 Fix Issue 15720 - iota(long.max, long.min, step) does not work properly merged-on-behalf-of: Vladimir Panteleev <[email protected]>
Comment #5 by github-bugzilla — 2017-06-17T11:34:50Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/49ee158a9e5887ad835dc0f04c0309adf22ff965 Fix Issue 15720 - iota(long.max, long.min, step) does not work properly https://github.com/dlang/phobos/commit/138d9b91284bba8da3ceb9e5da2f52fea85db1c9 Merge pull request #5397 from andralex/15720
Comment #6 by github-bugzilla — 2018-01-05T13:28:51Z
Commits pushed to dmd-cxx at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/49ee158a9e5887ad835dc0f04c0309adf22ff965 Fix Issue 15720 - iota(long.max, long.min, step) does not work properly https://github.com/dlang/phobos/commit/138d9b91284bba8da3ceb9e5da2f52fea85db1c9 Merge pull request #5397 from andralex/15720