float(X) vs. map(Float64, X)

148 views
Skip to first unread message

Júlio Hoffimann

unread,
Nov 29, 2015, 6:02:54 PM11/29/15
to julia-users
Hi,

Could you please confirm Xf = float(X) is a shortcut for Xf = map(Float64, X) on a 64bits machine?

For some reason the tests in my package fail when I replace one by the other.

-Júlio

Yichao Yu

unread,
Nov 29, 2015, 6:10:10 PM11/29/15
to Julia Users
On Sun, Nov 29, 2015 at 6:02 PM, Júlio Hoffimann
<julio.h...@gmail.com> wrote:
> Hi,
>
> Could you please confirm Xf = float(X) is a shortcut for Xf = map(Float64,
> X) on a 64bits machine?

No it isn't and it's independ of whether it's 32 or 64bits.

Depending one what exactly you are doing, the error message should be
fairly clear about the difference.

Júlio Hoffimann

unread,
Nov 29, 2015, 6:14:23 PM11/29/15
to Julia Users
Hi Yichao,

What is the difference?

-Júlio

Júlio Hoffimann

unread,
Nov 29, 2015, 8:44:21 PM11/29/15
to Julia Users
It has something to do with NaN representation:

# 64bits
a = ones(10,10)
a[:,5] = NaN
b = float(a)
c = map(Float64, a)

b == c # this should be true, but it's not

I'll open an issue on GitHub.

-Júlio

David P. Sanders

unread,
Nov 29, 2015, 9:44:07 PM11/29/15
to julia-users
NaNs are not equal to each other.

Júlio Hoffimann

unread,
Nov 29, 2015, 10:43:55 PM11/29/15
to Julia Users
Hi David,

Yes, I got confused for a second. Yichao clarified on the GitHub issue.

I still don't know why these two functions are causing different behavior in my package though.

-Júlio

Eric Forgy

unread,
Nov 29, 2015, 11:56:44 PM11/29/15
to julia-users
Hi Julio,

I'm not sure if this helps (I'm still learning the ropes), but you can find the definition of "float(x)" in base/float.jl on line 121:

float(x) = convert(AbstractFloat, x)

I found this from the following command:

julia> which(float,(Number,))
float(x) at float.jl:121

Similarly,

julia> which(map,(Float64,Number))
map
(f, x::Number) at number.jl:46

where we find:

map(f, x::Number, ys::Number...) = f(x, ys...)

So if your "x" is a Number, then it looks like your asking if 

julia> float(10) == map(Float64,10)
true

But I think your "x" is probably an AbstractArray{Number}, in which case we find:

julia> which(float,(AbstractArray{Number},))
float{T}(A::AbstractArray{T,N}) at float.jl:431

which is defined by:

function float{T}(A::AbstractArray{T})
   
if !isleaftype(T)
        error
("`float` not defined on abstractly-typed arrays; please convert to a more specific type")
   
end
    convert
(AbstractArray{typeof(float(zero(T)))}, A)
end

Similarly:

julia> which(map,(Float64,AbstractArray{Number}))
map
(f, A::AbstractArray{T,N}) at abstractarray.jl:1305

and:

function map(f, A::AbstractArray)
   
if isempty(A)
       
return isa(f,Type) ? similar(A,f) : similar(A)
   
end
    first
= f(A[1])
    dest
= similar(A, typeof(first))
    dest
[1] = first
   
return map_to!(f, 2, dest, A)
end

AND... I run out of time. Gotta run. Hope it helps a little. Cheers!

Júlio Hoffimann

unread,
Nov 30, 2015, 12:01:21 AM11/30/15
to Julia Users
Hi Eric,

I did all those steps, but thanks for spending time with it. :)

-Jùlio
Reply all
Reply to author
Forward
0 new messages