This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Scientific notation doesn't work with itertools.islice
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, rhettinger, ynikitenko
Priority: normal Keywords:

Created on 2019-11-14 17:31 by ynikitenko, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg356618 - (view) Author: Yaroslav Nikitenko (ynikitenko) Date: 2019-11-14 17:31
Numbers written in scientific notation don't work with itertools.islice.

Check this script: 

# a usual function works
## def works as well
f = lambda x : x 
f(1e+6)
# 1000000.0
import itertools
# islice without scientific notation works
itertools.islice([], 1000000)
# <itertools.islice object at 0x7fc7ce55be90>
itertools.islice([], 1e+6)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.

All this works well in Python 2.7.17, but scientific notation fails in Python 3.7.5. I use Fedora Core 29.
msg356619 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-14 18:00
Hello Yaroslav.  What you've observed was a intentional change to make islice() behave more like regular slices.  Neither of those accept floats which are precarious to use for indexing.

A workaround is to replace 1e+6 with 10**6.
msg356622 - (view) Author: Yaroslav Nikitenko (ynikitenko) Date: 2019-11-14 18:35
Hello Raymond. Many thanks for your explanation.

In this case I suggest any of the following:

1) distinguish between integer and floating numbers in scientific notation. Definitely, 1e+6 is an integer. I can't see where else numbers in scientific notation are defined as only floats.

2) to write explicitly in the documentation that scientific notation is always float. I searched documentation of 'scientific notation', but didn't found that (https://docs.python.org/3/search.html?q=scientific+notation&check_keywords=yes&area=default)

3) to provide a better exception message in islice if this is a known issue. 

Should I change parameters of my report or create a new one? I'm afraid this message may get lost if closed.
msg356625 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2019-11-14 19:23
The behavior is documented here: https://docs.python.org/3.9/library/stdtypes.html#numeric-types-int-float-complex

> Numeric literals containing a decimal point or an exponent sign yield floating point numbers.
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82982
2019-11-14 19:23:45ammar2setnosy: + ammar2
messages: + msg356625
2019-11-14 18:35:17ynikitenkosetmessages: + msg356622
2019-11-14 18:00:24rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg356619

resolution: not a bug
stage: resolved
2019-11-14 17:31:42ynikitenkocreate