I found that I often have to force this conversion, which is not too difficult. The question why comprehension has to build with type Any?

On 2014年11月04日 07:06, Miguel Bazdresch wrote:
> How could I force the type of gxs1 to be of an array of Float64?

The simplest way is:

gxs1 = Float64[g(x) for x in xs]

-- mb

On Mon, Nov 3, 2014 at 6:01 PM, Evan Pu <[email protected] <mailto:[email protected]>> wrote:

    Consider the following interaction:

    julia> g(x) = 1 / (1 + x)
    g (generic function with 1 method)

    julia> typeof(g(1.0))
    Float64

    julia> xs = [1.0, 2.0, 3.0, 4.0]
    4-element Array{Float64,1}:
     1.0
     2.0
     3.0
     4.0

    julia> gxs1 = [g(x) for x in xs]
    4-element Array{Any,1}:
     0.5
     0.333333
     0.25
     0.2

    Why isn't gxs1 type of Array{Float64,1}?
    How could I force the type of gxs1 to be of an array of Float64?

    julia> gxs2 = [convert(Float64,g(x)) for x in xs]
    4-element Array{Any,1}:
     0.5
     0.333333
     0.25
     0.2

    somehow this doesn't seem to work...





Reply via email to