diff --git a/NEWS.md b/NEWS.md index cdeecd1c34e27..521a6251714b1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -140,6 +140,12 @@ Deprecated or removed * `{...}` syntax is deprecated in favor of `Any[...]` ([#8578]). + * `itrunc`, `ifloor`, `iceil` and `iround` are deprecated in favour of + `trunc{T<:Integer}(T,x)`, `floor{T<:Integer}(T,x)`, etc.. `trunc` is now + always bound-checked;`Base.unsafe_trunc` provides the old unchecked `itrunc` + behaviour ([#9133]). + + Julia v0.3.0 Release Notes ========================== diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 23fc292d379e9..fd8464199cb83 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1385,7 +1385,7 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real) empty!(S) p == 0 && return S nexpected = p * length(A) - sizehint(S, iround(nexpected + 5*sqrt(nexpected))) + sizehint(S, round(Int,nexpected + 5*sqrt(nexpected))) if p > 0.15 # empirical threshold for trivial O(n) algorithm to be better for i = 1:n rand() <= p && push!(S, A[i]) @@ -1396,14 +1396,14 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real) # s==k (k > 0) is (1-p)^(k-1) * p, and hence the probability (CDF) that # s is in {1,...,k} is 1-(1-p)^k = F(k). Thus, we can draw the skip s # from this probability distribution via the discrete inverse-transform - # method: s = iceil(F^{-1}(u)) where u = rand(), which is simply - # s = iceil(log(rand()) / log1p(-p)). + # method: s = ceil(F^{-1}(u)) where u = rand(), which is simply + # s = ceil(log(rand()) / log1p(-p)). L = 1 / log1p(-p) i = 0 while true s = log(rand()) * L # note that rand() < 1, so s > 0 - s >= n - i && return S # compare before iceil to avoid overflow - push!(S, A[i += iceil(s)]) + s >= n - i && return S # compare before ceil to avoid overflow + push!(S, A[i += ceil(Int,s)]) end # [This algorithm is similar in spirit to, but much simpler than, # the one by Vitter for a related problem in "Faster methods for diff --git a/base/bitarray.jl b/base/bitarray.jl index 40794ba4100e1..e10008a41fe35 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -790,10 +790,7 @@ function empty!(B::BitVector) end ## Misc functions - -for f in (:iround, :itrunc, :ifloor, :iceil, :abs) - @eval ($f)(B::BitArray) = copy(B) -end +abs(B::BitArray) = copy(B) ## Unary operators ## diff --git a/base/char.jl b/base/char.jl index f61a7cfd98475..778a1db06dd40 100644 --- a/base/char.jl +++ b/base/char.jl @@ -1,5 +1,5 @@ char(x) = convert(Char, x) -char(x::FloatingPoint) = char(iround(x)) +char(x::FloatingPoint) = char(round(UInt32,x)) integer(x::Char) = int(x) diff --git a/base/complex.jl b/base/complex.jl index 544211050eb9b..270143182b711 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -477,21 +477,19 @@ function ^{T<:Complex}(z::T, p::T) # apply some corrections to force known zeros if pim == 0 - ip = itrunc(pr) - if ip == pr + if isinteger(pr) if zi == 0 im = copysign(zero(im), im) elseif zr == 0 - if isodd(ip) - re = copysign(zero(re), re) - else + if isinteger(0.5*pr) # pr is even im = copysign(zero(im), im) + else + re = copysign(zero(re), re) end end else dr = pr*2 - ip = itrunc(dr) - if ip == dr && zi == 0 + if isinteger(dr) && zi == 0 if zr < 0 re = copysign(zero(re), re) else diff --git a/base/darray.jl b/base/darray.jl index c96096209c923..cfb854ceea212 100644 --- a/base/darray.jl +++ b/base/darray.jl @@ -94,7 +94,7 @@ end # get array of start indexes for dividing sz into nc chunks function defaultdist(sz::Int, nc::Int) if sz >= nc - iround(linspace(1, sz+1, nc+1)) + round(Int,linspace(1, sz+1, nc+1)) else [[1:(sz+1)], zeros(Int, nc-sz)] end diff --git a/base/dates/ranges.jl b/base/dates/ranges.jl index 719c680705fa4..a9baf97915aea 100644 --- a/base/dates/ranges.jl +++ b/base/dates/ranges.jl @@ -4,7 +4,7 @@ Base.colon{T<:DateTime}(start::T, stop::T) = StepRange(start, Day(1), stop) # Given a start and end date, how many steps/periods are in between -guess(a::DateTime,b::DateTime,c) = ifloor(Int64,(int128(b) - int128(a))/toms(c)) +guess(a::DateTime,b::DateTime,c) = floor(Int64,(int128(b) - int128(a))/toms(c)) guess(a::Date,b::Date,c) = int64(div(int64(b - a),days(c))) function len(a,b,c) lo, hi, st = min(a,b), max(a,b), abs(c) diff --git a/base/deprecated.jl b/base/deprecated.jl index 9dfe8988dd03e..ab873f30b4c05 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -231,3 +231,12 @@ const Uint128 = UInt128 @deprecate rand!(r::Range, A::AbstractArray) rand!(A, r) @deprecate rand!(mt::MersenneTwister, r::Range, A::AbstractArray) rand!(mt, A, r) + +@deprecate itrunc(x) trunc(Integer,x) +@deprecate itrunc{T<:Integer}(::Type{T},x::Real) trunc(T,x) +@deprecate iceil(x) ceil(Integer,x) +@deprecate iceil{T}(::Type{T},x) ceil(T,x) +@deprecate ifloor(x) floor(Integer,x) +@deprecate ifloor{T}(::Type{T},x) floor(T,x) +@deprecate iround(x) round(Integer,x) +@deprecate iround{T}(::Type{T},x) round(T,x) diff --git a/base/exports.jl b/base/exports.jl index c16c24591f2a2..7e2f9340909f9 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -360,8 +360,6 @@ export gcdx, hex2num, hypot, - iceil, - ifloor, imag, int, int128, @@ -373,7 +371,6 @@ export inv, invdigamma, invmod, - iround, isapprox, iseltype, iseven, @@ -387,7 +384,6 @@ export isqrt, isreal, issubnormal, - itrunc, lcm, ldexp, leading_ones, diff --git a/base/float.jl b/base/float.jl index 295a1f29f4221..ed11352cdcda0 100644 --- a/base/float.jl +++ b/base/float.jl @@ -1,15 +1,4 @@ ## conversions to floating-point ## - -convert(::Type{Float32}, x::Int128) = float32(reinterpret(UInt128,abs(x)))*(1-2(x<0)) -convert(::Type{Float32}, x::UInt128) = float32(uint64(x&0xffffffffffffffff)) + ldexp(float32(uint64(x>>>64)),64) -promote_rule(::Type{Float32}, ::Type{Int128} ) = Float32 -promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32 - -convert(::Type{Float64}, x::Int128) = float64(reinterpret(UInt128,abs(x)))*(1-2(x<0)) -convert(::Type{Float64}, x::UInt128) = float64(uint64(x&0xffffffffffffffff)) + ldexp(float64(uint64(x>>>64)),64) -promote_rule(::Type{Float64}, ::Type{Int128} ) = Float64 -promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64 - convert(::Type{Float16}, x::Integer) = convert(Float16, convert(Float32,x)) for t in (Bool,Char,Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64) @eval promote_rule(::Type{Float16}, ::Type{$t}) = Float32 @@ -29,6 +18,72 @@ for t1 in (Float32,Float64) end end end + +promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64 +promote_rule(::Type{Float64}, ::Type{Int128}) = Float64 +promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32 +promote_rule(::Type{Float32}, ::Type{Int128}) = Float32 + +function convert(::Type{Float64}, x::UInt128) + x == 0 && return 0.0 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 53 + y = ((x % UInt64) << (53-n)) & 0x000f_ffff_ffff_ffff + else + y = ((x >> (n-54)) % UInt64) & 0x001f_ffff_ffff_ffff # keep 1 extra bit + y = (y+1)>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt64(trailing_zeros(x) == (n-54)) # fix last bit to round to even + end + d = ((n+1022) % UInt64) << 52 + reinterpret(Float64, d + y) +end + +function convert(::Type{Float64}, x::Int128) + x == 0 && return 0.0 + s = ((x >>> 64) % UInt64) & 0x8000_0000_0000_0000 # sign bit + x = abs(x) % UInt128 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 53 + y = ((x % UInt64) << (53-n)) & 0x000f_ffff_ffff_ffff + else + y = ((x >> (n-54)) % UInt64) & 0x001f_ffff_ffff_ffff # keep 1 extra bit + y = (y+1)>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt64(trailing_zeros(x) == (n-54)) # fix last bit to round to even + end + d = ((n+1022) % UInt64) << 52 + reinterpret(Float64, s | d + y) +end + +function convert(::Type{Float32}, x::UInt128) + x == 0 && return 0f0 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 24 + y = ((x % UInt32) << (24-n)) & 0x007f_ffff + else + y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit + y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even + end + d = ((n+126) % UInt32) << 23 + reinterpret(Float32, d + y) +end + +function convert(::Type{Float32}, x::Int128) + x == 0 && return 0f0 + s = ((x >>> 96) % UInt32) & 0x8000_0000 # sign bit + x = abs(x) % UInt128 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 24 + y = ((x % UInt32) << (24-n)) & 0x007f_ffff + else + y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit + y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even + end + d = ((n+126) % UInt32) << 23 + reinterpret(Float32, s | d + y) +end + #convert(::Type{Float16}, x::Float32) = box(Float16,fptrunc(Float16,x)) convert(::Type{Float16}, x::Float64) = convert(Float16, convert(Float32,x)) convert(::Type{Float32}, x::Float64) = box(Float32,fptrunc(Float32,x)) @@ -55,55 +110,68 @@ float32(x) = convert(Float32, x) float64(x) = convert(Float64, x) float(x) = convert(FloatingPoint, x) -## conversions from floating-point ## - -# fallbacks using only convert, trunc, ceil, floor, round -itrunc(x::FloatingPoint) = convert(Integer,trunc(x)) -iceil (x::FloatingPoint) = convert(Integer,ceil(x)) # TODO: fast primitive for iceil -ifloor(x::FloatingPoint) = convert(Integer,floor(x)) # TOOD: fast primitive for ifloor -iround(x::FloatingPoint) = convert(Integer,round(x)) - -itrunc{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,trunc(x)) -iceil {T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,ceil(x)) -ifloor{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,floor(x)) -iround{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,round(x)) - -## fast specific type conversions ## - -iround(x::Float32) = iround(Int, x) -iround(x::Float64) = iround(Int, x) -itrunc(x::Float32) = itrunc(Int, x) -itrunc(x::Float64) = itrunc(Int, x) - -for to in (Int8, Int16, Int32, Int64) +for Ti in (Int8, Int16, Int32, Int64) @eval begin - iround(::Type{$to}, x::Float32) = box($to,fpsiround($to,unbox(Float32,x))) - iround(::Type{$to}, x::Float64) = box($to,fpsiround($to,unbox(Float64,x))) - itrunc(::Type{$to}, x::Float32) = box($to,fptosi($to,unbox(Float32,x))) - itrunc(::Type{$to}, x::Float64) = box($to,fptosi($to,unbox(Float64,x))) + unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptosi($Ti,unbox(Float32,x))) + unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptosi($Ti,unbox(Float64,x))) end end - -for to in (UInt8, UInt16, UInt32, UInt64) +for Ti in (UInt8, UInt16, UInt32, UInt64) @eval begin - iround(::Type{$to}, x::Float32) = box($to,fpuiround($to,unbox(Float32,x))) - iround(::Type{$to}, x::Float64) = box($to,fpuiround($to,unbox(Float64,x))) - itrunc(::Type{$to}, x::Float32) = box($to,fptoui($to,unbox(Float32,x))) - itrunc(::Type{$to}, x::Float64) = box($to,fptoui($to,unbox(Float64,x))) + unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptoui($Ti,unbox(Float32,x))) + unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptoui($Ti,unbox(Float64,x))) + end +end + +function unsafe_trunc(::Type{UInt128}, x::Float64) + xu = reinterpret(UInt64,x) + k = int(xu >> 52) & 0x07ff - 1075 + xu = (xu & 0x000f_ffff_ffff_ffff) | 0x0010_0000_0000_0000 + if k <= 0 + UInt128(xu >> -k) + else + UInt128(xu) << k end end +function unsafe_trunc(::Type{Int128}, x::Float64) + copysign(unsafe_trunc(UInt128,x) % Int128, x) +end + +function unsafe_trunc(::Type{UInt128}, x::Float32) + xu = reinterpret(UInt32,x) + k = int(xu >> 23) & 0x00ff - 150 + xu = (xu & 0x007f_ffff) | 0x0080_0000 + if k <= 0 + UInt128(xu >> -k) + else + UInt128(xu) << k + end +end +function unsafe_trunc(::Type{Int128}, x::Float32) + copysign(unsafe_trunc(UInt128,x) % Int128, x) +end + + +# matches convert methods +# also determines floor, ceil, round +trunc(::Type{Signed}, x::Float32) = trunc(Int,x) +trunc(::Type{Signed}, x::Float64) = trunc(Int,x) +trunc(::Type{Unsigned}, x::Float32) = trunc(UInt,x) +trunc(::Type{Unsigned}, x::Float64) = trunc(UInt,x) +trunc(::Type{Integer}, x::Float32) = trunc(Int,x) +trunc(::Type{Integer}, x::Float64) = trunc(Int,x) + +# fallbacks +floor{T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,floor(x)) +ceil {T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,ceil(x)) +round {T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,round(x)) -iround(::Type{Int128}, x::Float32) = convert(Int128,round(x)) -iround(::Type{Int128}, x::Float64) = convert(Int128,round(x)) -iround(::Type{UInt128}, x::Float32) = convert(UInt128,round(x)) -iround(::Type{UInt128}, x::Float64) = convert(UInt128,round(x)) # this is needed very early because it is used by Range and colon round(x::Float64) = ccall((:round, Base.libm_name), Float64, (Float64,), x) floor(x::Float64) = ccall((:floor, Base.libm_name), Float64, (Float64,), x) ## floating point promotions ## - promote_rule(::Type{Float32}, ::Type{Float16}) = Float32 promote_rule(::Type{Float64}, ::Type{Float16}) = Float64 promote_rule(::Type{Float64}, ::Type{Float32}) = Float64 @@ -112,7 +180,6 @@ widen(::Type{Float16}) = Float32 widen(::Type{Float32}) = Float64 ## floating point arithmetic ## - -(x::Float32) = box(Float32,neg_float(unbox(Float32,x))) -(x::Float64) = box(Float64,neg_float(unbox(Float64,x))) @@ -128,7 +195,6 @@ widen(::Type{Float32}) = Float64 # TODO: faster floating point div? # TODO: faster floating point fld? # TODO: faster floating point mod? - rem(x::Float32, y::Float32) = box(Float32,rem_float(unbox(Float32,x),unbox(Float32,y))) rem(x::Float64, y::Float64) = box(Float64,rem_float(unbox(Float64,x),unbox(Float64,y))) @@ -147,7 +213,6 @@ end ## floating point comparisons ## - ==(x::Float32, y::Float32) = eq_float(unbox(Float32,x),unbox(Float32,y)) ==(x::Float64, y::Float64) = eq_float(unbox(Float64,x),unbox(Float64,y)) !=(x::Float32, y::Float32) = ne_float(unbox(Float32,x),unbox(Float32,y)) @@ -177,31 +242,31 @@ function cmp(x::FloatingPoint, y::Real) ifelse(xy, 1, 0)) end -for Ti in (Int64,UInt64) +for Ti in (Int64,UInt64,Int128,UInt128) for Tf in (Float32,Float64) @eval begin function ==(x::$Tf, y::$Ti) fy = ($Tf)(y) - (x == fy) & (y == itrunc($Ti,fy)) + (x == fy) & (y == unsafe_trunc($Ti,fy)) end ==(y::$Ti, x::$Tf) = x==y function <(x::$Ti, y::$Tf) fx = ($Tf)(x) - (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < itrunc($Ti,fx)) )) + (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) )) end function <=(x::$Ti, y::$Tf) fx = ($Tf)(x) - (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= itrunc($Ti,fx)) )) + (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) )) end function <(x::$Tf, y::$Ti) fy = ($Tf)(y) - (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) < y)) + (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y)) end function <=(x::$Tf, y::$Ti) fy = ($Tf)(y) - (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) <= y)) + (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y)) end end end @@ -263,6 +328,31 @@ nextfloat(x::Float64, i::Integer) = nextfloat(x::FloatingPoint) = nextfloat(x,1) prevfloat(x::FloatingPoint) = nextfloat(x,-1) +for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128) + for Tf in (Float32, Float64) + if sizeof(Ti) < sizeof(Tf) || Ti <: Unsigned # Tf(typemin(Ti))-1 is exact + @eval function trunc(::Type{$Ti},x::$Tf) + $(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf)) || throw(InexactError()) + unsafe_trunc($Ti,x) + end + else + @eval function trunc(::Type{$Ti},x::$Tf) + $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))) || throw(InexactError()) + unsafe_trunc($Ti,x) + end + end + end +end + +# adding prevfloat(0.5) will prevent prevfloat(0.5) and odd x with eps(x)=1.0 +# from rounding in the wrong direction in RoundToNearest +for Tf in (Float32,Float64) + @eval function round{T<:Integer}(::Type{T}, x::$Tf) + trunc(T,x+copysign($(prevfloat(Tf(0.5))),x)) + end +end + + @eval begin issubnormal(x::Float32) = (abs(x) < $(box(Float32,unbox(UInt32,0x00800000)))) & (x!=0) issubnormal(x::Float64) = (abs(x) < $(box(Float64,unbox(UInt64,0x0010000000000000)))) & (x!=0) diff --git a/base/float16.jl b/base/float16.jl index 0001394e2bf6f..177517b6fd7d8 100644 --- a/base/float16.jl +++ b/base/float16.jl @@ -102,8 +102,10 @@ convert(::Type{UInt128}, x::Float16) = convert(UInt128, float32(x)) convert{T<:Integer}(::Type{T}, x::Float16) = convert(T, float32(x)) -iround{T<:Integer}(::Type{T}, x::Float16) = iround(T, float32(x)) -itrunc{T<:Integer}(::Type{T}, x::Float16) = itrunc(T, float32(x)) +round{T<:Integer}(::Type{T}, x::Float16) = round(T, float32(x)) +trunc{T<:Integer}(::Type{T}, x::Float16) = trunc(T, float32(x)) +floor{T<:Integer}(::Type{T}, x::Float16) = floor(T, float32(x)) +ceil {T<:Integer}(::Type{T}, x::Float16) = ceil(T, float32(x)) round(x::Float16) = float16(round(float32(x))) trunc(x::Float16) = float16(trunc(float32(x))) diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 25efe7168046c..5194ecd503d82 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -35,11 +35,6 @@ function hex2num(s::AbstractString) return box(Float64,unbox(Int64,parseint(Int64,s,16))) end -@vectorize_1arg Real iround -@vectorize_1arg Real itrunc -@vectorize_1arg Real ifloor -@vectorize_1arg Real iceil - @vectorize_1arg Number abs @vectorize_1arg Number abs2 @vectorize_1arg Number angle @@ -48,9 +43,20 @@ end @vectorize_1arg Number isinf @vectorize_1arg Number isfinite -iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R,1}) = [ iround(T, x[i]) for i = 1:length(x) ] -iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R,2}) = [ iround(T, x[i,j]) for i = 1:size(x,1), j = 1:size(x,2) ] -iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R}) = reshape([ iround(T, x[i]) for i = 1:length(x) ], size(x)) +for f in (:trunc,:floor,:ceil,:round) + @eval begin + function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R,1}) + [ ($f)(T, x[i]) for i = 1:length(x) ] + end + function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R,2}) + [ ($f)(T, x[i,j]) for i = 1:size(x,1), j = 1:size(x,2) ] + end + function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R}) + reshape([ ($f)(T, x[i]) for i = 1:length(x) ], size(x)) + end + end +end + # adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212 # for round, og is the power of 10 relative to the decimal point diff --git a/base/grisu/float.jl b/base/grisu/float.jl index e23520dcdeb02..1cac1c4e6cd58 100644 --- a/base/grisu/float.jl +++ b/base/grisu/float.jl @@ -219,7 +219,7 @@ const MinDecimalExponent = -348 const MaxDecimalExponent = 340 function binexp_cache(min_exponent,max_exponent) - k = iceil((min_exponent+63)*D_1_LOG2_10) + k = ceil(Integer,(min_exponent+63)*D_1_LOG2_10) index = div(CachedPowersOffset+k-1,DecimalExponentDistance) + 1 cp = CachedPowers[index+1] return cp diff --git a/base/int.jl b/base/int.jl index 46b731ad79219..ef0143f57059c 100644 --- a/base/int.jl +++ b/base/int.jl @@ -193,23 +193,14 @@ for to in (UInt8, UInt16, UInt32, UInt64) end end -function convert(::Type{Int128}, x::FloatingPoint) - ax = abs(x) - top = trunc(ldexp(ax,-64)) - bot = ax - ldexp(top,64) - n = int128(convert(UInt64,top))<<64 + int128(convert(UInt64,bot)) - return x<0 ? -n : n -end -convert(::Type{Int128}, x::Float32) = convert(Int128, float64(x)) - -function convert(::Type{UInt128}, x::FloatingPoint) - ax = abs(x) - top = trunc(ldexp(ax,-64)) - bot = ax - ldexp(top,64) - n = uint128(convert(UInt64,top))<<64 + uint128(convert(UInt64,bot)) - return x<0 ? -n : n +for Ti in (Int128,UInt128) + for Tf in (Float32,Float64) + @eval function convert(::Type{$Ti},x::$Tf) + isinteger(x) || throw(InexactError()) + trunc($Ti,x) + end + end end -convert(::Type{UInt128}, x::Float32) = convert(UInt128, float64(x)) convert(::Type{Signed}, x::UInt8 ) = convert(Int8,x) convert(::Type{Signed}, x::UInt16 ) = convert(Int16,x) @@ -257,11 +248,10 @@ trunc(x::Integer) = x floor(x::Integer) = x ceil(x::Integer) = x -iround(x::Integer) = x -iround{T<:Integer}(::Type{T}, x::Integer) = convert(T, x) -itrunc(x::Integer) = x -ifloor(x::Integer) = x - iceil(x::Integer) = x +round{T<:Integer}(::Type{T},x::Integer) = convert(T,x) +trunc{T<:Integer}(::Type{T},x::Integer) = convert(T,x) +floor{T<:Integer}(::Type{T},x::Integer) = convert(T,x) + ceil{T<:Integer}(::Type{T},x::Integer) = convert(T,x) ## integer construction ## @@ -382,7 +372,7 @@ for (f,t) in ((:uint8,:UInt8), (:uint16,:UInt16), (:uint32,:UInt32), (:uint64,:U (:int128,:Int128), (:uint128,:UInt128), (:signed,:Int), (:unsigned,:UInt), (:integer,:Int), (:int,:Int), (:uint,:UInt)) - @eval ($f)(x::FloatingPoint) = iround($t,x) + @eval ($f)(x::FloatingPoint) = round($t,x) end ## wide multiplication, Int128 multiply and divide ## diff --git a/base/interactiveutil.jl b/base/interactiveutil.jl index 4af05d3eccc3d..4d990fac5d851 100644 --- a/base/interactiveutil.jl +++ b/base/interactiveutil.jl @@ -368,7 +368,7 @@ end # testing -function runtests(tests = ["all"], numcores = iceil(CPU_CORES/2)) +function runtests(tests = ["all"], numcores = ceil(Int,CPU_CORES/2)) if isa(tests,AbstractString) tests = split(tests) end diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 11e3cb937979f..e312ff251c401 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -140,7 +140,7 @@ ispow2(x::Integer) = count_ones(x)==1 function nextpow(a::Real, x::Real) (a <= 1 || x <= 0) && throw(DomainError()) x <= 1 && return one(a) - n = iceil(log(a, x)) + n = ceil(Integer,log(a, x)) p = a^(n-1) # guard against roundoff error, e.g., with a=5 and x=125 p >= x ? p : a^n @@ -148,7 +148,7 @@ end # largest a^n <= x, with integer n function prevpow(a::Real, x::Real) (a <= 1 || x < 1) && throw(DomainError()) - n = ifloor(log(a, x)) + n = floor(Integer,log(a, x)) p = a^(n+1) p <= x ? p : a^n end diff --git a/base/libc.jl b/base/libc.jl index 19eebd12a38d9..29bf59aa3ce4e 100644 --- a/base/libc.jl +++ b/base/libc.jl @@ -1,8 +1,8 @@ ## time-related functions ## # TODO: check for usleep errors? -@unix_only systemsleep(s::Real) = ccall(:usleep, Int32, (UInt32,), uint32(iround(s*1e6))) -@windows_only systemsleep(s::Real) = (ccall(:Sleep, stdcall, Void, (UInt32,), uint32(iround(s*1e3))); return int32(0)) +@unix_only systemsleep(s::Real) = ccall(:usleep, Int32, (UInt32,), round(UInt32,s*1e6)) +@windows_only systemsleep(s::Real) = (ccall(:Sleep, stdcall, Void, (UInt32,), round(UInt32,s*1e3)); return int32(0)) type TmStruct sec::Int32 diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index 56c8417b43663..a6ef673ca4151 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -64,9 +64,13 @@ size(M::Bidiagonal) = (length(M.dv), length(M.dv)) size(M::Bidiagonal, d::Integer) = d<1 ? error("dimension out of range") : (d<=2 ? length(M.dv) : 1) #Elementary operations -for func in (conj, copy, round, iround) - func(M::Bidiagonal) = Bidiagonal(func(M.dv), func(M.ev), M.isupper) +for func in (:conj, :copy, :round, :trunc, :floor, :ceil) + @eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.isupper) end +for func in (:round, :trunc, :floor, :ceil) + @eval ($func){T<:Integer}(::Type{T}, M::Bidiagonal) = Bidiagonal(($func)(T,M.dv), ($func)(T,M.ev), M.isupper) +end + transpose(M::Bidiagonal) = Bidiagonal(M.dv, M.ev, !M.isupper) ctranspose(M::Bidiagonal) = Bidiagonal(conj(M.dv), conj(M.ev), !M.isupper) diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index 9b1a0973a20b8..474a494c72005 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -249,7 +249,7 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T}) else s = log2(nA/5.4) # power of 2 later reversed by squaring if s > 0 - si = iceil(s) + si = ceil(Int,s) A /= convert(T,2^si) end CC = T[64764752532480000.,32382376266240000.,7771770303897600., diff --git a/base/linalg/givens.jl b/base/linalg/givens.jl index 4ad93cd3a13d5..f8e17b3a490ca 100644 --- a/base/linalg/givens.jl +++ b/base/linalg/givens.jl @@ -12,7 +12,7 @@ typealias AbstractRotation Union(Givens, Rotation) realmin2(::Type{Float32}) = reinterpret(Float32, 0x26000000) realmin2(::Type{Float64}) = reinterpret(Float64, 0x21a0000000000000) -realmin2{T}(::Type{T}) = (twopar = 2one(T); twopar^itrunc(log(realmin(T)/eps(T))/log(twopar)/twopar)) +realmin2{T}(::Type{T}) = (twopar = 2one(T); twopar^trunc(Integer,log(realmin(T)/eps(T))/log(twopar)/twopar)) function givensAlgorithm{T<:FloatingPoint}(f::T, g::T) zeropar = zero(T) diff --git a/base/linalg/matmul.jl b/base/linalg/matmul.jl index 1af6d5281ee01..c59f9c34a4512 100644 --- a/base/linalg/matmul.jl +++ b/base/linalg/matmul.jl @@ -373,7 +373,7 @@ function generic_matmatmul!{T,S,R}(C::AbstractVecOrMat{R}, tA, tB, A::AbstractVe @inbounds begin if isbits(R) - tile_size = int(ifloor(sqrt(tilebufsize/sizeof(R)))) + tile_size = floor(Int,sqrt(tilebufsize/sizeof(R))) sz = (tile_size, tile_size) Atile = pointer_to_array(convert(Ptr{R}, pointer(Abuf)), sz) Btile = pointer_to_array(convert(Ptr{R}, pointer(Bbuf)), sz) diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index b1b64cd4a8675..f63873c00b81a 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -28,12 +28,12 @@ size(m::SymTridiagonal) = (length(m.dv), length(m.dv)) size(m::SymTridiagonal, d::Integer) = d<1 ? error("dimension out of range") : (d<=2 ? length(m.dv) : 1) #Elementary operations -for func in (:copy, :round, :iround, :conj) - @eval begin - ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev)) - end +for func in (:conj, :copy, :round, :trunc, :floor, :ceil) + @eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev)) +end +for func in (:round, :trunc, :floor, :ceil) + @eval ($func){T<:Integer}(::Type{T},M::SymTridiagonal) = SymTridiagonal(($func)(T,M.dv), (T,$func)(M.ev)) end - transpose(M::SymTridiagonal) = M #Identity operation ctranspose(M::SymTridiagonal) = conj(M) @@ -203,9 +203,14 @@ end copy!(dest::Tridiagonal, src::Tridiagonal) = Tridiagonal(copy!(dest.dl, src.dl), copy!(dest.d, src.d), copy!(dest.du, src.du), copy!(dest.du2, src.du2)) #Elementary operations -for func in (:copy, :round, :iround, :conj) - @eval begin - ($func)(M::Tridiagonal) = Tridiagonal(map(($func), (M.dl, M.d, M.du, M.du2))...) +for func in (:conj, :copy, :round, :trunc, :floor, :ceil) + @eval function ($func)(M::Tridiagonal) + Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du), ($func)(M.du2)) + end +end +for func in (:round, :trunc, :floor, :ceil) + @eval function ($func){T<:Integer}(::Type{T},M::Tridiagonal) + Tridiagonal(($func)(T,M.dl), ($func)(T,M.d), ($func)(T,M.du), ($func)(T,M.du2)) end end diff --git a/base/mmap.jl b/base/mmap.jl index 41ac927151e3d..bf2fcd34a98b4 100644 --- a/base/mmap.jl +++ b/base/mmap.jl @@ -28,7 +28,7 @@ function mmap(len::Integer, prot::Integer, flags::Integer, fd, offset::Integer) error("requested size is too large") end # Set the offset to a page boundary - offset_page::FileOffset = ifloor(offset/pagesize)*pagesize + offset_page::FileOffset = floor(Integer,offset/pagesize)*pagesize len_page::Int = (offset-offset_page) + len # Mmap the file p = ccall(:jl_mmap, Ptr{Void}, (Ptr{Void}, Csize_t, Cint, Cint, Cint, FileOffset), C_NULL, len_page, prot, flags, fd, offset_page) diff --git a/base/mpfr.jl b/base/mpfr.jl index 95b5b71bc8daa..3f732d6528779 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -9,14 +9,14 @@ export import Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely, bessely0, bessely1, ceil, cmp, convert, copysign, deg2rad, - exp, exp2, exponent, factorial, floor, hypot, isinteger, iround, + exp, exp2, exponent, factorial, floor, hypot, isinteger, isfinite, isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf, nextfloat, prevfloat, promote_rule, rad2deg, rem, round, show, showcompact, sum, sqrt, string, print, trunc, precision, exp10, expm1, - gamma, lgamma, digamma, erf, erfc, zeta, eta, log1p, airyai, iceil, ifloor, - itrunc, eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan, + gamma, lgamma, digamma, erf, erfc, zeta, eta, log1p, airyai, + eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan, cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2, - serialize, deserialize, cbrt, typemax, typemin, + serialize, deserialize, cbrt, typemax, typemin, unsafe_trunc, realmin, realmax, get_rounding, set_rounding, maxintfloat, widen, significand, frexp @@ -54,6 +54,7 @@ widen(::Type{BigFloat}) = BigFloat BigFloat(x::BigFloat) = x +# convert to BigFloat for (fJ, fC) in ((:si,:Clong), (:ui,:Culong), (:d,:Float64)) @eval begin function BigFloat(x::($fC)) @@ -91,30 +92,75 @@ convert{S}(::Type{BigFloat}, x::Rational{S}) = BigFloat(x) # to resolve ambiguit convert(::Type{BigFloat}, x::Real) = BigFloat(x) convert(::Type{FloatingPoint}, x::BigInt) = BigFloat(x) -for to in (Int8, Int16, Int32, Int64) - @eval begin - function convert(::Type{$to}, x::BigFloat) - (isinteger(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) - convert($to, ccall((:__gmpfr_mpfr_get_sj,:libmpfr), - Cintmax_t, (Ptr{BigFloat}, Int32), &x, 0)) - end - end +## BigFloat -> Integer +function unsafe_cast(::Type{Int64}, x::BigFloat, r::RoundingMode) + ccall((:__gmpfr_mpfr_get_sj,:libmpfr), Cintmax_t, + (Ptr{BigFloat}, Int32), &x, to_mpfr(r)) +end +function unsafe_cast(::Type{UInt64}, x::BigFloat, r::RoundingMode) + ccall((:__gmpfr_mpfr_get_uj,:libmpfr), Cuintmax_t, + (Ptr{BigFloat}, Int32), &x, to_mpfr(r)) +end + +function unsafe_cast{T<:Signed}(::Type{T}, x::BigFloat, r::RoundingMode) + unsafe_cast(Int64, x, r) % T +end +function unsafe_cast{T<:Unsigned}(::Type{T}, x::BigFloat, r::RoundingMode) + unsafe_cast(UInt64, x, r) % T +end + +function unsafe_cast(::Type{BigInt}, x::BigFloat, r::RoundingMode) + # actually safe, just keep naming consistent + z = BigInt() + ccall((:mpfr_get_z, :libmpfr), Int32, (Ptr{BigInt}, Ptr{BigFloat}, Int32), + &z, &x, to_mpfr(r)) + z +end + +unsafe_trunc{T<:Integer}(::Type{T}, x::BigFloat) = unsafe_cast(T,x,RoundToZero) + +function trunc{T<:Union(Signed,Unsigned)}(::Type{T}, x::BigFloat) + (typemin(T) <= x <= typemax(T)) || throw(InexactError()) + unsafe_cast(T,x,RoundToZero) +end +function floor{T<:Union(Signed,Unsigned)}(::Type{T}, x::BigFloat) + (typemin(T) <= x <= typemax(T)) || throw(InexactError()) + unsafe_cast(T,x,RoundDown) +end +function ceil{T<:Union(Signed,Unsigned)}(::Type{T}, x::BigFloat) + (typemin(T) <= x <= typemax(T)) || throw(InexactError()) + unsafe_cast(T,x,RoundUp) end -for to in (UInt8, UInt16, UInt32, UInt64) +trunc(::Type{BigInt}, x::BigFloat) = unsafe_cast(BigInt, x, RoundToZero) +floor(::Type{BigInt}, x::BigFloat) = unsafe_cast(BigInt, x, RoundDown) +ceil(::Type{BigInt}, x::BigFloat) = unsafe_cast(BigInt, x, RoundUp) +# convert/round/trunc/floor/ceil(Integer, x) should return a BigInt +trunc(::Type{Integer}, x::BigFloat) = trunc(BigInt, x) +floor(::Type{Integer}, x::BigFloat) = floor(BigInt, x) +ceil(::Type{Integer}, x::BigFloat) = ceil(BigInt, x) + +for Ti in (Int128,UInt128) @eval begin - function convert(::Type{$to}, x::BigFloat) - (isinteger(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) - convert($to, ccall((:__gmpfr_mpfr_get_uj,:libmpfr), - Cuintmax_t, (Ptr{BigFloat}, Int32), &x, 0)) - end + trunc(::Type{$Ti}, x::BigFloat) = ($Ti)(trunc(BigInt, x)) + floor(::Type{$Ti}, x::BigFloat) = ($Ti)(floor(BigInt, x)) + ceil(::Type{$Ti}, x::BigFloat) = ($Ti)(ceil(BigInt, x)) end end -function Base.BigInt(x::BigFloat) - !isinteger(x) && throw(InexactError()) - return itrunc(x) +convert(::Type{Bool}, x::BigFloat) = (x != 0) +function convert(::Type{BigInt},x::BigFloat) + isinteger(x) || throw(InexactError()) + trunc(BigInt,x) end +Base.BigInt(x::BigFloat) = convert(BigInt,x) + +function convert{T<:Integer}(::Type{T},x::BigFloat) + isinteger(x) || throw(InexactError()) + trunc(T,x) +end + +## BigFloat -> FloatingPoint convert(::Type{Float64}, x::BigFloat) = ccall((:mpfr_get_d,:libmpfr), Float64, (Ptr{BigFloat},Int32), &x, ROUNDING_MODE[end]) convert(::Type{Float32}, x::BigFloat) = @@ -125,8 +171,6 @@ call(::Type{Float64}, x::BigFloat, r::RoundingMode) = call(::Type{Float32}, x::BigFloat, r::RoundingMode) = ccall((:mpfr_get_flt,:libmpfr), Float32, (Ptr{BigFloat},Int32), &x, to_mpfr(r)) -convert(::Type{Integer}, x::BigFloat) = convert(BigInt, x) - promote_rule{T<:Real}(::Type{BigFloat}, ::Type{T}) = BigFloat promote_rule{T<:FloatingPoint}(::Type{BigInt},::Type{T}) = BigFloat promote_rule{T<:FloatingPoint}(::Type{BigFloat},::Type{T}) = BigFloat @@ -653,14 +697,6 @@ for f in (:ceil, :floor, :trunc, :round) end end -function itrunc(x::BigFloat) - z = BigInt() - ccall((:mpfr_get_z, :libmpfr), Int32, (Ptr{BigInt}, Ptr{BigFloat}, Int32), &z, &x, to_mpfr(RoundToZero)) - return z -end - -iround(x::BigFloat) = itrunc(round(x)) - function isinf(x::BigFloat) return ccall((:mpfr_inf_p, :libmpfr), Int32, (Ptr{BigFloat},), &x) != 0 end diff --git a/base/pkg/resolve/maxsum.jl b/base/pkg/resolve/maxsum.jl index 61cc615294b16..504535a9b2c31 100644 --- a/base/pkg/resolve/maxsum.jl +++ b/base/pkg/resolve/maxsum.jl @@ -461,7 +461,7 @@ function maxsum(graph::Graph, msgs::Messages) end if it >= params.nondec_iterations && (it - params.nondec_iterations) % params.dec_interval == 0 - numdec = clamp(ifloor(params.dec_fraction * graph.np), 1, msgs.num_nondecimated) + numdec = clamp(floor(Int,params.dec_fraction * graph.np), 1, msgs.num_nondecimated) decimate(numdec, graph, msgs) if msgs.num_nondecimated == 0 break diff --git a/base/poll.jl b/base/poll.jl index 4f4cfb8113408..9a8aa0bc085a8 100644 --- a/base/poll.jl +++ b/base/poll.jl @@ -250,7 +250,7 @@ function start_watching(t::PollingFileWatcher, interval) associate_julia_struct(t.handle, t) uv_error("start_watching (File)", ccall(:jl_fs_poll_start, Int32, (Ptr{Void},Ptr{UInt8},UInt32), - t.handle, t.file, iround(interval*1000))) + t.handle, t.file, round(UInt32,interval*1000))) end start_watching(f::Function, t::PollingFileWatcher, interval) = (t.cb = f;start_watching(t,interval)) diff --git a/base/primes.jl b/base/primes.jl index c42b62d5e307c..fc9bf4ed0f447 100644 --- a/base/primes.jl +++ b/base/primes.jl @@ -8,7 +8,7 @@ function primesmask(s::AbstractVector{Bool}) n = length(s) n < 2 && return s; s[2] = true n < 3 && return s; s[3] = true - r = ifloor(sqrt(n)) + r = floor(Int,sqrt(n)) for x = 1:r xx = x*x for y = 1:r diff --git a/base/printf.jl b/base/printf.jl index 01330c5a33472..ef21bb0d15261 100644 --- a/base/printf.jl +++ b/base/printf.jl @@ -888,7 +888,7 @@ function ini_dec(x::BigInt, n::Int) ccall(:memset, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), p, '0', n - info[2]) return info end - return (n, d, decode_dec(iround(x/big(10)^(d-n)))[3]) + return (n, d, decode_dec(round(BigInt,x/big(10)^(d-n)))[3]) end diff --git a/base/profile.jl b/base/profile.jl index 66eac14921986..6aecdcb979dd4 100644 --- a/base/profile.jl +++ b/base/profile.jl @@ -33,7 +33,7 @@ function init(; n::Union(Void,Integer) = nothing, delay::Union(Void,Float64) = n end function init(n::Integer, delay::Float64) - status = ccall(:jl_profile_init, Cint, (Csize_t, UInt64), n, iround(10^9*delay)) + status = ccall(:jl_profile_init, Cint, (Csize_t, UInt64), n, round(UInt64,10^9*delay)) if status == -1 error("could not allocate space for ", n, " instruction pointers") end @@ -245,8 +245,8 @@ function print_flat(io::IO, lilist::Vector{LineInfo}, n::Vector{Int}, combine::B wfile = maxfile wfunc = maxfunc else - wfile = ifloor(2*ntext/5) - wfunc = ifloor(3*ntext/5) + wfile = floor(Integer,2*ntext/5) + wfunc = floor(Integer,3*ntext/5) end println(io, lpad("Count", wcounts, " "), " ", rpad("File", wfile, " "), " ", rpad("Function", wfunc, " "), " ", lpad("Line", wline, " ")) for i = 1:length(n) @@ -280,12 +280,12 @@ end tree_format_linewidth(x::LineInfo) = ndigits(x.line)+6 function tree_format(lilist::Vector{LineInfo}, counts::Vector{Int}, level::Int, cols::Integer) - nindent = min(ifloor(cols/2), level) + nindent = min(cols>>1, level) ndigcounts = ndigits(maximum(counts)) ndigline = maximum([tree_format_linewidth(x) for x in lilist]) ntext = cols-nindent-ndigcounts-ndigline-5 - widthfile = ifloor(0.4ntext) - widthfunc = ifloor(0.6ntext) + widthfile = floor(Integer,0.4ntext) + widthfunc = floor(Integer,0.6ntext) strs = Array(ByteString, length(lilist)) showextra = false if level > nindent diff --git a/base/range.jl b/base/range.jl index 7c21201509bff..75ed0e2fec1a7 100644 --- a/base/range.jl +++ b/base/range.jl @@ -105,7 +105,7 @@ function rat(x) b = c = 0 m = maxintfloat(Float32) while abs(y) <= m - f = itrunc(y) + f = trunc(Int,y) y -= f a, c = f*a + c, a b, d = f*b + d, b @@ -415,11 +415,11 @@ function _findin{T1<:Integer, T2<:Integer}(r::Range{T1}, span::UnitRange{T2}) lr = last(r) sr = step(r) if sr > 0 - ifirst = fr >= fspan ? 1 : iceil((fspan-fr)/sr)+1 - ilast = lr <= lspan ? length(r) : length(r) - iceil((lr-lspan)/sr) + ifirst = fr >= fspan ? 1 : ceil(Integer,(fspan-fr)/sr)+1 + ilast = lr <= lspan ? length(r) : length(r) - ceil(Integer,(lr-lspan)/sr) elseif sr < 0 - ifirst = fr <= lspan ? 1 : iceil((lspan-fr)/sr)+1 - ilast = lr >= fspan ? length(r) : length(r) - iceil((lr-fspan)/sr) + ifirst = fr <= lspan ? 1 : ceil(Integer,(lspan-fr)/sr)+1 + ilast = lr >= fspan ? length(r) : length(r) - ceil(Integer,(lr-fspan)/sr) else ifirst = fr >= fspan ? 1 : length(r)+1 ilast = fr <= lspan ? length(r) : 0 @@ -557,7 +557,7 @@ function map!(f::Callable, dest, r::Range) end function in(x, r::Range) - n = step(r) == 0 ? 1 : iround((x-first(r))/step(r))+1 + n = step(r) == 0 ? 1 : round(Integer,(x-first(r))/step(r))+1 n >= 1 && n <= length(r) && r[n] == x end diff --git a/base/rational.jl b/base/rational.jl index 8f16c89c01948..63f901a89b5b1 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -220,26 +220,26 @@ for op in (:div, :fld, :cld) end end -itrunc(x::Rational) = div(x.num,x.den) -ifloor(x::Rational) = fld(x.num,x.den) -iceil (x::Rational) = cld(x.num,x.den) -function iround(x::Rational) - t = itrunc(x) +trunc{T<:Integer}(::Type{T}, x::Rational) = convert(T,div(x.num,x.den)) +floor{T<:Integer}(::Type{T}, x::Rational) = convert(T,fld(x.num,x.den)) +ceil {T<:Integer}(::Type{T}, x::Rational) = convert(T,cld(x.num,x.den)) +function round{T<:Integer}(::Type{T}, x::Rational) + t = trunc(T,x) r = x-t abs(r.num) > (r.den-one(r.den))>>1 ? t + copysign(one(t),x) : t end -trunc(x::Rational) = Rational(itrunc(x)) -floor(x::Rational) = Rational(ifloor(x)) -ceil (x::Rational) = Rational(iceil(x)) -round(x::Rational) = Rational(iround(x)) +trunc{T}(x::Rational{T}) = Rational(trunc(T,x)) +floor{T}(x::Rational{T}) = Rational(floor(T,x)) +ceil {T}(x::Rational{T}) = Rational(ceil(T,x)) +round{T}(x::Rational{T}) = Rational(round(T,x)) ## rational to int coercion ## for f in (:int8, :int16, :int32, :int64, :int128, :uint8, :uint16, :uint32, :uint64, :uint128, :signed, :integer, :unsigned, :int, :uint) - @eval ($f)(x::Rational) = ($f)(iround(x)) + @eval ($f)(x::Rational) = ($f)(round(Integer,x)) end function ^(x::Rational, n::Integer) diff --git a/base/sort.jl b/base/sort.jl index 542646b563f6b..4015816555aa4 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -180,7 +180,7 @@ function searchsortedlast{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, x, first(a)) ? 0 : length(a) else - n = max(min(iround((x-first(a))/step(a))+1,length(a)),1) + n = max(min(round(Integer,(x-first(a))/step(a))+1,length(a)),1) lt(o, x, a[n]) ? n-1 : n end end @@ -189,7 +189,7 @@ function searchsortedfirst{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, first(a), x) ? length(a)+1 : 1 else - n = max(min(iround((x-first(a))/step(a))+1,length(a)),1) + n = max(min(round(Integer,(x-first(a))/step(a))+1,length(a)),1) lt(o, a[n] ,x) ? n+1 : n end end @@ -198,7 +198,7 @@ function searchsortedlast{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, x, first(a)) ? 0 : length(a) else - max(min(fld(ifloor(x)-first(a),step(a))+1,length(a)),0) + max(min(fld(floor(Integer,x)-first(a),step(a))+1,length(a)),0) end end @@ -206,7 +206,7 @@ function searchsortedfirst{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, first(a), x) ? length(a)+1 : 1 else - max(min(-fld(ifloor(-x)+first(a),step(a))+1,length(a)+1),1) + max(min(-fld(floor(Integer,-x)+first(a),step(a))+1,length(a)+1),1) end end diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 2760b67ef3a49..35b030bd50590 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -433,24 +433,46 @@ end # Operations that may map nonzeros to zero, and zero to zero # Result is sparse -for (op, restype) in ((:iceil, Int), (:ceil, Void), - (:ifloor, Int), (:floor, Void), - (:itrunc, Int), (:trunc, Void), - (:iround, Int), (:round, Void), - (:sin, Void), (:tan, Void), - (:sinh, Void), (:tanh, Void), - (:asin, Void), (:atan, Void), - (:asinh, Void), (:atanh, Void), - (:sinpi, Void), (:cosc, Void), - (:sind, Void), (:tand, Void), - (:asind, Void), (:atand, Void) ) +for op in (:ceil, :floor, :trunc, :round, + :sin, :tan, :asin, :atan, + :sinh, :tanh, :asinh, :atanh, + :sinpi, :cosc, + :sind, :tand, :asind, :atand) @eval begin - function ($op){Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}) nfilledA = nnz(A) colptrB = Array(Ti, A.n+1) rowvalB = Array(Ti, nfilledA) - nzvalB = Array($(restype==Void ? (:Tv) : restype), nfilledA) + nzvalB = Array(Tv, nfilledA) + + k = 0 # number of additional zeros introduced by op(A) + @inbounds for i = 1 : A.n + colptrB[i] = A.colptr[i] - k + for j = A.colptr[i] : A.colptr[i+1]-1 + opAj = $(op)(A.nzval[j]) + if opAj == 0 + k += 1 + else + rowvalB[j - k] = A.rowval[j] + nzvalB[j - k] = opAj + end + end + end + colptrB[end] = A.colptr[end] - k + deleteat!(rowvalB, colptrB[end]:nfilledA) + deleteat!(nzvalB, colptrB[end]:nfilledA) + return SparseMatrixCSC(A.m, A.n, colptrB, rowvalB, nzvalB) + end + end # quote +end # macro + +for op in (:ceil, :floor, :trunc, :round) + @eval begin + function ($op){T,Tv,Ti}(::Type{T},A::SparseMatrixCSC{Tv,Ti}) + nfilledA = nnz(A) + colptrB = Array(Ti, A.n+1) + rowvalB = Array(Ti, nfilledA) + nzvalB = Array(T, nfilledA) k = 0 # number of additional zeros introduced by op(A) @inbounds for i = 1 : A.n @@ -474,6 +496,8 @@ for (op, restype) in ((:iceil, Int), (:ceil, Void), end # quote end # macro + + # Operations that map nonzeros to nonzeros, and zeros to zeros # Result is sparse for op in (:-, :abs, :abs2, :log1p, :expm1) diff --git a/base/special/gamma.jl b/base/special/gamma.jl index 09f40f62f06a8..a7438e76afbfb 100644 --- a/base/special/gamma.jl +++ b/base/special/gamma.jl @@ -80,7 +80,7 @@ function digamma(z::Union(Float64,Complex{Float64})) end if x < 7 # shift using recurrence formula - n = 7 - ifloor(x) + n = 7 - floor(Int,x) for ν = 1:n-1 ψ -= inv(z + ν) end @@ -103,7 +103,7 @@ function trigamma(z::Union(Float64,Complex{Float64})) ψ = zero(z) if x < 8 # shift using recurrence formula - n = 8 - ifloor(x) + n = 8 - floor(Int,x) ψ += inv(z)^2 for ν = 1:n-1 ψ += inv(z + ν)^2 @@ -285,7 +285,7 @@ function zeta(s::Union(Int,Float64,Complex{Float64}), throw(DomainError()) # or return NaN? end nx = int(xf) - n = iceil(cutoff - nx) + n = ceil(Int,cutoff - nx) ζ += inv_oftype(ζ, z)^s for ν = -nx:-1:1 ζₒ= ζ @@ -437,7 +437,7 @@ function zeta(s::Union(Float64,Complex{Float64})) # shift using recurrence formula: # n is a semi-empirical cutoff for the Stirling series, based # on the error term ~ (|m|/n)^18 / n^real(m) - n = iceil(6 + 0.7*abs(imag(s-1))^inv(1 + real(m)*0.05)) + n = ceil(Int,6 + 0.7*abs(imag(s-1))^inv(1 + real(m)*0.05)) ζ = one(s) for ν = 2:n ζₒ= ζ diff --git a/base/statistics.jl b/base/statistics.jl index 05f2763f18350..b7400429d7f34 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -490,8 +490,8 @@ function quantile!(v::AbstractVector, q::AbstractVector) lq = length(q) index = 1 .+ (lv-1)*q - lo = ifloor(index) - hi = iceil(index) + lo = floor(Int,index) + hi = ceil(Int,index) sort!(v) isnan(v[end]) && error("quantiles are undefined in presence of NaNs") i = find(index .> lo) @@ -537,7 +537,7 @@ function histrange{T<:FloatingPoint,N}(v::AbstractArray{T,N}, n::Integer) end end start = step*(ceil(lo/step)-1) - nm1 = iceil((hi - start)/step) + nm1 = ceil(Int,(hi - start)/step) start:step:(start + nm1*step) end @@ -550,7 +550,7 @@ function histrange{T<:Integer,N}(v::AbstractArray{T,N}, n::Integer) step = 1 else bw = (hi - lo) / n - e = 10^max(0,ifloor(log10(bw))) + e = 10^max(0,floor(Int,log10(bw))) r = bw / e if r <= 1 step = e @@ -563,7 +563,7 @@ function histrange{T<:Integer,N}(v::AbstractArray{T,N}, n::Integer) end end start = step*(ceil(lo/step)-1) - nm1 = iceil((hi - start)/step) + nm1 = ceil(Int,(hi - start)/step) start:step:(start + nm1*step) end @@ -574,7 +574,7 @@ midpoints(v::AbstractVector) = [0.5*(v[i] + v[i+1]) for i in 1:length(v)-1] ## hist ## function sturges(n) # Sturges' formula n==0 && return one(n) - iceil(log2(n))+1 + ceil(Int,log2(n))+1 end function hist!{HT}(h::AbstractArray{HT}, v::AbstractVector, edg::AbstractVector; init::Bool=true) diff --git a/doc/helpdb.jl b/doc/helpdb.jl index 79e5792457ea5..38e1ad01c9e52 100644 --- a/doc/helpdb.jl +++ b/doc/helpdb.jl @@ -4508,63 +4508,63 @@ popdisplay(d::Display) "), -("Base","round","round(x[, digits[, base]]) +("Base","round","round([T], x[, digits[, base]]) \"round(x)\" returns the nearest integral value of the same type as - \"x\" to \"x\". \"round(x, digits)\" rounds to the specified number - of digits after the decimal place, or before if negative, e.g., - \"round(pi,2)\" is \"3.14\". \"round(x, digits, base)\" rounds - using a different base, defaulting to 10, e.g., \"round(pi, 1, 8)\" - is \"3.125\". + \"x\" to \"x\", breaking ties by rounding away from zero. -"), - -("Base","ceil","ceil(x[, digits[, base]]) + \"round(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. - Returns the nearest integral value of the same type as \"x\" not - less than \"x\". \"digits\" and \"base\" work as above. + \"round(x, digits)\" rounds to the specified number of digits after + the decimal place, or before if negative, e.g., \"round(pi,2)\" is + \"3.14\". \"round(x, digits, base)\" rounds using a different base, + defaulting to 10, e.g., \"round(pi, 1, 8)\" is \"3.125\". "), -("Base","floor","floor(x[, digits[, base]]) +("Base","ceil","ceil([T], x[, digits[, base]]) - Returns the nearest integral value of the same type as \"x\" not - greater than \"x\". \"digits\" and \"base\" work as above. + \"ceil(x)\" returns the nearest integral value of the same type as + \"x\" that is greater than or equal to \"x\". -"), + \"ceil(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. -("Base","trunc","trunc(x[, digits[, base]]) - - Returns the nearest integral value of the same type as \"x\" not - greater in magnitude than \"x\". \"digits\" and \"base\" work as - above. + \"digits\" and \"base\" work as for \"round\". "), -("Base","iround","iround([T], x) -> Integer - - Returns the nearest integer to \"x\", converted to an integer type, - optionally passed as the first argument. +("Base","floor","floor([T], x[, digits[, base]]) -"), + \"floor(x)\" returns the nearest integral value of the same type as + \"x\" that is less than or equal to \"x\". -("Base","iceil","iceil(x) -> Integer + \"floor(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. - Returns the nearest integer not less than \"x\". + \"digits\" and \"base\" work as above. "), -("Base","ifloor","ifloor(x) -> Integer +("Base","trunc","trunc([T], x[, digits[, base]]) + + \"trunc(x)\" returns the nearest integral value of the same type as + \"x\" whose absolute value is less than or equal to \"x\". + + \"trunc(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. - Returns the nearest integer not greater than \"x\". + \"digits\" and \"base\" work as above. "), -("Base","itrunc","itrunc([T], x) -> Integer +("Base","unsafe_trunc","unsafe_trunc(T, x) - Returns the nearest integer not greater in magnitude than \"x\", - converted to an integer type, optionally passed as the first - argument. + \"unsafe_trunc(T, x)\" returns the nearest integral value of type + \"T\" whose absolute value is less than or equal to \"x\". If the + value is not representable by \"T\", an arbitrary value will be + returned. "), @@ -5854,16 +5854,13 @@ popdisplay(d::Display) "), -("Base","rand!","rand!([rng], A) - - Populate the array A with random values. - -"), - -("Base","rand!","rand!([rng], r, A) +("Base","rand!","rand!([rng], A[, coll]) - Populate the array A with random values drawn uniformly from the - range \"r\". + Populate the array A with random values. If the indexable + collection \"coll\" is specified, the values are picked randomly + from \"coll\". This is equivalent to \"copy!(A, rand(rng, coll, + size(A)))\" or \"copy!(A, rand(rng, eltype(A), size(A)))\" but + without allocating a new array. "), @@ -5913,6 +5910,35 @@ popdisplay(d::Display) "), +("Base","eachindex","eachindex(A) + + Creates an iterable object for visiting each multi-dimensional + index of the AbstractArray \"A\". Example for a 2-d array: + + julia> A = rand(2,3) + 2x3 Array{Float64,2}: + 0.960084 0.629326 0.625155 + 0.432588 0.955903 0.991614 + + julia> for iter in eachindex(A) + @show iter.I_1, iter.I_2 + @show A[iter] + end + (iter.I_1,iter.I_2) = (1,1) + A[iter] = 0.9600836263003063 + (iter.I_1,iter.I_2) = (2,1) + A[iter] = 0.4325878255452178 + (iter.I_1,iter.I_2) = (1,2) + A[iter] = 0.6293256402775211 + (iter.I_1,iter.I_2) = (2,2) + A[iter] = 0.9559027084099654 + (iter.I_1,iter.I_2) = (1,3) + A[iter] = 0.6251548453735303 + (iter.I_1,iter.I_2) = (2,3) + A[iter] = 0.9916142534546522 + +"), + ("Base","countnz","countnz(A) Counts the number of nonzero values in array A (dense or sparse). diff --git a/doc/manual/arrays.rst b/doc/manual/arrays.rst index ba3290c95297f..e318e86e1db5c 100644 --- a/doc/manual/arrays.rst +++ b/doc/manual/arrays.rst @@ -352,7 +352,6 @@ elementwise:: exponent eta zeta gamma hankelh1 hankelh2 ceil floor round trunc - iceil ifloor iround itrunc isfinite isinf isnan lbeta lfact lgamma log log10 log1p log2 diff --git a/doc/manual/mathematical-operations.rst b/doc/manual/mathematical-operations.rst index f1f89956d5796..160d4396fc159 100644 --- a/doc/manual/mathematical-operations.rst +++ b/doc/manual/mathematical-operations.rst @@ -362,14 +362,14 @@ Rounding functions ============= ================================== ================= Function Description Return type ============= ================================== ================= -``round(x)`` round ``x`` to the nearest integer ``FloatingPoint`` -``iround(x)`` round ``x`` to the nearest integer ``Integer`` -``floor(x)`` round ``x`` towards ``-Inf`` ``FloatingPoint`` -``ifloor(x)`` round ``x`` towards ``-Inf`` ``Integer`` -``ceil(x)`` round ``x`` towards ``+Inf`` ``FloatingPoint`` -``iceil(x)`` round ``x`` towards ``+Inf`` ``Integer`` -``trunc(x)`` round ``x`` towards zero ``FloatingPoint`` -``itrunc(x)`` round ``x`` towards zero ``Integer`` +``round(x)`` round ``x`` to the nearest integer ``typeof(x)`` +``round(T, x)`` round ``x`` to the nearest integer ``T`` +``floor(x)`` round ``x`` towards ``-Inf`` ``typeof(x)`` +``floor(T, x)`` round ``x`` towards ``-Inf`` ``T`` +``ceil(x)`` round ``x`` towards ``+Inf`` ``typeof(x)`` +``ceil(T, x)`` round ``x`` towards ``+Inf`` ``T`` +``trunc(x)`` round ``x`` towards zero ``typeof(x)`` +``trunc(T, x)`` round ``x`` towards zero ``T`` ============= ================================== ================= Division functions diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 0b7f7a77aa979..6a130fea21b44 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -3174,38 +3174,52 @@ Mathematical Functions Accurately compute :math:`e^x-1` -.. function:: round(x, [digits, [base]]) +.. function:: round([T,] x, [digits, [base]]) - ``round(x)`` returns the nearest integral value of the same type as ``x`` to ``x``. ``round(x, digits)`` rounds to the specified number of digits after the decimal place, or before if negative, e.g., ``round(pi,2)`` is ``3.14``. ``round(x, digits, base)`` rounds using a different base, defaulting to 10, e.g., ``round(pi, 1, 8)`` is ``3.125``. + ``round(x)`` returns the nearest integral value of the same type as ``x`` + to ``x``, breaking ties by rounding away from zero. -.. function:: ceil(x, [digits, [base]]) + ``round(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integral value of the same type as ``x`` not less than ``x``. ``digits`` and ``base`` work as above. + ``round(x, digits)`` rounds to the specified number of digits after the decimal place, or before if negative, e.g., ``round(pi,2)`` is ``3.14``. ``round(x, digits, base)`` rounds using a different base, defaulting to 10, e.g., ``round(pi, 1, 8)`` is ``3.125``. -.. function:: floor(x, [digits, [base]]) +.. function:: ceil([T,] x, [digits, [base]]) - Returns the nearest integral value of the same type as ``x`` not greater than ``x``. ``digits`` and ``base`` work as above. + ``ceil(x)`` returns the nearest integral value of the same type as ``x`` + that is greater than or equal to ``x``. -.. function:: trunc(x, [digits, [base]]) + ``ceil(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integral value of the same type as ``x`` not greater in magnitude than ``x``. ``digits`` and ``base`` work as above. + ``digits`` and ``base`` work as for ``round``. -.. function:: iround([T,]x) -> Integer +.. function:: floor([T,] x, [digits, [base]]) - Returns the nearest integer to ``x``, converted to an integer type, optionally passed as the first argument. + ``floor(x)`` returns the nearest integral value of the same type as ``x`` + that is less than or equal to ``x``. -.. function:: iceil(x) -> Integer + ``floor(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integer not less than ``x``. + ``digits`` and ``base`` work as above. -.. function:: ifloor(x) -> Integer +.. function:: trunc([T,] x, [digits, [base]]) - Returns the nearest integer not greater than ``x``. + ``trunc(x)`` returns the nearest integral value of the same type as ``x`` whose absolute + value is less than or equal to ``x``. -.. function:: itrunc([T,]x) -> Integer + ``trunc(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integer not greater in magnitude than ``x``, converted to an integer type, optionally passed as the first argument. + ``digits`` and ``base`` work as above. +.. function:: unsafe_trunc(T, x) + + ``unsafe_trunc(T, x)`` returns the nearest integral value of type ``T`` whose absolute + value is less than or equal to ``x``. If the value is not representable by + ``T``, an arbitrary value will be returned. + .. function:: signif(x, digits, [base]) Rounds (in the sense of ``round``) ``x`` so that there are ``digits`` significant digits, under a base ``base`` representation, default 10. E.g., ``signif(123.456, 2)`` is ``120.0``, and ``signif(357.913, 4, 2)`` is ``352.0``. diff --git a/src/intrinsics.cpp b/src/intrinsics.cpp index 4e6a478722e15..af69094d14b0b 100644 --- a/src/intrinsics.cpp +++ b/src/intrinsics.cpp @@ -21,7 +21,7 @@ namespace JL_I { fptoui, fptosi, uitofp, sitofp, fptrunc, fpext, // checked conversion - fpsiround, fpuiround, checked_fptosi, checked_fptoui, + checked_fptosi, checked_fptoui, checked_trunc_sint, checked_trunc_uint, check_top_bit, // checked arithmetic checked_sadd, checked_uadd, checked_ssub, checked_usub, @@ -577,115 +577,6 @@ static Value *emit_checked_fptoui(jl_value_t *targ, Value *x, jl_codectx_t *ctx) return emit_checked_fptoui(staticeval_bitstype(targ, "checked_fptoui", ctx), x, ctx); } -static Value *emit_iround(Type *to, Value *x, bool issigned, jl_codectx_t *ctx) -{ - int nmantissa, expoffs, expbits; - int64_t topbit; - Type *intt, *floatt; - Value *bits = JL_INT(x); - Value *max, *min; - int tobits = to->getPrimitiveSizeInBits(); - - if (bits->getType()->getPrimitiveSizeInBits() == 32) { - nmantissa = 23; - expoffs = 127; - expbits = 0xff; - topbit = BIT31; - intt = T_int32; floatt = T_float32; - if (issigned) { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 127.99999); - min = ConstantFP::get(floatt, -128.99998); break; - case 16: max = ConstantFP::get(floatt, 32767.998); - min = ConstantFP::get(floatt, -32768.996); break; - case 32: max = ConstantFP::get(floatt, 2.1474835e9); - min = ConstantFP::get(floatt, -2.1474836e9); break; - case 64: max = ConstantFP::get(floatt, 9.2233715e18); - min = ConstantFP::get(floatt, -9.223372e18); break; - default: - jl_error("unsupported type in fpsiround"); - } - } - else { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 255.99998); break; - case 16: max = ConstantFP::get(floatt, 65535.996); break; - case 32: max = ConstantFP::get(floatt, 4.294967e9); break; - case 64: max = ConstantFP::get(floatt, 1.8446743e19); break; - default: - jl_error("unsupported type in fpuiround"); - } - // most negative number that truncates to zero - min = ConstantFP::get(floatt, -0.99999994); - } - } - else { - nmantissa = 52; - expoffs = 1023; - expbits = 0x7ff; - topbit = BIT63; - intt = T_int64; floatt = T_float64; - if (issigned) { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 127.99999999999999); - min = ConstantFP::get(floatt, -128.99999999999997); break; - case 16: max = ConstantFP::get(floatt, 32767.999999999996); - min = ConstantFP::get(floatt, -32768.99999999999); break; - case 32: max = ConstantFP::get(floatt, 2.1474836479999998e9); - min = ConstantFP::get(floatt, -2.1474836489999995e9); break; - case 64: max = ConstantFP::get(floatt, 9.223372036854775e18); - min = ConstantFP::get(floatt, -9.223372036854776e18); break; - default: - jl_error("unsupported type in fpsiround"); - } - } - else { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 255.99999999999997); break; - case 16: max = ConstantFP::get(floatt, 65535.99999999999); break; - case 32: max = ConstantFP::get(floatt, 4.2949672959999995e9); break; - case 64: max = ConstantFP::get(floatt, 1.844674407370955e19); break; - default: - jl_error("unsupported type in fpuiround"); - } - min = ConstantFP::get(floatt, -0.9999999999999999); - } - } - - // itrunc(x + copysign(0.5,x)) - // values with exponent >= nbits are already integers, and this - // rounding method doesn't always give the right answer there. - x = FP(x); - Value *expo = builder.CreateAShr(bits, ConstantInt::get(intt,nmantissa)); - expo = builder.CreateAnd(expo, ConstantInt::get(intt,expbits)); - Value *isint = builder.CreateICmpSGE(expo, - ConstantInt::get(intt,expoffs+nmantissa)); - Value *half = builder.CreateBitCast(ConstantFP::get(floatt, 0.5), intt); - Value *signedhalf = - builder.CreateOr(half, - builder.CreateAnd(bits, - ConstantInt::get(intt,topbit))); - Value *sum = builder.CreateFAdd(x, - builder.CreateBitCast(signedhalf, floatt)); - - Value *src = builder. - CreateSelect(builder. - CreateOr(isint, builder. - // need to give 0 for -0.5 < x < 0.5 (exponent < -1) - // otherwise iround(prevfloat(0.5)) == 1 - CreateICmpSLT(expo, - ConstantInt::get(intt,expoffs-1))), - x, sum); - - raise_exception_unless(builder.CreateAnd(builder.CreateFCmpOLE(src, max), - builder.CreateFCmpOGE(src, min)), - prepare_global(jlinexacterr_var), ctx); - if (issigned) - return builder.CreateFPToSI(src, to); - else - return builder.CreateFPToUI(src, to); -} - static Value *emit_runtime_pointerref(jl_value_t *e, jl_value_t *i, jl_codectx_t *ctx) { Value *preffunc = @@ -924,20 +815,6 @@ static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs, jl_error("fptosi: wrong number of arguments"); } - case fpsiround: - case fpuiround: - if (nargs == 1) { - Value *x = FP(auto_unbox(args[1], ctx)); - return emit_iround(JL_INTT(x->getType()), x, f == fpsiround, ctx); - } - else if (nargs == 2) { - return emit_iround(Type::getIntNTy(jl_LLVMContext, try_to_determine_bitstype_nbits(args[1],ctx)), - FP(auto_unbox(args[2],ctx)), f == fpsiround, ctx); - } - else { - jl_errorf("%s: wrong number of arguments", f == fpsiround ? "fpsiround" : "fpuiround"); - } - HANDLE(fptrunc,2) return builder.CreateFPTrunc(FP(auto_unbox(args[2],ctx)), FTnbits(try_to_determine_bitstype_nbits(args[1],ctx))); HANDLE(fpext,2) { Value *x = auto_unbox(args[2],ctx); @@ -1365,7 +1242,6 @@ extern "C" void jl_init_intrinsic_functions(void) ADD_I(ctpop_int); ADD_I(ctlz_int); ADD_I(cttz_int); ADD_I(sext_int); ADD_I(zext_int); ADD_I(trunc_int); ADD_I(fptoui); ADD_I(fptosi); - ADD_I(fpsiround); ADD_I(fpuiround); ADD_I(uitofp); ADD_I(sitofp); ADD_I(fptrunc); ADD_I(fpext); ADD_I(abs_float); ADD_I(copysign_float); diff --git a/test/arrayperf.jl b/test/arrayperf.jl index 10d1a849a5f7d..f2931a16747cd 100644 --- a/test/arrayperf.jl +++ b/test/arrayperf.jl @@ -11,7 +11,7 @@ if run_ref sz = ntuple(n_dims,i->lensmall) A = randn(sz) n_el = prod(sz) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r @@ -24,7 +24,7 @@ if run_ref sz = ntuple(n_dims,i->lenbig[n_dims]) A = randn(sz) n_el = prod(sz) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r @@ -40,7 +40,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -53,7 +53,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -69,7 +69,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -82,7 +82,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -96,7 +96,7 @@ if run_ref println("Small arrays:") function randind(len) i = rand(1:6) - indchoices = {1:len,1:iceil(len/2),1:iceil(3*len/4),2:2:len,1:iceil(len/2):len,len:-1:1} + indchoices = {1:len,1:ceil(Int,len/2),1:ceil(Int,3*len/4),2:2:len,1:ceil(Int,len/2):len,len:-1:1} return indchoices[i] end #indsmall = {1:4,1:2,1:3,2:2:4,1:3:4,4:-1:1} @@ -105,7 +105,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i->randind(sz[i])) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) @@ -118,7 +118,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i->randind(sz[i])) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) @@ -135,7 +135,7 @@ if run_assign sz = ntuple(n_dims,i->lensmall) B = zeros(sz) A = randn(sz) - n_r = iceil(n_evals/prod(sz)) + n_r = ceil(Int,n_evals/prod(sz)) print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r B[:] = A @@ -146,7 +146,7 @@ if run_assign sz = ntuple(n_dims,i->lenbig[n_dims]) B = zeros(sz) A = randn(sz) - n_r = iceil(n_evals/prod(sz)) + n_r = ceil(Int,n_evals/prod(sz)) print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r B[:] = A @@ -162,7 +162,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -177,7 +177,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -195,7 +195,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -210,7 +210,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -225,7 +225,7 @@ if run_assign println("Small arrays:") function randind(len) i = rand(1:6) - indchoices = {1:len,1:iceil(len/2),1:iceil(3*len/4),2:2:len,1:iceil(len/2):len,len:-1:1} + indchoices = {1:len,1:ceil(Int,len/2),1:ceil(Int,3*len/4),2:2:len,1:ceil(Int,len/2):len,len:-1:1} return indchoices[i] end #indsmall = {1:4,1:2,1:3,2:2:4,1:3:4,4:-1:1} @@ -235,7 +235,7 @@ if run_assign ind = ntuple(n_dims,i->randind(sz[i])) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) @@ -250,7 +250,7 @@ if run_assign ind = ntuple(n_dims,i->randind(sz[i])) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) diff --git a/test/complex.jl b/test/complex.jl index 9c03aef856d51..1fcd6e0d2433c 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -704,7 +704,7 @@ function logacc(x::Float64,expected::Float64) (x == Inf || x == -Inf) && (return 0) isnan(x) && (return 0) ra = relacc(BigFloat(x),BigFloat(expected)) - max(ifloor(-log2(ra)),0) + max(floor(Int,-log2(ra)),0) end # the robust division algorithm should have 53 or 52 # bits accuracy for each of the hard divisions diff --git a/test/euler.jl b/test/euler.jl index 04fd4f3b29c9b..525cf7db6332b 100644 --- a/test/euler.jl +++ b/test/euler.jl @@ -46,7 +46,7 @@ end @test sum(1:100)^2 - sum((1:100).^2) == 25164150 #7: 104743 -euler7(n) = primes(ifloor(n*log(n*log(n))))[n] +euler7(n) = primes(floor(Int,n*log(n*log(n))))[n] @test euler7(10001) == 104743 #8: 40824 diff --git a/test/file.jl b/test/file.jl index 7d5df16beb5f6..bb373297f3f1e 100644 --- a/test/file.jl +++ b/test/file.jl @@ -96,7 +96,7 @@ rm(c_tmpdir, recursive=true) # This section tests file watchers. # ####################################################################### function test_file_poll(channel,timeout_s) - rc = poll_file(file, iround(timeout_s/10), timeout_s) + rc = poll_file(file, round(Int,timeout_s/10), timeout_s) put!(channel,rc) end diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 6bbafe1740082..bbb8be43c1734 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -210,7 +210,7 @@ for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int) end debug && println("Test forward error [JIN 5705] if this is not a BigFloat") - b = eltyb == Int ? itrunc(Atri*ones(n, 2)) : convert(Matrix{eltyb}, Atri*ones(eltya, n, 2)) + b = eltyb == Int ? trunc(Int,Atri*ones(n, 2)) : convert(Matrix{eltyb}, Atri*ones(eltya, n, 2)) x = Atri \ b γ = n*ε/(1 - n*ε) if eltya != BigFloat diff --git a/test/mpfr.jl b/test/mpfr.jl index 7a05b2528d929..6d7a9f0d14eb8 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -345,21 +345,21 @@ y = BigFloat(42) @test convert(UInt32, y) == 42 @test convert(UInt32, y) == 42 -# iround +# round x = BigFloat(42.42) y = with_bigfloat_precision(256) do BigFloat("9223372036854775809.2324") end z = BigInt("9223372036854775809") -@test iround(x) == 42 -@test iround(y) == z -@test typeof(iround(UInt8, x)) == UInt8 && iround(UInt8, x) == 0x2a -@test typeof(iround(UInt16, x)) == UInt16 && iround(UInt16, x) == 0x2a -@test typeof(iround(UInt32, x)) == UInt32 && iround(UInt32, x) == 0x2a -@test typeof(iround(UInt64, x)) == UInt64 && iround(UInt64, x) == 0x2a -@test typeof(iround(Int64, x)) == Int64 && iround(Int64, x) == 42 -@test typeof(iround(Int, x)) == Int && iround(Int, x) == 42 -@test typeof(iround(UInt, x)) == UInt && iround(UInt, x) == 0x2a +@test round(Integer,x) == 42 +@test round(Integer,y) == z +@test typeof(round(UInt8, x)) == UInt8 && round(UInt8, x) == 0x2a +@test typeof(round(UInt16, x)) == UInt16 && round(UInt16, x) == 0x2a +@test typeof(round(UInt32, x)) == UInt32 && round(UInt32, x) == 0x2a +@test typeof(round(UInt64, x)) == UInt64 && round(UInt64, x) == 0x2a +@test typeof(round(Int64, x)) == Int64 && round(Int64, x) == 42 +@test typeof(round(Int, x)) == Int && round(Int, x) == 42 +@test typeof(round(UInt, x)) == UInt && round(UInt, x) == 0x2a # string representation str = "1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012e+00" @@ -445,7 +445,7 @@ with_bigfloat_precision(53) do @test ldexp(BigFloat(24.5), 0x48) == ldexp(24.5, 72) end -# ceil / iceil / floor / ifloor / trunc / itrunc +# ceil / floor / trunc x = BigFloat("28273.7312487489135135135") y = BigInt(28273) z = BigInt(28274) @@ -459,75 +459,90 @@ c = BigInt("123456789012345678901234567891") @test trunc(x) == y @test typeof(trunc(x)) == BigFloat -@test iceil(x) == z -@test typeof(iceil(x)) == BigInt -@test ifloor(x) == y -@test typeof(ifloor(x)) == BigInt -@test itrunc(x) == y -@test typeof(itrunc(x)) == BigInt - -@test iceil(Int64, x) == int64(z) -@test typeof(iceil(Int64, x)) == Int64 -@test ifloor(Int64, x) == int64(y) -@test typeof(ifloor(Int64, x)) == Int64 -@test itrunc(Int64, x) == int64(y) -@test typeof(itrunc(Int64, x)) == Int64 - -@test iceil(Int32, x) == int32(z) -@test typeof(iceil(Int32, x)) == Int32 -@test ifloor(Int32, x) == int32(y) -@test typeof(ifloor(Int32, x)) == Int32 -@test itrunc(Int32, x) == int32(y) -@test typeof(itrunc(Int32, x)) == Int32 - -@test iceil(Int16, x) == int16(z) -@test typeof(iceil(Int16, x)) == Int16 -@test ifloor(Int16, x) == int16(y) -@test typeof(ifloor(Int16, x)) == Int16 -@test itrunc(Int16, x) == int16(y) -@test typeof(itrunc(Int16, x)) == Int16 - -#@test iceil(Int8, x) == int8(z) -#@test typeof(iceil(Int8, x)) == Int8 -#@test ifloor(Int8, x) == int8(y) -#@test typeof(ifloor(Int8, x)) == Int8 -#@test itrunc(Int8, x) == int8(y) -#@test typeof(itrunc(Int8, x)) == Int8 - -@test iceil(UInt64, x) == uint64(z) -@test typeof(iceil(UInt64, x)) == UInt64 -@test ifloor(UInt64, x) == uint64(y) -@test typeof(ifloor(UInt64, x)) == UInt64 -@test itrunc(UInt64, x) == uint64(y) -@test typeof(itrunc(UInt64, x)) == UInt64 - -@test iceil(UInt32, x) == uint32(z) -@test typeof(iceil(UInt32, x)) == UInt32 -@test ifloor(UInt32, x) == uint32(y) -@test typeof(ifloor(UInt32, x)) == UInt32 -@test itrunc(UInt32, x) == uint32(y) -@test typeof(itrunc(UInt32, x)) == UInt32 - -@test iceil(UInt16, x) == uint16(z) -@test typeof(iceil(UInt16, x)) == UInt16 -@test ifloor(UInt16, x) == uint16(y) -@test typeof(ifloor(UInt16, x)) == UInt16 -@test itrunc(UInt16, x) == uint16(y) -@test typeof(itrunc(UInt16, x)) == UInt16 - -#@test iceil(UInt8, x) == uint8(z) -#@test typeof(iceil(UInt8, x)) == UInt8 -#@test ifloor(UInt8, x) == uint8(y) -#@test typeof(ifloor(UInt8, x)) == UInt8 -#@test itrunc(UInt8, x) == uint8(y) -#@test typeof(itrunc(UInt8, x)) == UInt8 - -@test iceil(a) == c -@test typeof(iceil(a)) == BigInt -@test ifloor(a) == b -@test typeof(ifloor(a)) == BigInt -@test itrunc(a) == b -@test typeof(itrunc(a)) == BigInt +@test ceil(Integer,x) == z +@test typeof(ceil(Integer,x)) == BigInt +@test floor(Integer,x) == y +@test typeof(floor(Integer,x)) == BigInt +@test trunc(Integer,x) == y +@test typeof(trunc(Integer,x)) == BigInt + +@test ceil(Int64, x) == int64(z) +@test typeof(ceil(Int64, x)) == Int64 +@test floor(Int64, x) == int64(y) +@test typeof(floor(Int64, x)) == Int64 +@test trunc(Int64, x) == int64(y) +@test typeof(trunc(Int64, x)) == Int64 + +@test ceil(Int32, x) == int32(z) +@test typeof(ceil(Int32, x)) == Int32 +@test floor(Int32, x) == int32(y) +@test typeof(floor(Int32, x)) == Int32 +@test trunc(Int32, x) == int32(y) +@test typeof(trunc(Int32, x)) == Int32 + +@test ceil(Int16, x) == int16(z) +@test typeof(ceil(Int16, x)) == Int16 +@test floor(Int16, x) == int16(y) +@test typeof(floor(Int16, x)) == Int16 +@test trunc(Int16, x) == int16(y) +@test typeof(trunc(Int16, x)) == Int16 + +#@test ceil(Int8, x) == int8(z) +#@test typeof(ceil(Int8, x)) == Int8 +#@test floor(Int8, x) == int8(y) +#@test typeof(floor(Int8, x)) == Int8 +#@test trunc(Int8, x) == int8(y) +#@test typeof(trunc(Int8, x)) == Int8 + +@test ceil(UInt64, x) == uint64(z) +@test typeof(ceil(UInt64, x)) == UInt64 +@test floor(UInt64, x) == uint64(y) +@test typeof(floor(UInt64, x)) == UInt64 +@test trunc(UInt64, x) == uint64(y) +@test typeof(trunc(UInt64, x)) == UInt64 + +@test ceil(UInt32, x) == uint32(z) +@test typeof(ceil(UInt32, x)) == UInt32 +@test floor(UInt32, x) == uint32(y) +@test typeof(floor(UInt32, x)) == UInt32 +@test trunc(UInt32, x) == uint32(y) +@test typeof(trunc(UInt32, x)) == UInt32 + +@test ceil(UInt16, x) == uint16(z) +@test typeof(ceil(UInt16, x)) == UInt16 +@test floor(UInt16, x) == uint16(y) +@test typeof(floor(UInt16, x)) == UInt16 +@test trunc(UInt16, x) == uint16(y) +@test typeof(trunc(UInt16, x)) == UInt16 + +#@test ceil(UInt8, x) == uint8(z) +#@test typeof(ceil(UInt8, x)) == UInt8 +#@test floor(UInt8, x) == uint8(y) +#@test typeof(floor(UInt8, x)) == UInt8 +#@test trunc(UInt8, x) == uint8(y) +#@test typeof(trunc(UInt8, x)) == UInt8 + +@test ceil(Integer,a) == c +@test typeof(ceil(Integer,a)) == BigInt +@test floor(Integer,a) == b +@test typeof(floor(Integer,a)) == BigInt +@test trunc(Integer,a) == b +@test typeof(trunc(Integer,a)) == BigInt + +@test ceil(Int128,a) == c +@test typeof(ceil(Int128,a)) == Int128 +@test floor(Int128,a) == b +@test typeof(floor(Int128,a)) == Int128 +@test trunc(Int128,a) == b +@test typeof(trunc(Int128,a)) == Int128 + +@test ceil(UInt128,a) == c +@test typeof(ceil(UInt128,a)) == UInt128 +@test floor(UInt128,a) == b +@test typeof(floor(UInt128,a)) == UInt128 +@test trunc(UInt128,a) == b +@test typeof(trunc(UInt128,a)) == UInt128 + # basic arithmetic # Signed addition @@ -789,7 +804,7 @@ f = BigFloat(10)^int32(1000) @test f > i1 @test f > i2 -i3 = itrunc(f) +i3 = trunc(Integer,f) @test i3 == f @test i3+1 > f @test i3+1 >= f diff --git a/test/numbers.jl b/test/numbers.jl index b026429c91f9d..9415304630fdc 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -563,11 +563,7 @@ end for x=int64(2)^53-2:int64(2)^53+5, y=[2.0^53-2 2.0^53-1 2.0^53 2.0^53+2 2.0^53+4] u = uint64(x) - if WORD_SIZE == 64 - @test y == float64(itrunc(y)) - else - @test y == float64(int64(trunc(y))) - end + @test y == float64(trunc(Int64,y)) @test (x==y)==(y==x) @test (x!=y)==!(x==y) @@ -707,6 +703,16 @@ end @test float64(typemax(UInt128)) == 2.0^128 @test float32(typemax(UInt128)) == 2.0f0^128 +# check for double rounding in conversion +@test float64(10633823966279328163822077199654060032) == 1.0633823966279327e37 #0x1p123 +@test float64(10633823966279328163822077199654060033) == 1.063382396627933e37 #nextfloat(0x1p123) +@test float64(-10633823966279328163822077199654060032) == -1.0633823966279327e37 +@test float64(-10633823966279328163822077199654060033) == -1.063382396627933e37 + +# check Float vs Int128 comparisons +@test int128(1e30) == 1e30 +@test int128(1e30)+1 > 1e30 + @test int128(-2.0^127) == typemin(Int128) @test float64(uint128(3.7e19)) == 3.7e19 @test float64(uint128(3.7e30)) == 3.7e30 @@ -1301,46 +1307,45 @@ end for x = 2^53-10:2^53+10 y = float64(x) - i = WORD_SIZE == 64 ? itrunc(y) : int64(trunc(y)) + i = trunc(Int64,y) @test int64(trunc(y)) == i @test int64(round(y)) == i @test int64(floor(y)) == i @test int64(ceil(y)) == i - if WORD_SIZE == 64 - @test iround(y) == i - @test ifloor(y) == i - @test iceil(y) == i - end + + @test round(Int64,y) == i + @test floor(Int64,y) == i + @test ceil(Int64,y) == i end for x = 2^24-10:2^24+10 y = float32(x) - i = itrunc(y) + i = trunc(Int,y) @test int(trunc(y)) == i @test int(round(y)) == i @test int(floor(y)) == i @test int(ceil(y)) == i - @test iround(y) == i - @test ifloor(y) == i - @test iceil(y) == i + @test round(Int,y) == i + @test floor(Int,y) == i + @test ceil(Int,y) == i end -@test_throws InexactError iround(Inf) -@test_throws InexactError iround(NaN) -@test iround(2.5) == 3 -@test iround(-1.9) == -2 -@test_throws InexactError iround(Int64, 9.223372036854776e18) -@test iround(Int64, 9.223372036854775e18) == 9223372036854774784 -@test_throws InexactError iround(Int64, -9.223372036854778e18) -@test iround(Int64, -9.223372036854776e18) == typemin(Int64) -@test_throws InexactError iround(UInt64, 1.8446744073709552e19) -@test iround(UInt64, 1.844674407370955e19) == 0xfffffffffffff800 -@test_throws InexactError iround(Int32, 2.1474836f9) -@test iround(Int32, 2.1474835f9) == 2147483520 -@test_throws InexactError iround(Int32, -2.147484f9) -@test iround(Int32, -2.1474836f9) == typemin(Int32) -@test_throws InexactError iround(UInt32, 4.2949673f9) -@test iround(UInt32, 4.294967f9) == 0xffffff00 +@test_throws InexactError round(Int,Inf) +@test_throws InexactError round(Int,NaN) +@test round(Int,2.5) == 3 +@test round(Int,-1.9) == -2 +@test_throws InexactError round(Int64, 9.223372036854776e18) +@test round(Int64, 9.223372036854775e18) == 9223372036854774784 +@test_throws InexactError round(Int64, -9.223372036854778e18) +@test round(Int64, -9.223372036854776e18) == typemin(Int64) +@test_throws InexactError round(UInt64, 1.8446744073709552e19) +@test round(UInt64, 1.844674407370955e19) == 0xfffffffffffff800 +@test_throws InexactError round(Int32, 2.1474836f9) +@test round(Int32, 2.1474835f9) == 2147483520 +@test_throws InexactError round(Int32, -2.147484f9) +@test round(Int32, -2.1474836f9) == typemin(Int32) +@test_throws InexactError round(UInt32, 4.2949673f9) +@test round(UInt32, 4.294967f9) == 0xffffff00 for n = 1:100 m = 1 @@ -1350,32 +1355,32 @@ for n = 1:100 @test n == m end -@test iround(UInt,-0.0) == 0 -@test iround(Int,-0.0) == 0 +@test round(UInt,-0.0) == 0 +@test round(Int,-0.0) == 0 -@test iround(Int, 0.5) == 1 -@test iround(Int, prevfloat(0.5)) == 0 -@test iround(Int, -0.5) == -1 -@test iround(Int, nextfloat(-0.5)) == 0 +@test round(Int, 0.5) == 1 +@test round(Int, prevfloat(0.5)) == 0 +@test round(Int, -0.5) == -1 +@test round(Int, nextfloat(-0.5)) == 0 -@test iround(UInt, 0.5) == 1 -@test iround(UInt, prevfloat(0.5)) == 0 -@test_throws InexactError iround(UInt, -0.5) -@test iround(UInt, nextfloat(-0.5)) == 0 +@test round(UInt, 0.5) == 1 +@test round(UInt, prevfloat(0.5)) == 0 +@test_throws InexactError round(UInt, -0.5) +@test round(UInt, nextfloat(-0.5)) == 0 -@test iround(Int, 0.5f0) == 1 -@test iround(Int, prevfloat(0.5f0)) == 0 -@test iround(Int, -0.5f0) == -1 -@test iround(Int, nextfloat(-0.5f0)) == 0 +@test round(Int, 0.5f0) == 1 +@test round(Int, prevfloat(0.5f0)) == 0 +@test round(Int, -0.5f0) == -1 +@test round(Int, nextfloat(-0.5f0)) == 0 -@test iround(UInt, 0.5f0) == 1 -@test iround(UInt, prevfloat(0.5f0)) == 0 -@test_throws InexactError iround(UInt, -0.5f0) -@test iround(UInt, nextfloat(-0.5f0)) == 0 +@test round(UInt, 0.5f0) == 1 +@test round(UInt, prevfloat(0.5f0)) == 0 +@test_throws InexactError round(UInt, -0.5f0) +@test round(UInt, nextfloat(-0.5f0)) == 0 # numbers that can't be rounded by trunc(x+0.5) -@test iround(Int64, 2.0^52 + 1) == 4503599627370497 -@test iround(Int32, 2.0f0^23 + 1) == 8388609 +@test round(Int64, 2.0^52 + 1) == 4503599627370497 +@test round(Int32, 2.0f0^23 + 1) == 8388609 # binary literals @@ -1982,11 +1987,11 @@ end # issue #7441 @test_throws InexactError int32(2.0^50) -@test_throws InexactError iround(UInt8, 255.5) -@test iround(UInt8, 255.4) === 0xff +@test_throws InexactError round(UInt8, 255.5) +@test round(UInt8, 255.4) === 0xff -@test_throws InexactError iround(Int16, -32768.7) -@test iround(Int16, -32768.1) === int16(-32768) +@test_throws InexactError round(Int16, -32768.7) +@test round(Int16, -32768.1) === int16(-32768) # issue #7508 @test_throws ErrorException reinterpret(Int, 0x01) diff --git a/test/random.jl b/test/random.jl index 0e5b7105f7501..3bf0d87fec087 100644 --- a/test/random.jl +++ b/test/random.jl @@ -114,7 +114,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # defines this as # k_0 = 2^31 * r * f(r) / v, w_0 = 0.5^31 * v / f(r), f_0 = 1, # where v is the area of each strip of the ziggurat. - ki[1] = uint64(itrunc(x1*fib[256]/nor_section_area*nmantissa)) + ki[1] = trunc(UInt64,x1*fib[256]/nor_section_area*nmantissa) wib[1] = nor_section_area/fib[256]/nmantissa fib[1] = one(BigFloat) @@ -122,7 +122,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus # need inverse operator of y = exp(-0.5*x*x) -> x = sqrt(-2*ln(y)) x = sqrt(-2.0*log(nor_section_area/x1 + fib[i+1])) - ki[i+1] = uint64(itrunc(x/x1*nmantissa)) + ki[i+1] = trunc(UInt64,x/x1*nmantissa) wib[i] = x/nmantissa fib[i] = exp(-0.5*x*x) x1 = x @@ -139,7 +139,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # defines this as # k_0 = 2^32 * r * f(r) / v, w_0 = 0.5^32 * v / f(r), f_0 = 1, # where v is the area of each strip of the ziggurat. - ke[1] = uint64(itrunc(x1*feb[256]/exp_section_area*emantissa)) + ke[1] = trunc(UInt64,x1*feb[256]/exp_section_area*emantissa) web[1] = exp_section_area/feb[256]/emantissa feb[1] = one(BigFloat) @@ -147,7 +147,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus # need inverse operator of y = exp(-x) -> x = -ln(y) x = -log(exp_section_area/x1 + feb[i+1]) - ke[i+1] = uint64(itrunc(x/x1*emantissa)) + ke[i+1] = trunc(UInt64,x/x1*emantissa) web[i] = x/emantissa feb[i] = exp(-x) x1 = x diff --git a/test/rounding.jl b/test/rounding.jl index 65c840c40cfeb..38e26d38aa137 100644 --- a/test/rounding.jl +++ b/test/rounding.jl @@ -105,24 +105,22 @@ for v = [sqrt(2),-1/3,nextfloat(1.0),prevfloat(1.0),nextfloat(-1.0), @test pu - pd == eps(pz) end -# TODO: add UInt128 check -# Float vs UInt128 comparisons are currently broken, e.g. Inf32 == typemax(UInt128) for T in [Float32,Float64] for v in [sqrt(big(2.0)),-big(1.0)/big(3.0),nextfloat(big(1.0)), prevfloat(big(1.0)),nextfloat(big(0.0)),prevfloat(big(0.0)), pi,e,eulergamma,catalan,golden, - typemax(Int64),typemax(UInt64),typemax(Int128),0xa2f30f6001bb2ec6] + typemax(Int64),typemax(UInt64),typemax(Int128),typemax(UInt128),0xa2f30f6001bb2ec6] pn = T(v,RoundNearest) - @test pn == convert(T,v) + @test pn == convert(T,BigFloat(v)) pz = T(v,RoundToZero) - @test pz == with_rounding(()->convert(T,v), BigFloat, RoundToZero) + @test pz == with_rounding(()->convert(T,BigFloat(v)), BigFloat, RoundToZero) pd = T(v,RoundDown) - @test pd == with_rounding(()->convert(T,v), BigFloat, RoundDown) + @test pd == with_rounding(()->convert(T,BigFloat(v)), BigFloat, RoundDown) pu = T(v,RoundUp) - @test pu == with_rounding(()->convert(T,v), BigFloat, RoundUp) + @test pu == with_rounding(()->convert(T,BigFloat(v)), BigFloat, RoundUp) @test pn == pd || pn == pu @test v > 0 ? pz == pd : pz == pu - @test pu - pd == eps(pz) + @test isinf(pu) || pu - pd == eps(pz) end end diff --git a/test/sparse.jl b/test/sparse.jl index 5d3ae411fd539..f03efcf4a4944 100644 --- a/test/sparse.jl +++ b/test/sparse.jl @@ -243,12 +243,19 @@ end # Unary functions a = sprand(5,15, 0.5) afull = full(a) -for op in (:sin, :cos, :tan, :iceil, :ifloor, :ceil, :floor, :abs, :abs2) +for op in (:sin, :cos, :tan, :ceil, :floor, :abs, :abs2) @eval begin @test ($op)(afull) == full($(op)(a)) end end +for op in (:ceil, :floor) + @eval begin + @test ($op)(Int,afull) == full($(op)(Int,a)) + end +end + + # getindex tests ni = 23 nj = 32 diff --git a/test/strings.jl b/test/strings.jl index d5dc665611888..5716ce1148139 100644 --- a/test/strings.jl +++ b/test/strings.jl @@ -771,7 +771,7 @@ n = 3 a = [3,1,2] @test """$(a[2])""" == "1" @test """$(a[3]+7)""" == "9" -@test """$(ifloor(4.5))""" == "4" +@test """$(floor(Int,4.5))""" == "4" nl = " " @test """