0
votes

I have 2 arrays of tuples and I have a loop asking if one element is in the other. At each step I ask if the tuple contained in the coord array is in the Y array. The loop works fine except for one element which I cant explain why. Here is what I have :

Y[55:65] # This is the array I want to check at each step if my state is in or not.

11-element Array{Any,1}: (2.0, 1.0) (3.0, 1.0) (4.0, 1.0) (5.0, 1.0) (6.0, 1.0) (7.0, 1.0) (8.0, 1.0) (9.0, 1.0) (10.0, 1.0) (11.0, 1.0) (12.0, 1.0)

coord[i-1]  # this is one element of coord that is in Y

0-dimensional Array{Tuple{Float64,Float64},0}: (6.0, 1.0)

coord[i] # this is an other element of coord that is in Y

0-dimensional Array{Tuple{Float64,Float64},0}: (7.0, 1.0)

But then when I test when they are in Y:

in(coord[i],Y[55:65]) # testing if coord[i] is in Y

false

in(coord[i-1],Y[55:65]) # testing if coord[i-1] is in Y

true

I dont understand: they are both represented in the same way in Y, they have the same type, why do I get from using in() that one is in and not the other?

I use Julia version 0.6.3.

Thanks in advance for the help!

1
Most likely Y[55:65] contains (7.0, 1.0) wrapped in an 0-dimensional array and (6.0, 1.0) is not wrapped. They get printed in the same way so it is hard to distinguish them using visual inspection only. But this might be the case as Y[55:65] is a Vector{Any} so it seems that it contains mixed types. Can you post the result of typeof.(Y[55:65])?Bogumił Kamiński
Thanks for your help. typeof.(Y[55:65]) gives : 11-element Array{DataType,1}: Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0} Array{Tuple{Float64,Float64},0}ChrlTsr
I cant find what is the difference between these 2.ChrlTsr
It seems that the type is the same. So what does Y[60] == coord[i] comparison return? If it is false then what Y[60][][1] == coord[i][][1] and Y[60][][2] == coord[i][][2] return? If any of these two return false then actually entries of the tuples are not equal (which can happe if you are working with Float64 due to rounding).Bogumił Kamiński
Thank you for your help, yes I realised the values indeed were not totally equal, but differed by the machine precision. which is weird because I did something like 7/100*100, I did not expect that to happen! Thank youChrlTsr

1 Answers

1
votes

How did you get coord and Y? If you get them by calculations rather than direct assignments, they may not be exactly equal even if they are displayed so. For example:

julia> p1 = fill((6.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(6.0, 1.0)

julia> p2 = fill((7.0 + 3eps(), 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(7.000000000000001, 1.0)

julia> Y = [p1, p2]
2-element Array{Array{Tuple{Float64,Float64},0},1}:
 (6.0, 1.0)
 (7.0, 1.0) # NOTE that it get truncated in display but the content did not changed!

julia> x = fill((6.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(6.0, 1.0)

julia> x in Y
true

julia> x = fill((7.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(7.0, 1.0)

julia> x in Y
false

If this is the case, you can either round them before comparing or write the in function mannually using isapprox (or the operator, typed in Julia by \approx + Tab)