obliczenia_naukowe/l2/5.jl
2024-11-11 00:40:18 +01:00

84 lines
1.5 KiB
Julia
Executable file

#!/usr/bin/env julia
# Jacek Poziemski 272389
"""
model(r::T, p::T)::T where T <: AbstractFloat
Calculate `p + rp(1 - p)` on a given IEEE 754 floating point type.
# Arguments
- `r::T`: the first argument
- `p::T`: the second argument
"""
function model(r::T, p::T)::T where T <: AbstractFloat
return p + r * p * (one(T) - p)
end
"""
iter10trunciter30()::Vector{Float32}
Iterate on the model function 10 times, truncate
the result and iterate 30 more times.
"""
function iter10trunciter30()::Vector{Float32}
ret = Vector{Float32}()
p::Float32 = 0.01
r::Float32 = 3.0
push!(ret, p)
for i in 1:10
p = model(r, p)
push!(ret, p)
end
p = trunc(p, digits = 3)
for i in 1:30
p = model(r, p)
push!(ret, p)
end
return ret
end
"""
iter40(T::Type{<: AbstractFloat})::Vector{T}
Iterate on the model function 40 times.
# Arguments
- `T::Type{<: AbstractFloat}`: the IEEE 754 floating type to operate on
"""
function iter40(T::Type{<: AbstractFloat})::Vector{T}
ret = Vector{T}()
p::T = 0.01
r::T = 3.0
push!(ret, p)
for i in 1:40
p = model(r, p)
push!(ret, p)
end
return ret
end
p1::Vector{Float32} = iter40(Float32)
p2::Vector{Float32} = iter10trunciter30()
p3::Vector{Float64} = iter40(Float64)
println("1.")
println()
for n in 1:41
println("n: $(n - 1); 40 iter: $(p1[n]); trunc: $(p2[n])")
end
println()
println("2.")
println()
for n in 1:41
println("n: $(n - 1); Float32: $(p1[n]); Float64: $(p3[n])")
end