Skip to content

Commit dc26b09

Browse files
committed
disallow negative numbers
1 parent 2f7f0e9 commit dc26b09

File tree

2 files changed

+5
-25
lines changed

2 files changed

+5
-25
lines changed

base/range.jl

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,10 +1527,6 @@ julia> logrange(1f0, 32f0, 11)
15271527
15281528
julia> logrange(1, 1000, length=4) ≈ 10 .^ (0:3)
15291529
true
1530-
1531-
julia> logrange(-27, -3, length=7) # allows negative numbers
1532-
7-element Base.LogRange{Float64, Base.TwicePrecision{Float64}}:
1533-
-27.0, -18.7208, -12.9802, -9.0, -6.24025, -4.32675, -3.0
15341530
```
15351531
15361532
See the [`LogRange`](@ref Base.LogRange) type for further details.
@@ -1555,18 +1551,15 @@ provided, but intermediate values may have small floating-point errors.
15551551
These are calculated using the logs of the endpoints, which are
15561552
stored on construction, often in higher precision than `T`.
15571553
1558-
Negative values of `start` and `stop` are allowed, but both must have the
1559-
same sign. All values are then negative.
1560-
15611554
# Examples
15621555
```jldoctest
15631556
julia> logrange(1, 4, length=5)
15641557
5-element Base.LogRange{Float64, Base.TwicePrecision{Float64}}:
15651558
1.0, 1.41421, 2.0, 2.82843, 4.0
15661559
1567-
julia> Base.LogRange{Float16}(-1, -4, 5)
1560+
julia> Base.LogRange{Float16}(1, 4, 5)
15681561
5-element Base.LogRange{Float16, Float64}:
1569-
-1.0, -1.414, -2.0, -2.828, -4.0
1562+
1.0, 1.414, 2.0, 2.828, 4.0
15701563
15711564
julia> logrange(1e-310, 1e-300, 11)[1:2:end]
15721565
6-element Vector{Float64}:
@@ -1664,20 +1657,15 @@ struct LogRange{T<:Number,X} <: AbstractArray{T,1}
16641657
elseif len == 1 && start != stop
16651658
throw(ArgumentError(LazyString(
16661659
"LogRange(", start, ", ", stop, ", ", len, "): endpoints differ, while length is 1")))
1667-
elseif iszero(start) || iszero(stop)
1668-
elseif T <: Real && (start<0) (stop<0)
1660+
elseif T <: Real && ((start<0) || (stop<0))
16691661
throw(DomainError((start, stop),
16701662
"LogRange will only return complex results if called with a complex argument"))
16711663
end
16721664
if T <: Integer || T <: Complex{<:Integer}
16731665
# LogRange{Int}(1, 512, 4) produces InexactError: Int64(7.999999999999998)
16741666
throw(ArgumentError("LogRange{T} does not support integer types"))
16751667
end
1676-
ex = if T <: Real && start + stop < 0 # start+stop allows for LogRange(-0.0, -2, 3)
1677-
_logrange_extra(-a, -b, len)
1678-
else
1679-
_logrange_extra(a, b, len)
1680-
end
1668+
ex = _logrange_extra(a, b, len)
16811669
new{T,typeof(ex[1])}(a, b, len, ex)
16821670
end
16831671
end

test/ranges.jl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,6 @@ end
26122612
@test logrange(1, 10^9, 19)[1:2:end] 10 .^ (0:9)
26132613

26142614
# negative & complex
2615-
@test logrange(-1, -4, 3) == [-1, -2, -4]
26162615
@test logrange(1, -1+0.0im, 3) [1, im, -1] # branch cut first arg
26172616
@test logrange(1, -1-0.0im, 3) [1, -im, -1]
26182617
@test logrange(-1+1e-10im, 1, 3) [-1, im, 1] # branch cut second arg
@@ -2628,10 +2627,8 @@ end
26282627
@test logrange(nextfloat(0f0), floatmax(Float32), typemax(Int))[end] === floatmax(Float32)
26292628
@test logrange(nextfloat(Float16(0)), floatmax(Float16), 66_000)[end] === floatmax(Float16)
26302629
@test first(logrange(pi, 2pi, 3000)) === logrange(pi, 2pi, 3000)[1] === Float64(pi)
2631-
@test last(logrange(-0.01, -0.1, 3000)) === last(logrange(-0.01, -0.1, 3000))[end] === -0.1
26322630
if Int == Int64
26332631
@test logrange(0.1, 1000, 2^54)[end] === 1000.0
2634-
@test logrange(-0.1, -1000, 2^55)[end] === -1000.0
26352632
end
26362633

26372634
# empty, only, NaN, Inf
@@ -2645,18 +2642,12 @@ end
26452642
@test isnan(logrange(1f0, NaN32, 3)[2])
26462643
@test isnan(logrange(NaN32, 2f0, 3)[2])
26472644
@test isnan(logrange(0, 2, 3)[1])
2648-
@test isnan(logrange(0, -2, 3)[1])
26492645
@test isnan(logrange(-0.0, +2.0, 3)[1])
26502646
@test isnan(logrange(0f0, 2f0, 3)[1])
2651-
@test isnan(logrange(0f0, -2f0, 3)[1])
2652-
@test isnan(logrange(-0f0, 2f0, 3)[1])
26532647
@test isinf(logrange(1, Inf, 3)[2])
2654-
@test -Inf === logrange(-1, -Inf, 3)[2]
26552648
@test isinf(logrange(1f0, Inf32, 3)[2])
2656-
@test -Inf32 === logrange(-1f0, -Inf32, 3)[2]
26572649
# constant
26582650
@test logrange(1, 1, 3) == fill(1.0, 3)
2659-
@test logrange(-1f0, -1f0, 3) == fill(-1f0, 3)
26602651
@test all(isnan, logrange(0.0, -0.0, 3))
26612652
@test all(isnan, logrange(-0f0, 0f0, 3))
26622653

@@ -2684,6 +2675,7 @@ end
26842675
@test_throws ArgumentError logrange(1, 10, -1) # negative length
26852676
@test_throws ArgumentError logrange(1, 10, 1) # endpoints must not differ
26862677
@test_throws DomainError logrange(1, -1, 3) # needs complex numbers
2678+
@test_throws DomainError logrange(-1, -2, 3) # not supported, for now
26872679
@test_throws ArgumentError logrange(1, 10, 2)[true] # bad index
26882680
@test_throws BoundsError logrange(1, 10, 2)[3]
26892681
@test_throws ArgumentError Base.LogRange{Int}(1,4,5) # no integer ranges

0 commit comments

Comments
 (0)