@@ -53,7 +53,7 @@ def __getitem__(self, key):
53
53
if isinstance (key , slice ):
54
54
if key .start is None and key .stop is None and key .step == - 1 :
55
55
return float_range (
56
- start = self .stop - self .step ,
56
+ start = self .start + ( len ( self ) - 1 ) * self .step ,
57
57
stop = self .start - self .step ,
58
58
step = - self .step ,
59
59
)
@@ -68,24 +68,18 @@ def __len__(self):
68
68
69
69
def __next__ (self ):
70
70
self .current += self .step
71
- if self .current < self .stop :
71
+ if self .current < self .stop and not np . isclose ( self . current , self . stop ) :
72
72
return self .current
73
73
raise StopIteration
74
74
75
- def index (self , key ):
75
+ def index (self , key ) -> int :
76
76
if key not in self :
77
77
raise ValueError
78
78
return round ((key - self .start ) / self .step )
79
79
80
80
@property
81
- def length (self ):
82
- if self .step > 0 :
83
- lo , hi = self .start , self .stop
84
- step = self .step
85
- else :
86
- hi , lo = self .start , self .stop
87
- step = - self .step
88
- if lo > hi or np .isclose (lo , hi ):
89
- return 0
90
- else :
91
- return round ((hi - lo - step ) / step + 1 )
81
+ def length (self ) -> int :
82
+ estimate = (self .stop - self .start ) // self .step
83
+ if not np .isclose (self .start + estimate * self .step , self .stop ):
84
+ estimate += 1
85
+ return estimate
0 commit comments