Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting an array of Any to Float : float() -> no method one(Type{Any}) #6422

Closed
bensadeghi opened this issue Apr 5, 2014 · 19 comments
Closed
Labels
regression Regression in behavior compared to a previous version

Comments

@bensadeghi
Copy link
Member

julia> float(Any[3.0])
ERROR: no method one(Type{Any})
 in float at abstractarray.jl:315
@ivarne
Copy link
Member

ivarne commented Apr 5, 2014

Not really sure what we should do here (should float sometimes not return a float, but an array?), but until that becomes decided, you can use convert(Array{Float64,1}, var).

@bensadeghi
Copy link
Member Author

This used to work just fine, returning an array of floats.
Using a ~3 month old version of Julia:

julia> float(Any[3.0])
1-element Array{Float64,1}:
 3.0

@timholy
Copy link
Member

timholy commented Apr 5, 2014

It works for me on a version built yesterday.

@tkelman
Copy link
Contributor

tkelman commented Apr 5, 2014

@timholy the merging of #6183 changed the behavior here.

@timholy
Copy link
Member

timholy commented Apr 5, 2014

Gotcha, thanks.

@JeffBezanson
Copy link
Member

I'm not sure what this should do; the old behavior was effectively picking Float64 as a default, which doesn't make much sense:

julia> float({big(pi)})
1-element Array{Float64,1}:
 3.14159

Since float depends on what you pass it, the best we can do is return another Any array with float applied to each element, but that's not too useful. Perhaps the fuzzy-typed functions like float and complex should require numeric arrays.

@XilinJia
Copy link

XilinJia commented Apr 7, 2014

I am pretty new to Julia, but it seems to me that type Any is causing confusion to everyone in this case.

It is very convenient to have a type Any as one can do things like:
julia> aa = ["a", 3.1, 4.2]
3-element Array{Any,1}:
"a"
3.1
4.2

Since one can also do
julia> bb = aa[2:end]
2-element Array{Any,1}:
3.1
4.2

the intention of bb is very clear - one wants to perform some kind of computation with these float (or real) numbers.

So in this case, bb should be allowed to be passed to
function func(xx::Array{Float64,1})
either directly without explicit convert, or with convert
float(bb)

If this can not be done, then what is the sense of having type Any?

@JeffBezanson
Copy link
Member

It's fine with me if float(bb) works; we just need a proposal for what it actually means. For example, we could iterate over all the elements to determine the minimal floating-point type that can hold them all, but that's not very efficient.

@jiahao
Copy link
Member

jiahao commented Apr 8, 2014

we could iterate over all the elements to determine the minimal floating-point type that can hold them all

It's not even clear what this means. If it's minimal type that can support the necessary precision, what happens to ["3.1"]? If it's minimal type given the dynamic range of values, then you'd end up with lots of Array{Float16}s, which are currently next to useless.

@JeffBezanson
Copy link
Member

That reminds me --- float("3.1") simply picks Float64 by default, so maybe we should just go with that.

@jiahao
Copy link
Member

jiahao commented Apr 8, 2014

I'd be fine with a Float64 default.

@andreasnoack
Copy link
Member

@bensadeghi The problem is that it is not obvious what default element type an array like {"Jensen", [1 2; 3 4], 3.1} should have.

@JeffBezanson For an Any array x the type would then be determined by something like eltype(promote(0.0,x...)), right?

It could be pointed out that you are able to convert the vector in the example with float64.

@JeffBezanson
Copy link
Member

Yes, you would have to reduce promote or promote_type over the values, ideally with a loop instead of ... to avoid stack overflow. This would be quite costly, but is arguably worth it if you end up with an Array{Float64} that would cause subsequent operations to be faster.

@JeffBezanson
Copy link
Member

I came up with a meaning: convert everything to FloatingPoint, then promote the results together.

@ashleylid
Copy link

Apologies for posting on a closed topic but I wasn't able to get float(point) where point is type {Any,2} to work with ERROR: LoadError: float not defined on abstractly-typed arrays; please convert to a more specific type. So I reverted to using:
point = Array{Float32}(point)

@navid11m
Copy link

navid11m commented Apr 6, 2021

Hi
How can I convert a sym array to Float?

@KristofferC
Copy link
Member

Please ask questions at https://discourse.julialang.org/ (also include a bit more information in your question, what package you are using etc).

@navid11m
Copy link

navid11m commented Apr 6, 2021 via email

@KristofferC
Copy link
Member

I understand that. The issue tracker is not a good place to ask these types of usage questions. That's why the forum at https://discourse.julialang.org/ exists. I would suggest posting your question there. And again, include more information. Just saying " I wrote an exercise and I wrote it symbolic and now I want to convert it to a float or integer" makes it very hard for anyone to understand what you are doing. Code is good to post because that is not so ambiguous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression Regression in behavior compared to a previous version
Projects
None yet
Development

No branches or pull requests