Skip to content

Commit

Permalink
linspace: raise errors for cases that cannot be constructed.
Browse files Browse the repository at this point in the history
E.g. linspace(realmin(), realmax(), maxintfloat())
  • Loading branch information
StefanKarpinski committed Apr 13, 2015
1 parent f7bff89 commit 7ce35bf
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,16 @@ function linspace{T<:FloatingPoint}(start::T, stop::T, len::T)
end
end
end
if isinf(n*start) || isinf(n*stop)
n, p = frexp(n)
a, c, s = start, stop, n
if isinf(a*n) || isinf(c*n)
s, p = frexp(s)
p = one(p) << p
start /= p; stop /= p
a /= p; c /= p
end
return LinSpace(start, stop, len, n)
if a*n/s == start && c*n/s == stop

This comment has been minimized.

Copy link
@andreasnoack

andreasnoack Apr 17, 2015

Member

@StefanKarpinski I'm not into the details here, but this causes "cannot be constructed" errors in less pathological cases than the example in the commit message. E.g. if I do

julia> linspace(rand(), 10rand() + 1, rand(3:10))

some of the realizations will fail. Is this to be expected?

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Apr 17, 2015

Author Member

Doesn't always fail, but here's a specific case where it does:

julia> linspace(0.36329458194274333, 4.494641491373471, 8.0)
ERROR: linspace(0.36329458194274333, 4.494641491373471, 8.0): cannot be constructed
 in error at error.jl:19
 in linspace at range.jl:220

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Apr 17, 2015

Author Member

This basically fails for endpoints where start*n/n != start or stop*n/n != stop.

return LinSpace(a, c, len, s)
end
error("linspace($start, $stop, $len): cannot be constructed")
end
function linspace{T<:FloatingPoint}(start::T, stop::T, len::Real)
T_len = convert(T, len)
Expand Down

0 comments on commit 7ce35bf

Please sign in to comment.