@@ -8,7 +8,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
88 div, fld, rem, mod, mod1, fld1, min, max, minmax,
99 rand
1010
11- using Base. Checked: checked_add, checked_sub
11+ using Base. Checked: checked_add, checked_sub, checked_div
1212
1313using Base: @pure
1414
@@ -142,6 +142,21 @@ for f in (:+, :-, :rem, :mod, :mod1, :min, :max)
142142 end
143143end
144144
145+ function Base. unsafe_length (r:: StepRange{X,Y} ) where {X<: FixedPoint{<:ShorterThanInt} , Y<: FixedPoint{<:ShorterThanInt} }
146+ start, step, stop = reinterpret (r. start), reinterpret (r. step), reinterpret (r. stop)
147+ return div (Int (stop) - Int (start) + Int (step), Int (step))
148+ end
149+ function Base. unsafe_length (r:: StepRange{X,Y} ) where {X<: FixedPoint , Y<: FixedPoint }
150+ start, step, stop = reinterpret (r. start), reinterpret (r. step), reinterpret (r. stop)
151+ return div ((stop - start) + step, step)
152+ end
153+ Base. length (r:: StepRange{X,Y} ) where {X<: FixedPoint{<:ShorterThanInt} , Y<: FixedPoint{<:ShorterThanInt} } =
154+ Base. unsafe_length (r)
155+ function Base. length (r:: StepRange{X,Y} ) where {X<: FixedPoint , Y<: FixedPoint }
156+ start, step, stop = reinterpret (r. start), reinterpret (r. step), reinterpret (r. stop)
157+ return checked_div (checked_add (checked_sub (stop, start), step), step)
158+ end
159+
145160# Printing. These are used to generate type-symbols, so we need them
146161# before we include any files.
147162function showtype (io:: IO , :: Type{X} ) where {X <: FixedPoint }
0 commit comments