Skip to content

Commit

Permalink
making typemin and typemax give Infs on Floats
Browse files Browse the repository at this point in the history
adding isdenormal()
using contains() instead of has(String, Char), part of issue #50
  • Loading branch information
JeffBezanson committed Aug 26, 2011
1 parent e055905 commit 27c84fe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 72 deletions.
21 changes: 7 additions & 14 deletions examples/graph.j
Expand Up @@ -77,13 +77,6 @@
# * Implement more algorithms, including Dijkstra's, max flow, (and maximal matchings?).


function in(A::Array, b)
for a = A
if isequal(a,b); return true; end
end
return false
end

function index_of(A::Array, b)
i = 1
for a = A
Expand Down Expand Up @@ -239,7 +232,7 @@ function has_vertex(G::ILGraph, name)
end

function has_vertex(G::ILGraph, v::ILVertex)
in(G.vertices, v)
contains(G.vertices, v)
end

function get_vertex(G::ILGraph, name)
Expand All @@ -252,13 +245,13 @@ end
function adjacent(G::ILGraph, name1, name2)
x = get_vertex(G, name1)
y = get_vertex(G, name2)
return in(x.neighbors, y)
return contains(x.neighbors, y)
end

function adjacent(G::ILGraph, x::ILVertex, y::ILVertex)
if !has_vertex(G, x); error("adjacent: graph does not have $x"); end
if !has_vertex(G, y); error("adjacent: graph does not have $y"); end
return in(x.neighbors, y)
return contains(x.neighbors, y)
end

function neighbors(G::ILGraph, name)
Expand Down Expand Up @@ -504,7 +497,7 @@ function delete(G::BGraph, x::BVertex, y::BVertex)
end

function has_vertex(G::BGraph, v::BVertex)
in(G.vertices, v)
contains(G.vertices, v)
end

function has_vertex(G::BGraph, name, side::Bool)
Expand All @@ -526,13 +519,13 @@ get_vertex(G::BGraph, nameside) = get_vertex(G, nameside[1], nameside[2])
function adjacent(G::BGraph, name1, name2)
x = get_vertex(G, name1, true)
y = get_vertex(G, name2, false)
return in(x.neighbors, y)
return contains(x.neighbors, y)
end

function adjacent(G::BGraph, x::BVertex, y::BVertex)
if !has_vertex(G, x); error("adjacent: graph does not have $x"); end
if !has_vertex(G, y); error("adjacent: graph does not have $y"); end
return in(x.neighbors, y)
return contains(x.neighbors, y)
end

function neighbors(G::BGraph, name, side::Bool)
Expand Down Expand Up @@ -672,7 +665,7 @@ function search(G::Graph, start_test, end_test, move_test, bfs::Bool)
path = pop(paths)
lastPlace = path[numel(path)]
for v = lastPlace.neighbors
if in(path, v); continue; end
if contains(path, v); continue; end
if move_test(lastPlace, v)
path2 = copy(path)
newPath = push(path2, v)
Expand Down
35 changes: 5 additions & 30 deletions j/abstractarray.j
Expand Up @@ -749,16 +749,7 @@ end

## Reductions ##

function contains(itr, x)
for y=itr
if y==x
return true
end
end
return false
end

contains(s::Number, n::Int) = (s == n)
contains(s::Number, n::Number) = (s == n)

areduce{T}(f::Function, A::AbstractArray{T}, region::Region, v0) =
areduce(f,A,region,v0,T)
Expand Down Expand Up @@ -914,32 +905,16 @@ function areduce(f::Function, A::AbstractArray, dim::Size, RType::Type)
end
end

function initial_max_val{T}(::Type{T})
if subtype(T,Int)
typemin(T)
else
convert(T,-Inf)
end
end

function initial_min_val{T}(::Type{T})
if subtype(T,Int)
typemax(T)
else
convert(T,Inf)
end
end

function max{T}(A::AbstractArray{T})
v = initial_max_val(T)
v = typemin(T)
for i=1:numel(A)
v = max(v,A[i])
end
v
end

function min{T}(A::AbstractArray{T})
v = initial_min_val(T)
v = typemax(T)
for i=1:numel(A)
v = min(v,A[i])
end
Expand All @@ -963,9 +938,9 @@ function prod{T}(A::AbstractArray{T})
end

max{T}(A::AbstractArray{T}, region::Region) = areduce(max, A, region,
initial_max_val(T), T)
typemin(T), T)
min{T}(A::AbstractArray{T}, region::Region) = areduce(min, A, region,
initial_min_val(T), T)
typemax(T), T)
sum{T}(A::AbstractArray{T}, region::Region) = areduce(+, A, region, zero(T))
prod{T}(A::AbstractArray{T}, region::Region) = areduce(*, A, region, one(T))

Expand Down
11 changes: 7 additions & 4 deletions j/float.j
Expand Up @@ -118,10 +118,13 @@ NaN = boxf64(unbox64(0x7ff8000000000000))
inf{T<:Float}(x::T) = inf(T)
nan{T<:Float}(x::T) = nan(T)

typemin(::Type{Float32}) = $boxf32(unbox32(uint32(0x00800000)))
typemax(::Type{Float32}) = $boxf32(unbox32(uint32(0x7f7fffff)))
typemin(::Type{Float64}) = $boxf64(unbox64(0x0010000000000000))
typemax(::Type{Float64}) = $boxf64(unbox64(0x7fefffffffffffff))
isdenormal(x::Float32) = (abs(x) < $boxf32(unbox32(uint32(0x00800000))))
isdenormal(x::Float64) = (abs(x) < $boxf64(unbox64(0x0010000000000000)))

typemin(::Type{Float32}) = $(-float32(Inf))
typemax(::Type{Float32}) = $(float32(Inf))
typemin(::Type{Float64}) = $(-Inf)
typemax(::Type{Float64}) = Inf

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Aug 28, 2011

Member

Shouldn't this be typemax(::Type{Float64}) = $(Inf) so that the global lookup doesn't happen on every call?

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson Aug 28, 2011

Author Member

Yes. Also for type inference purposes, since we can't infer the types of globals since we don't have actual constants yet.


eps(::Type{Float32}) = $boxf32(unbox32(uint32(0x34000000)))
eps(::Type{Float64}) = $boxf64(unbox64(0x3cb0000000000000))
Expand Down
9 changes: 9 additions & 0 deletions j/reduce.j
Expand Up @@ -86,6 +86,15 @@ function allp(pred, itr)
return true
end

function contains(itr, x)
for y=itr
if isequal(y,x)
return true
end
end
return false
end

## Scans ##

scan(op::Function, x::()) = ()
Expand Down
36 changes: 12 additions & 24 deletions j/string.j
Expand Up @@ -109,18 +109,6 @@ end

strchr(s::String, c::Char) = strchr(s, c, start(s))

function has(s::String, c::Char)
i = start(s)
while !done(s,i)
d, j = next(s,i)
if c == d
return true
end
i = j
end
return false
end

function chars(s::String)
cx = Array(Char,strlen(s))
i = 0
Expand Down Expand Up @@ -343,15 +331,15 @@ function print_escaped(s::String, esc::String)
i = start(s)
while !done(s,i)
c, j = next(s,i)
c == '\0' ? print(escape_nul(s,j)) :
c == '\e' ? print(L"\e") :
c == '\\' ? print("\\\\") :
has(esc,c) ? print('\\', c) :
iswprint(c) ? print(c) :
7 <= c <= 13 ? print('\\', "abtnvfr"[c-6]) :
c <= '\x7f' ? print(L"\x", hex(c, 2)) :
c <= '\uffff' ? print(L"\u", hex(c, need_full_hex(s,j) ? 4 : 2)) :
print(L"\U", hex(c, need_full_hex(s,j) ? 8 : 4))
c == '\0' ? print(escape_nul(s,j)) :
c == '\e' ? print(L"\e") :
c == '\\' ? print("\\\\") :
contains(esc,c) ? print('\\', c) :
iswprint(c) ? print(c) :
7 <= c <= 13 ? print('\\', "abtnvfr"[c-6]) :
c <= '\x7f' ? print(L"\x", hex(c, 2)) :
c <= '\uffff' ? print(L"\u", hex(c, need_full_hex(s,j) ? 4 : 2)) :
print(L"\U", hex(c, need_full_hex(s,j) ? 8 : 4))
i = j
end
end
Expand All @@ -364,13 +352,13 @@ quote_string(s::String) = print_to_string(length(s)+2, print_quoted, s)
# bare minimum unescaping function unescapes only given characters

function print_unescaped_chars(s::String, esc::String)
if !has(esc,'\\')
if !contains(esc,'\\')
esc = strcat("\\", esc)
end
i = start(s)
while !done(s,i)
c, i = next(s,i)
if c == '\\' && !done(s,i) && has(esc,s[i])
if c == '\\' && !done(s,i) && contains(esc,s[i])
c, i = next(s,i)
end
print(c)
Expand Down Expand Up @@ -703,7 +691,7 @@ function split(s::String, delims, include_empty)
tokstart = tokend = i
while !done(s,i)
(c,i) = next(s,i)
if has(delims, c)
if contains(delims, c)
break
end
tokend = i
Expand Down

0 comments on commit 27c84fe

Please sign in to comment.