0.5 how to convert to Int

711 views
Skip to first unread message

program...@gmail.com

unread,
Oct 27, 2016, 3:36:59 AM10/27/16
to julia-users
in ver 0.5 how to convert to Int f.e bool or Float

julia> int(rand(Bool,10))
ERROR: UndefVarError: int not defined

julia> convert(Int64,rand(Bool,10))
ERROR: MethodError: Cannot `convert` an object of type Array{Bool,1} to an object of type Int64
This may have arisen from a call to the constructor Int64(...),
since type constructors fall back to convert methods.

Paul

Mauro

unread,
Oct 27, 2016, 3:45:36 AM10/27/16
to julia...@googlegroups.com
you must have missed Julia 0.4 which told you:

_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.4.7 (2016-09-18 16:17 UTC)
_/ |\__'_|_|_|\__'_| |
|__/ | x86_64-pc-linux-gnu

julia> int(4.0)
WARNING: int(x::AbstractFloat) is deprecated, use round(Int,x) instead.
in depwarn at deprecated.jl:73
in int at deprecated.jl:50
while loading no file, in expression starting on line 0
4
Maybe the nicest now is



_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.5.0 (2016-09-19 18:14 UTC)
_/ |\__'_|_|_|\__'_| |
|__/ | x86_64-pc-linux-gnu

julia> Int.(rand(Bool, 4))
4-element Array{Int64,1}:
0
1
0
1

Angel de Vicente

unread,
Oct 27, 2016, 4:00:42 AM10/27/16
to julia...@googlegroups.com
Hi,
just learning Julia, so probably there are better ways, but this works:

,----
| julia> VERSION
| v"0.5.0"
|
| julia> Int(rand(Bool))
| 1
|
| julia> [Int(x) for x in rand(Bool,5)]
| 5-element Array{Int64,1}:
| 0
| 1
| 0
| 1
| 0
|
| julia>
`----

--
Ángel de Vicente
http://www.iac.es/galeria/angelv/

program...@gmail.com

unread,
Oct 27, 2016, 4:14:52 AM10/27/16
to julia-users
BIG THX, but what about Float array ?
julia> Int.(rand(Bool,10))
10-element Array{Int64,1}:
 1
 0
 1
 1
 1
 0
 1
 0
 0
 0

julia> Int.(rand(10))
ERROR: InexactError()
 in macro expansion at .\broadcast.jl:129 [inlined]
 in macro expansion at .\simdloop.jl:73 [inlined]
 in macro expansion at .\broadcast.jl:123 [inlined]
 in _broadcast!(::Type{Int64}, ::Array{Int64,1}, ::Tuple{Tuple{Bool}}, ::Tuple{Tuple{Int64}}, ::Tuple
 ::Type{Val{1}}) at .\broadcast.jl:117
 in broadcast!(::Type{T}, ::Array{Int64,1}, ::Array{Float64,1}) at .\broadcast.jl:172
 in broadcast_t(::Type{T}, ::Type{T}, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N}) at .\broadcas
 in broadcast(::Type{T}, ::Array{Float64,1}) at .\broadcast.jl:230

Angel de Vicente

unread,
Oct 27, 2016, 4:21:39 AM10/27/16
to julia...@googlegroups.com


program...@gmail.com writes:

> BIG THX, but what about Float array ?

map(x->round(Int,x),rand(Bool,5))
map(x->round(Int,x),rand(5))

both do work.

Steven G. Johnson

unread,
Oct 27, 2016, 10:52:39 AM10/27/16
to julia-users


On Thursday, October 27, 2016 at 4:21:39 AM UTC-4, Ángel de Vicente wrote:


program...@gmail.com writes:

> BIG THX, but what about Float array ?

round(Int, rand(5)) also works.  (In 0.6 this will probably be deprecated in favor of round.(Int, rand(5)) ...)
Reply all
Reply to author
Forward
0 new messages