obliczenia_naukowe/l1/1.jl

88 lines
1.6 KiB
Julia
Raw Permalink Normal View History

2024-10-27 21:56:24 +01:00
#!/usr/bin/env julia
# Jacek Poziemski 272389
"""
2024-11-08 10:54:40 +01:00
macheps(T::Type{<: AbstractFloat})::T
2024-10-27 21:56:24 +01:00
Calculate the machine epsilon iteratively
for the given IEEE 754 floating point type.
# Arguments
2024-11-08 10:21:34 +01:00
- `T`: the floating point type
2024-10-27 21:56:24 +01:00
"""
2024-11-08 10:54:40 +01:00
function macheps(T::Type{<: AbstractFloat})::T
one_T::T = one(T)
ret::T = one_T
2024-10-27 21:56:24 +01:00
while one_T + (ret / T(2)) > one_T
ret /= T(2)
end
return ret
end
"""
2024-11-08 10:54:40 +01:00
eta(T::Type{<: AbstractFloat})::T
2024-10-27 21:56:24 +01:00
Calculate the eta number iteratively
for the given IEEE 754 floating point type.
# Arguments
2024-11-08 10:21:34 +01:00
- `T`: the floating point type
2024-10-27 21:56:24 +01:00
"""
2024-11-08 10:54:40 +01:00
function eta(T::Type{<: AbstractFloat})::T
ret::T = one(T)
2024-10-27 21:56:24 +01:00
while ret / T(2) > zero(T)
ret /= T(2)
end
return ret
end
"""
2024-11-08 10:54:40 +01:00
max(T::Type{<: AbstractFloat})::T
2024-10-27 21:56:24 +01:00
Calculate the largest possible value
for the given IEEE 754 floating type.
# Arguments
2024-11-08 10:21:34 +01:00
- `T`: the floating point type
2024-10-27 21:56:24 +01:00
"""
2024-11-08 10:54:40 +01:00
function max(T::Type{<: AbstractFloat})::T
ret::T = one(T)
2024-10-27 21:56:24 +01:00
while isfinite(ret * T(2))
ret *= T(2)
end
2024-11-08 10:54:40 +01:00
x::T = ret / T(2)
2024-10-27 21:56:24 +01:00
while isfinite(ret + x)
ret += x
x /= T(2)
end
return ret
end
2024-11-08 10:54:40 +01:00
for T::Type{<: AbstractFloat} in [Float16, Float32, Float64]
2024-10-27 21:56:24 +01:00
println("me - macheps($T): $(macheps(T))")
println("julia - eps($T): $(eps(T))")
println()
println("me - eta($T): $(eta(T))")
println("julia - nextfloat($T(0.0)): $(nextfloat(T(0.0)))")
println()
println("me - max($T): $(max(T))")
println("julia - floatmax($T): $(floatmax(T))")
println()
println("julia - floatmin($T): $(floatmin(T))")
println()
end