Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JuliaLang/julia
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 17, 2012
2 parents 3ee0fc9 + c37e8f1 commit 4aba4d5
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 102 deletions.
4 changes: 4 additions & 0 deletions j/error.j
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ error(e::Exception) = throw(e)
error{E<:Exception}(::Type{E}) = throw(E())
error(s...) = throw(ErrorException(cstring(s...)))

macro unexpected()
:(error("unexpected branch reached"))
end

## system error handling ##

errno() = ccall(:jl_errno, Int32, ())
Expand Down
27 changes: 14 additions & 13 deletions j/grisu.j
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,39 @@ macro grisu_ccall(value, mode, ndigits)
(Float64, Int32, Int32, Ptr{Uint8}, Int32,
Ptr{Bool}, Ptr{Int32}, Ptr{Int32}),
float64($value), int32($mode), int32($ndigits),
digits, buflen, _neg, _len, _pt)
pdigits = pointer(digits)
_digits, buflen, _neg, _len, _pt)
pdigits = pointer(_digits)
neg = _neg[1]
len = _len[1]
pt = _pt[1]
end
end

let digits = Array(Uint8,309), # 308 is the largest possible decimal exponent.
let _digits = Array(Uint8,309), # 308 is the largest possible decimal exponent.
_neg = Array(Bool, 1),
_len = Array(Int32,1),
_pt = Array(Int32,1)

const buflen = int32(length(digits)+1)
const buflen = int32(length(_digits)+1)

const SHORTEST = int32(0) # shortest exact representation for doubles
const SHORTEST_SINGLE = int32(1) # shortest exact representation for singles
const FIXED = int32(2) # fixed number of trailing decimal points
const PRECISION = int32(3) # fixed precision regardless of magnitude

# wrapper for the core grisu function, primarily for debugging
global grisu
global grisu, grisu_fix, grisu_sig
function grisu(x::Float64, mode::Integer, ndigits::Integer)
if !isfinite(x); error("non-finite value: $x"); end
if ndigits < 0; error("negative digits requested"); end
@grisu_ccall x mode ndigits
neg, ASCIIString(digits[1:len]), pt
neg, ASCIIString(_digits[1:len]), pt
end
grisu(x::Float64) = grisu(x, SHORTEST, int32(0))
grisu(x::Float32) = grisu(float64(x), SHORTEST_SINGLE, int32(0))
grisu(x::Real) = grisu(float(x))
grisu(x::Real, n::Integer) = n >= 0 ? grisu(float64(x), PRECISION, int32(n)) :
grisu(float64(x), FIXED, -int32(n))
grisu_fix(x::Real, n::Integer) = grisu(float64(x), FIXED, int32(n))
grisu_sig(x::Real, n::Integer) = grisu(float64(x), PRECISION, int32(n))

function _show(x::Float, mode::Int32)
s = current_output_stream()
Expand All @@ -49,7 +50,7 @@ function _show(x::Float, mode::Int32)
end
if pt <= -4 || pt > 6 # .00001 to 100000.
# => #.#######e###
write(s, digits[1])
write(s, pdigits, 1)
write(s, '.')
if len > 1
write(s, pdigits+1, len-1)
Expand All @@ -65,17 +66,17 @@ function _show(x::Float, mode::Int32)
write(s, '0')
pt += 1
end
write(s, pdigits+0, len)
write(s, pdigits, len)
elseif pt >= len
# => ########00.0
write(s, pdigits+0, len)
write(s, pdigits, len)
while pt > len
write(s, '0')
len += 1
end
write(s, ".0")
else # => ####.####
write(s, pdigits+0, pt)
write(s, pdigits, pt)
write(s, '.')
write(s, pdigits+pt, len-pt)
end
Expand All @@ -86,7 +87,7 @@ global show
global showcompact
show(x::Float64) = _show(x, SHORTEST)
show(x::Float32) = _show(x, SHORTEST_SINGLE)
showcompact(x::Float) = _show(x, SHORTEST_SINGLE)
# showcompact(x::Float) = show(x, SHORTEST_SINGLE) # TODO: better short float printing.

# normal:
# 0 < pt < len ####.#### len+1
Expand Down
Loading

0 comments on commit 4aba4d5

Please sign in to comment.