Skip to content

Commit d17c909

Browse files
committed
itrunc -> trunc, etc, improve iround
1 parent 04271cd commit d17c909

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+576
-581
lines changed

base/abstractarray.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real)
13851385
empty!(S)
13861386
p == 0 && return S
13871387
nexpected = p * length(A)
1388-
sizehint(S, iround(nexpected + 5*sqrt(nexpected)))
1388+
sizehint(S, round(Int,nexpected + 5*sqrt(nexpected)))
13891389
if p > 0.15 # empirical threshold for trivial O(n) algorithm to be better
13901390
for i = 1:n
13911391
rand() <= p && push!(S, A[i])
@@ -1396,14 +1396,14 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real)
13961396
# s==k (k > 0) is (1-p)^(k-1) * p, and hence the probability (CDF) that
13971397
# s is in {1,...,k} is 1-(1-p)^k = F(k). Thus, we can draw the skip s
13981398
# from this probability distribution via the discrete inverse-transform
1399-
# method: s = iceil(F^{-1}(u)) where u = rand(), which is simply
1400-
# s = iceil(log(rand()) / log1p(-p)).
1399+
# method: s = ceil(F^{-1}(u)) where u = rand(), which is simply
1400+
# s = ceil(log(rand()) / log1p(-p)).
14011401
L = 1 / log1p(-p)
14021402
i = 0
14031403
while true
14041404
s = log(rand()) * L # note that rand() < 1, so s > 0
1405-
s >= n - i && return S # compare before iceil to avoid overflow
1406-
push!(S, A[i += iceil(s)])
1405+
s >= n - i && return S # compare before ceil to avoid overflow
1406+
push!(S, A[i += ceil(Int,s)])
14071407
end
14081408
# [This algorithm is similar in spirit to, but much simpler than,
14091409
# the one by Vitter for a related problem in "Faster methods for

base/bitarray.jl

+1-4
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,7 @@ function empty!(B::BitVector)
790790
end
791791

792792
## Misc functions
793-
794-
for f in (:iround, :itrunc, :ifloor, :iceil, :abs)
795-
@eval ($f)(B::BitArray) = copy(B)
796-
end
793+
abs(B::BitArray) = copy(B)
797794

798795
## Unary operators ##
799796

base/char.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
char(x) = convert(Char, x)
2-
char(x::FloatingPoint) = char(iround(x))
2+
char(x::FloatingPoint) = char(round(UInt32,x))
33

44
integer(x::Char) = int(x)
55

base/complex.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ function ^{T<:Complex}(z::T, p::T)
477477

478478
# apply some corrections to force known zeros
479479
if pim == 0
480-
ip = itrunc(pr)
480+
ip = trunc(pr)
481481
if ip == pr
482482
if zi == 0
483483
im = copysign(zero(im), im)
@@ -490,7 +490,7 @@ function ^{T<:Complex}(z::T, p::T)
490490
end
491491
else
492492
dr = pr*2
493-
ip = itrunc(dr)
493+
ip = trunc(dr)
494494
if ip == dr && zi == 0
495495
if zr < 0
496496
re = copysign(zero(re), re)

base/darray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ end
9494
# get array of start indexes for dividing sz into nc chunks
9595
function defaultdist(sz::Int, nc::Int)
9696
if sz >= nc
97-
iround(linspace(1, sz+1, nc+1))
97+
round(Int,linspace(1, sz+1, nc+1))
9898
else
9999
[[1:(sz+1)], zeros(Int, nc-sz)]
100100
end

base/dates/ranges.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Base.colon{T<:DateTime}(start::T, stop::T) = StepRange(start, Day(1), stop)
55

66
# Given a start and end date, how many steps/periods are in between
7-
guess(a::DateTime,b::DateTime,c) = ifloor(Int64,(int128(b) - int128(a))/toms(c))
7+
guess(a::DateTime,b::DateTime,c) = floor(Int64,(int128(b) - int128(a))/toms(c))
88
guess(a::Date,b::Date,c) = int64(div(int64(b - a),days(c)))
99
function len(a,b,c)
1010
lo, hi, st = min(a,b), max(a,b), abs(c)

base/deprecated.jl

+9
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,12 @@ const Uint128 = UInt128
231231

232232
@deprecate rand!(r::Range, A::AbstractArray) rand!(A, r)
233233
@deprecate rand!(mt::MersenneTwister, r::Range, A::AbstractArray) rand!(mt, A, r)
234+
235+
@deprecate itrunc(x) trunc(Integer,x)
236+
@deprecate itrunc{T<:Integer}(::Type{T},x::Real) trunc(T,x)
237+
@deprecate iceil(x) ceil(Integer,x)
238+
@deprecate iceil{T}(::Type{T},x) ceil(T,x)
239+
@deprecate ifloor(x) floor(Integer,x)
240+
@deprecate ifloor{T}(::Type{T},x) floor(T,x)
241+
@deprecate iround(x) round(Integer,x)
242+
@deprecate iround{T}(::Type{T},x) round(T,x)

base/exports.jl

-4
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,6 @@ export
360360
gcdx,
361361
hex2num,
362362
hypot,
363-
iceil,
364-
ifloor,
365363
imag,
366364
int,
367365
int128,
@@ -373,7 +371,6 @@ export
373371
inv,
374372
invdigamma,
375373
invmod,
376-
iround,
377374
isapprox,
378375
iseltype,
379376
iseven,
@@ -387,7 +384,6 @@ export
387384
isqrt,
388385
isreal,
389386
issubnormal,
390-
itrunc,
391387
lcm,
392388
ldexp,
393389
leading_ones,

base/float.jl

+53-58
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
## conversions to floating-point ##
2-
3-
convert(::Type{Float32}, x::Int128) = float32(reinterpret(UInt128,abs(x)))*(1-2(x<0))
4-
convert(::Type{Float32}, x::UInt128) = float32(uint64(x&0xffffffffffffffff)) + ldexp(float32(uint64(x>>>64)),64)
5-
promote_rule(::Type{Float32}, ::Type{Int128} ) = Float32
6-
promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32
7-
8-
convert(::Type{Float64}, x::Int128) = float64(reinterpret(UInt128,abs(x)))*(1-2(x<0))
9-
convert(::Type{Float64}, x::UInt128) = float64(uint64(x&0xffffffffffffffff)) + ldexp(float64(uint64(x>>>64)),64)
10-
promote_rule(::Type{Float64}, ::Type{Int128} ) = Float64
11-
promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64
12-
132
convert(::Type{Float16}, x::Integer) = convert(Float16, convert(Float32,x))
143
for t in (Bool,Char,Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64)
154
@eval promote_rule(::Type{Float16}, ::Type{$t}) = Float32
165
end
176

187
for t1 in (Float32,Float64)
19-
for st in (Int8,Int16,Int32,Int64)
8+
for st in (Int8,Int16,Int32,Int64,Int128)
209
@eval begin
2110
convert(::Type{$t1},x::($st)) = box($t1,sitofp($t1,unbox($st,x)))
2211
promote_rule(::Type{$t1}, ::Type{$st} ) = $t1
2312
end
2413
end
25-
for ut in (Bool,Char,UInt8,UInt16,UInt32,UInt64)
14+
for ut in (Bool,Char,UInt8,UInt16,UInt32,UInt64,UInt128)
2615
@eval begin
2716
convert(::Type{$t1},x::($ut)) = box($t1,uitofp($t1,unbox($ut,x)))
2817
promote_rule(::Type{$t1}, ::Type{$ut} ) = $t1
@@ -55,55 +44,39 @@ float32(x) = convert(Float32, x)
5544
float64(x) = convert(Float64, x)
5645
float(x) = convert(FloatingPoint, x)
5746

58-
## conversions from floating-point ##
59-
60-
# fallbacks using only convert, trunc, ceil, floor, round
61-
itrunc(x::FloatingPoint) = convert(Integer,trunc(x))
62-
iceil (x::FloatingPoint) = convert(Integer,ceil(x)) # TODO: fast primitive for iceil
63-
ifloor(x::FloatingPoint) = convert(Integer,floor(x)) # TOOD: fast primitive for ifloor
64-
iround(x::FloatingPoint) = convert(Integer,round(x))
65-
66-
itrunc{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,trunc(x))
67-
iceil {T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,ceil(x))
68-
ifloor{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,floor(x))
69-
iround{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,round(x))
70-
71-
## fast specific type conversions ##
72-
73-
iround(x::Float32) = iround(Int, x)
74-
iround(x::Float64) = iround(Int, x)
75-
itrunc(x::Float32) = itrunc(Int, x)
76-
itrunc(x::Float64) = itrunc(Int, x)
77-
78-
for to in (Int8, Int16, Int32, Int64)
47+
for Ti in (Int8, Int16, Int32, Int64, Int128)
7948
@eval begin
80-
iround(::Type{$to}, x::Float32) = box($to,fpsiround($to,unbox(Float32,x)))
81-
iround(::Type{$to}, x::Float64) = box($to,fpsiround($to,unbox(Float64,x)))
82-
itrunc(::Type{$to}, x::Float32) = box($to,fptosi($to,unbox(Float32,x)))
83-
itrunc(::Type{$to}, x::Float64) = box($to,fptosi($to,unbox(Float64,x)))
49+
unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptosi($Ti,unbox(Float32,x)))
50+
unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptosi($Ti,unbox(Float64,x)))
8451
end
8552
end
86-
87-
for to in (UInt8, UInt16, UInt32, UInt64)
53+
for Ti in (UInt8, UInt16, UInt32, UInt64, UInt128)
8854
@eval begin
89-
iround(::Type{$to}, x::Float32) = box($to,fpuiround($to,unbox(Float32,x)))
90-
iround(::Type{$to}, x::Float64) = box($to,fpuiround($to,unbox(Float64,x)))
91-
itrunc(::Type{$to}, x::Float32) = box($to,fptoui($to,unbox(Float32,x)))
92-
itrunc(::Type{$to}, x::Float64) = box($to,fptoui($to,unbox(Float64,x)))
55+
unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptoui($Ti,unbox(Float32,x)))
56+
unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptoui($Ti,unbox(Float64,x)))
9357
end
9458
end
9559

96-
iround(::Type{Int128}, x::Float32) = convert(Int128,round(x))
97-
iround(::Type{Int128}, x::Float64) = convert(Int128,round(x))
98-
iround(::Type{UInt128}, x::Float32) = convert(UInt128,round(x))
99-
iround(::Type{UInt128}, x::Float64) = convert(UInt128,round(x))
60+
# matches convert methods
61+
# also determines floor, ceil, round
62+
trunc(::Type{Signed}, x::Float32) = trunc(Int,x)
63+
trunc(::Type{Signed}, x::Float64) = trunc(Int,x)
64+
trunc(::Type{Unsigned}, x::Float32) = trunc(UInt,x)
65+
trunc(::Type{Unsigned}, x::Float64) = trunc(UInt,x)
66+
trunc(::Type{Integer}, x::Float32) = trunc(Int,x)
67+
trunc(::Type{Integer}, x::Float64) = trunc(Int,x)
68+
69+
# fallbacks
70+
floor{T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,floor(x))
71+
ceil {T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,ceil(x))
72+
round {T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,round(x))
73+
10074

10175
# this is needed very early because it is used by Range and colon
10276
round(x::Float64) = ccall((:round, Base.libm_name), Float64, (Float64,), x)
10377
floor(x::Float64) = ccall((:floor, Base.libm_name), Float64, (Float64,), x)
10478

10579
## floating point promotions ##
106-
10780
promote_rule(::Type{Float32}, ::Type{Float16}) = Float32
10881
promote_rule(::Type{Float64}, ::Type{Float16}) = Float64
10982
promote_rule(::Type{Float64}, ::Type{Float32}) = Float64
@@ -112,7 +85,6 @@ widen(::Type{Float16}) = Float32
11285
widen(::Type{Float32}) = Float64
11386

11487
## floating point arithmetic ##
115-
11688
-(x::Float32) = box(Float32,neg_float(unbox(Float32,x)))
11789
-(x::Float64) = box(Float64,neg_float(unbox(Float64,x)))
11890

@@ -128,7 +100,6 @@ widen(::Type{Float32}) = Float64
128100
# TODO: faster floating point div?
129101
# TODO: faster floating point fld?
130102
# TODO: faster floating point mod?
131-
132103
rem(x::Float32, y::Float32) = box(Float32,rem_float(unbox(Float32,x),unbox(Float32,y)))
133104
rem(x::Float64, y::Float64) = box(Float64,rem_float(unbox(Float64,x),unbox(Float64,y)))
134105

@@ -147,7 +118,6 @@ end
147118

148119

149120
## floating point comparisons ##
150-
151121
==(x::Float32, y::Float32) = eq_float(unbox(Float32,x),unbox(Float32,y))
152122
==(x::Float64, y::Float64) = eq_float(unbox(Float64,x),unbox(Float64,y))
153123
!=(x::Float32, y::Float32) = ne_float(unbox(Float32,x),unbox(Float32,y))
@@ -177,31 +147,31 @@ function cmp(x::FloatingPoint, y::Real)
177147
ifelse(x<y, -1, ifelse(x>y, 1, 0))
178148
end
179149

180-
for Ti in (Int64,UInt64)
150+
for Ti in (Int64,UInt64,Int128,UInt128)
181151
for Tf in (Float32,Float64)
182152
@eval begin
183153
function ==(x::$Tf, y::$Ti)
184154
fy = ($Tf)(y)
185-
(x == fy) & (y == itrunc($Ti,fy))
155+
(x == fy) & (y == unsafe_trunc($Ti,fy))
186156
end
187157
==(y::$Ti, x::$Tf) = x==y
188158

189159
function <(x::$Ti, y::$Tf)
190160
fx = ($Tf)(x)
191-
(fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < itrunc($Ti,fx)) ))
161+
(fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) ))
192162
end
193163
function <=(x::$Ti, y::$Tf)
194164
fx = ($Tf)(x)
195-
(fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= itrunc($Ti,fx)) ))
165+
(fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) ))
196166
end
197167

198168
function <(x::$Tf, y::$Ti)
199169
fy = ($Tf)(y)
200-
(x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) < y))
170+
(x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y))
201171
end
202172
function <=(x::$Tf, y::$Ti)
203173
fy = ($Tf)(y)
204-
(x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) <= y))
174+
(x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y))
205175
end
206176
end
207177
end
@@ -263,6 +233,31 @@ nextfloat(x::Float64, i::Integer) =
263233
nextfloat(x::FloatingPoint) = nextfloat(x,1)
264234
prevfloat(x::FloatingPoint) = nextfloat(x,-1)
265235

236+
for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
237+
for Tf in (Float32, Float64)
238+
if sizeof(Ti) < sizeof(Tf) || Ti <: Unsigned # Tf(typemin(Ti))-1 is exact
239+
@eval function trunc(::Type{$Ti},x::$Tf)
240+
$(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf)) || throw(InexactError())
241+
unsafe_trunc($Ti,x)
242+
end
243+
else
244+
@eval function trunc(::Type{$Ti},x::$Tf)
245+
$(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))) || throw(InexactError())
246+
unsafe_trunc($Ti,x)
247+
end
248+
end
249+
end
250+
end
251+
252+
# adding prevfloat(0.5) will prevent prevfloat(0.5) and odd x with eps(x)=1.0
253+
# from rounding in the wrong direction in RoundToNearest
254+
for Tf in (Float32,Float64)
255+
@eval function round{T<:Integer}(::Type{T}, x::$Tf)
256+
trunc(T,x+copysign($(prevfloat(Tf(0.5))),x))
257+
end
258+
end
259+
260+
266261
@eval begin
267262
issubnormal(x::Float32) = (abs(x) < $(box(Float32,unbox(UInt32,0x00800000)))) & (x!=0)
268263
issubnormal(x::Float64) = (abs(x) < $(box(Float64,unbox(UInt64,0x0010000000000000)))) & (x!=0)

base/float16.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ convert(::Type{UInt128}, x::Float16) = convert(UInt128, float32(x))
102102

103103
convert{T<:Integer}(::Type{T}, x::Float16) = convert(T, float32(x))
104104

105-
iround{T<:Integer}(::Type{T}, x::Float16) = iround(T, float32(x))
106-
itrunc{T<:Integer}(::Type{T}, x::Float16) = itrunc(T, float32(x))
105+
round{T<:Integer}(::Type{T}, x::Float16) = round(T, float32(x))
106+
trunc{T<:Integer}(::Type{T}, x::Float16) = trunc(T, float32(x))
107+
floor{T<:Integer}(::Type{T}, x::Float16) = floor(T, float32(x))
108+
ceil {T<:Integer}(::Type{T}, x::Float16) = ceil(T, float32(x))
107109

108110
round(x::Float16) = float16(round(float32(x)))
109111
trunc(x::Float16) = float16(trunc(float32(x)))

base/floatfuncs.jl

+14-8
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ function hex2num(s::AbstractString)
3535
return box(Float64,unbox(Int64,parseint(Int64,s,16)))
3636
end
3737

38-
@vectorize_1arg Real iround
39-
@vectorize_1arg Real itrunc
40-
@vectorize_1arg Real ifloor
41-
@vectorize_1arg Real iceil
42-
4338
@vectorize_1arg Number abs
4439
@vectorize_1arg Number abs2
4540
@vectorize_1arg Number angle
@@ -48,9 +43,20 @@ end
4843
@vectorize_1arg Number isinf
4944
@vectorize_1arg Number isfinite
5045

51-
iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R,1}) = [ iround(T, x[i]) for i = 1:length(x) ]
52-
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) ]
53-
iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R}) = reshape([ iround(T, x[i]) for i = 1:length(x) ], size(x))
46+
for f in (:trunc,:floor,:ceil,:round)
47+
@eval begin
48+
function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R,1})
49+
[ ($f)(T, x[i]) for i = 1:length(x) ]
50+
end
51+
function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R,2})
52+
[ ($f)(T, x[i,j]) for i = 1:size(x,1), j = 1:size(x,2) ]
53+
end
54+
function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R})
55+
reshape([ ($f)(T, x[i]) for i = 1:length(x) ], size(x))
56+
end
57+
end
58+
end
59+
5460

5561
# adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212
5662
# for round, og is the power of 10 relative to the decimal point

base/grisu/float.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ const MinDecimalExponent = -348
219219
const MaxDecimalExponent = 340
220220

221221
function binexp_cache(min_exponent,max_exponent)
222-
k = iceil((min_exponent+63)*D_1_LOG2_10)
222+
k = ceil(Integer,(min_exponent+63)*D_1_LOG2_10)
223223
index = div(CachedPowersOffset+k-1,DecimalExponentDistance) + 1
224224
cp = CachedPowers[index+1]
225225
return cp

0 commit comments

Comments
 (0)