# 当range遇上Float64,精度影响了range长？！

#1

range这个函数挺逗的，有一些奇怪的特性。

（下附解决方案）

dl = 5e-6

for i = 1:20
λ = (((i-1)/20*2)):(dl):(i/20*2-dl)
println(length(λ))
end


julia>

20000
20000
20000
20000
20000
20000
19999
20000
20000
20000
20000
20000
20000
19999
20000
20000
20000
20000
19999
20000


7 14 和 19真是神奇的数字。。。

dl = 5e-6

for i = 1:20
λ = (((i-1)*.1)):(dl):(i*.1-dl)
println(length(λ))
end



20000
20000
20000
20000
20000
20000
19999
19999
20000
20000
20000
20000
19999
20000
19999
20000
20000
19999
20000
19999


for i = .1:.1:2
λ = Float64(i-.1):Float64(dl):Float64(Float64(i)-Float64(dl))
println(length(λ))
end

20000
20000
20000
20000
20000
20000
19999
19999
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000


## 解决方案

range ()是可以设长度的！！！这避免了 数值计算的误差影响数据结构本身

i=7
λ = range((i-1)/20*2,Float64(i/20*2-dl),length = 20000)
length(λ)
λ[1]
λ[end]
λ[end-1]


7

0.6:4.999999999999997e-6:0.6999949999999999

20000

0.600

0.6999949999999999

0.6999899999999999


Starting Julia...
_
_       _ _(_)_     |  Documentation: https://docs.julialang.org
(_)     | (_) (_)    |
_   _       _ _(_)_     |  Documentation: https://d
ocs.julialang.org
(_)     | (_) (_)    |   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.  | | | | | | |/ _ |  |
| | |_| | | | (_| |  |  Version 1.1.0 (2019-01-21)
_/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia>
`

#2

#3

0.7 - dl = 0.6999949999999999 < 0.699995
0.8 - dl = 0. 79995

#4

#5

#6

#7