(x: y) in Julia

I am trying to understand this code:

r = (1:10) - (4/1) println(r) 

Output:

-3.0: 1.0: 6.0

I understood why I got -3 and 6 . But why did I get this value in the middle (1.0) ? How did Julia calculate this? Or how can I do this?

+8
julia-lang ijulia-notebook
source share
3 answers
Syntax

(first:step:last) represents the type of Range in Julia

 typeof(1:10) # => UnitRange{Int32} 

If the step is omitted, the default is 1

 1:10 == 1:1:10 # => true 

A Range - compact row view

 collect(1:10) # => 10-element Array{Int32,1}: # 1 # 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # 10 

Thus, it was expected that the types Range and Vector would follow the same rules, for example, when you add a constant value like this:

 collect(1+(1:10))==collect(1:10)+1 # => true 

or even adding two vectors gives you the same result of adding their range representation as follows:

 collect((1:10)+(1:10))==collect(1:10)+collect(1:10) # => true 
+12
source share

The division operator in 4/1 returns a Float64 . Although the source range is a 1 Int step, the measurement step, after adding a floating point to both sides, it becomes a Float64 range. Thus, a step size of 1.0 is created by converting an implicit integer step size (floating-point numbers are not evenly distributed, so unified stepping is a bit complicated - sometimes there are rounding problems).

+3
source share

This can be seen by applying float to the interval:

 julia> 1:10 1:10 julia> float(1:10) 1.0:1.0:10.0 

and this promotion is required before adding to Float64 4/1 ( 4.0 ).

Similarly, when adding an integer to a float, julia β€œpushes” the integer to a float before adding / subtracting:

 julia> 1 + 2.0 3.0 julia> @which 1 + 2.0 +(x::Number, y::Number) at promotion.jl:172 

see promotion rules :

 +(x::Number, y::Number) = +(promote(x,y)...) 

You can @which follow function calls to the end to understand what is going on (completely the following ):

 julia> @which +(1:10, 2.0) +(A::AbstractArray{T,N}, x::Number) at arraymath.jl julia> @which .+(1:10, 2.0) .+(r::Range{T}, x::Real) at range.jl julia> @which .+(2.0, 1:10) .+(x::Real, r::UnitRange{T<:Real}) at range.jl # which is defined as .+(x::Real, r::UnitRange) = range(x + r.start, length(r)) 

and therefore promotion is the addition of Int64 and Float64.


Note in the main mode the interval display is somewhat less confusing / ambiguous:

 julia> float(1:10) 10-element FloatRange{Float64}: 1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0 julia> 1:10 10-element UnitRange{Int64}: 1,2,3,4,5,6,7,8,9,10 
+2
source share

All Articles