Skip to content

Commit f8bae15

Browse files
sostockKristofferC
authored andcommitted
Fix for dimensionful numbers (#41595)
(cherry picked from commit 4720378)
1 parent 6153a7e commit f8bae15

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

base/range.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ function length(r::OrdinalRange{T}) where T
681681
# s != 0, by construction, but avoids the division error later
682682
start = first(r)
683683
if s == zero(s) || isempty(r)
684-
return Integer(start - start + zero(s))
684+
return Integer(div(start-start, oneunit(s)))
685685
end
686686
stop = last(r)
687687
if isless(s, zero(s))

test/ranges.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,7 @@ using .Main.Furlongs
14571457
@testset "dimensional correctness" begin
14581458
@test length(Vector(Furlong(2):Furlong(10))) == 9
14591459
@test length(range(Furlong(2), length=9)) == checked_length(range(Furlong(2), length=9)) == 9
1460+
@test @inferred(length(StepRange(Furlong(2), Furlong(1), Furlong(1)))) == 0
14601461
@test Vector(Furlong(2):Furlong(1):Furlong(10)) == Vector(range(Furlong(2), step=Furlong(1), length=9)) == Furlong.(2:10)
14611462
@test Vector(Furlong(1.0):Furlong(0.5):Furlong(10.0)) ==
14621463
Vector(Furlong(1):Furlong(0.5):Furlong(10)) == Furlong.(1:0.5:10)

test/testhelpers/Furlongs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Furlong{p,T<:Number} <: Number
1414
end
1515
Furlong(x::T) where {T<:Number} = Furlong{1,T}(x)
1616
Furlong(x::Furlong) = x
17-
(::Type{T})(x::Furlong) where {T<:Number} = T(x.val)::T
17+
(::Type{T})(x::Furlong{0}) where {T<:Number} = T(x.val)::T
1818
Furlong{p}(v::Number) where {p} = Furlong{p,typeof(v)}(v)
1919
Furlong{p}(x::Furlong{q}) where {p,q} = (@assert(p==q); Furlong{p,typeof(x.val)}(x.val))
2020
Furlong{p,T}(x::Furlong{q}) where {T,p,q} = (@assert(p==q); Furlong{p,T}(T(x.val)))

0 commit comments

Comments
 (0)