TYPE EVERYTHING !!!!!

This commit is contained in:
jacekpoz 2024-11-08 10:54:40 +01:00
parent 929cdd9ef3
commit 860333e5c9
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
7 changed files with 85 additions and 85 deletions

24
l1/1.jl
View file

@ -3,7 +3,7 @@
# Jacek Poziemski 272389
"""
macheps(T::Type{<: AbstractFloat})
macheps(T::Type{<: AbstractFloat})::T
Calculate the machine epsilon iteratively
for the given IEEE 754 floating point type.
@ -11,9 +11,9 @@ for the given IEEE 754 floating point type.
# Arguments
- `T`: the floating point type
"""
function macheps(T::Type{<: AbstractFloat})
one_T = one(T)
ret = one_T
function macheps(T::Type{<: AbstractFloat})::T
one_T::T = one(T)
ret::T = one_T
while one_T + (ret / T(2)) > one_T
ret /= T(2)
end
@ -22,7 +22,7 @@ function macheps(T::Type{<: AbstractFloat})
end
"""
eta(T::Type{<: AbstractFloat})
eta(T::Type{<: AbstractFloat})::T
Calculate the eta number iteratively
for the given IEEE 754 floating point type.
@ -30,8 +30,8 @@ for the given IEEE 754 floating point type.
# Arguments
- `T`: the floating point type
"""
function eta(T::Type{<: AbstractFloat})
ret = one(T)
function eta(T::Type{<: AbstractFloat})::T
ret::T = one(T)
while ret / T(2) > zero(T)
ret /= T(2)
@ -41,7 +41,7 @@ function eta(T::Type{<: AbstractFloat})
end
"""
max(T::Type{<: AbstractFloat})
max(T::Type{<: AbstractFloat})::T
Calculate the largest possible value
for the given IEEE 754 floating type.
@ -49,14 +49,14 @@ for the given IEEE 754 floating type.
# Arguments
- `T`: the floating point type
"""
function max(T::Type{<: AbstractFloat})
ret = one(T)
function max(T::Type{<: AbstractFloat})::T
ret::T = one(T)
while isfinite(ret * T(2))
ret *= T(2)
end
x = ret / T(2)
x::T = ret / T(2)
while isfinite(ret + x)
ret += x
x /= T(2)
@ -65,7 +65,7 @@ function max(T::Type{<: AbstractFloat})
return ret
end
for T in [Float16, Float32, Float64]
for T::Type{<: AbstractFloat} in [Float16, Float32, Float64]
println("me - macheps($T): $(macheps(T))")
println("julia - eps($T): $(eps(T))")

View file

@ -3,7 +3,7 @@
# Jacek Poziemski 272389
"""
kahanmacheps(T::Type{<: AbstractFloat})
kahanmacheps(T::Type{<: AbstractFloat})::T
Calculate the Kahan machine epsilon
using the following expression: `3(4/3 - 1) - 1`.
@ -11,11 +11,11 @@ using the following expression: `3(4/3 - 1) - 1`.
# Arguments
- `T`: the floating point type
"""
function kahanmacheps(T::Type{<: AbstractFloat})
function kahanmacheps(T::Type{<: AbstractFloat})::T
return T(3) * ((T(4) / T(3)) - one(T)) - one(T)
end
for T in [Float16, Float32, Float64]
for T::Type{<: AbstractFloat} in [Float16, Float32, Float64]
println("kahanmacheps($T): $(kahanmacheps(T))")
println("eps($T): $(eps(T))")

14
l1/3.jl
View file

@ -3,7 +3,7 @@
# Jacek Poziemski 272389
"""
exponentsequal(x::Float64, y::Float64)
exponentsequal(x::Float64, y::Float64)::Bool
Check if the exponents of 2 64-bit IEEE 754
numbers are equal.
@ -12,15 +12,15 @@ numbers are equal.
- `x::Float64`: first of the 2 numbers to check
- `y::Float64`: second of the 2 numbers to check
"""
function exponentsequal(x::Float64, y::Float64)
xexp = SubString(bitstring(x), 2:12)
yexp = SubString(bitstring(y), 2:12)
function exponentsequal(x::Float64, y::Float64)::Bool
xexp::String = SubString(bitstring(x), 2:12)
yexp::String = SubString(bitstring(y), 2:12)
return xexp == yexp
end
"""
numberdistribution(start::Float64, finish::Float64)
numberdistribution(start::Float64, finish::Float64)::Float64
Calculate the step between 2 neighbouring
64-bit IEEE 754 numbers in a given range.
@ -29,12 +29,12 @@ Calculate the step between 2 neighbouring
- `start::Float64`: bottom bound of the range
- `finish::Float64`: upper bound of the range
"""
function numberdistribution(start::Float64, finish::Float64)
function numberdistribution(start::Float64, finish::Float64)::Float64
if !exponentsequal(nextfloat(start), prevfloat(finish))
return missing
end
exp = parse(Int, SubString(bitstring(start), 2:12), base = 2)
exp::Int = parse(Int, SubString(bitstring(start), 2:12), base = 2)
return 2.0^(exp - 1023) * 2.0^(-52)
end

10
l1/4.jl
View file

@ -3,7 +3,7 @@
# Jacek Poziemski 272389
"""
findsmallest(start::Float64, finish::Float64)
findsmallest(start::Float64, finish::Float64)::Float64
Find the smallest Float64 larger than `start`
and smaller than `finish` that satisfies the
@ -13,9 +13,9 @@ following inequality `x * (1/x) ≠ 1`.
- `start::Float64`: bottom limit for the wanted number
- `finish::Float64`: upper limit for the wanted number
"""
function findsmallest(start::Float64, finish::Float64)
x = nextfloat(Float64(start))
res = zero(Float64)
function findsmallest(start::Float64, finish::Float64)::Float64
x::Float64 = nextfloat(Float64(start))
res::Float64 = zero(Float64)
while x < Float64(finish)
res = x * (one(Float64) / x)
@ -28,7 +28,7 @@ function findsmallest(start::Float64, finish::Float64)
return missing
end
smallest = findsmallest(1.0, 2.0)
smallest::Float64 = findsmallest(1.0, 2.0)
if ismissing(smallest)
println("smallest not found")

84
l1/5.jl
View file

@ -3,19 +3,19 @@
# Jacek Poziemski 272389
"""
sumvectorsforwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
Calculate the scalar sum of `x` and `y`,
going from the first element to the last.
# Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsforwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
s = 0.0
function sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::T = zero(T)
for i in 1:(length(x))
for i::Int in 1:(length(x))
s += x[i] * y[i]
end
@ -23,19 +23,19 @@ function sumvectorsforwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFl
end
"""
sumvectorsbackwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
Calculate the scalar sum of `x` and `y`,
going from the last element to the first.
# Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsbackwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
s = 0.0
function sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::T = zero(T)
for i in (length(x)):-1:1
for i::Int in (length(x)):-1:1
s += x[i] * y[i]
end
@ -43,7 +43,7 @@ function sumvectorsbackwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractF
end
"""
sumvectorsdecreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
Calculate the scalar sum of `x` and `y` by calculating
the partial sums of positive and negative numbers,
@ -51,37 +51,37 @@ summing up the partial sums using a decreasing order
according to their absolute values.
# Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsdecreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
s = zeros(length(x))
function sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::Vector{T} = zeros(T, length(x))
for i in 1:(length(x))
for i::Int in 1:(length(x))
s[i] = x[i] * y[i]
end
spos = s[s .> 0]
spos::Vector{T} = s[s .> 0]
sort!(spos, rev = true)
sneg = s[s .<= 0]
sneg::Vector{T} = s[s .<= 0]
sort!(sneg)
neg = 0
for i in sneg
neg += i
neg::T = zero(T)
for x::T in sneg
neg += x
end
pos = 0
for i in spos
pos += i
pos::T = zero(T)
for x::T in spos
pos += x
end
return pos + neg
end
"""
sumvectorsincreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
Calculate the scalar sum of `x` and `y` by calculating
the partial sums of positive and negative numbers,
@ -89,30 +89,30 @@ summing up the partial sums using an increasing order
according to their absolute values.
# Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsincreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
s = zeros(length(x))
function sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::Vector{T} = zeros(T, length(x))
for i in 1:(length(x))
for i::Int in 1:(length(x))
s[i] = x[i] * y[i]
end
spos = s[s .> 0]
spos::Vector{T} = s[s .> 0]
sort!(spos)
sneg = s[s .<= 0]
sneg::Vector{T} = s[s .<= 0]
sort!(sneg, rev = true)
neg = 0
for i in sneg
neg += i
neg::T = zero(T)
for x::T in sneg
neg += x
end
pos = 0
for i in spos
pos += i
pos::T = zero(T)
for x::T in spos
pos += x
end
return pos + neg
@ -124,9 +124,9 @@ y32::Vector{Float32} = [1486.2497, 878366.9879, 22.37492, 4773714.647, 0.0001
x64::Vector{Float64} = [2.718281828, 3.141592654, 1.414213562, 0.5772156649, 0.3010299957]
y64::Vector{Float64} = [1486.2497, 878366.9879, 22.37492, 4773714.647, 0.000185049]
expected = 1.00657107000000 * 10^(11)
expected::Float64 = 1.00657107000000 * 10^(11)
res = sumvectorsforwards(x32, y32)
res::Float64 = sumvectorsforwards(x32, y32)
println("summing x32 and y32 forwards: $res; correct: $(res == expected)")
res = sumvectorsbackwards(x32, y32)

10
l1/6.jl
View file

@ -3,30 +3,30 @@
# Jacek Poziemski 272389
"""
f(x::Float64)
f(x::Float64)::Float64
Calculate `sqrt(x^2 + 1) - 1`.
# Arguments
- `x::Float64`: function argument
"""
function f(x::Float64)
function f(x::Float64)::Float64
return sqrt(x^2 + one(Float64)) - one(Float64)
end
"""
g(x::Float64)
g(x::Float64)::Float64
Calculate `x^2 / (sqrt(x^2 + 1) + 1)`.
# Arguments
- `x::Float64`: function argument
"""
function g(x::Float64)
function g(x::Float64)::Float64
return x^2 / (sqrt(x^2 + one(Float64)) + one(Float64))
end
for i in 1:20
for i::Int in 1:20
x::Float64 = 8.0^(-i)
println("f(8^-$i) = $(f(x))")
println("g(8^-$i) = $(g(x))")

22
l1/7.jl
View file

@ -3,31 +3,31 @@
# Jacek Poziemski 272389
"""
f(x::Float64)
f(x::Float64)::Float64
Calculate `sin(x) + cos(3x)`.
# Arguments
- `x::Float64`: function argument
"""
function f(x::Float64)
function f(x::Float64)::Float64
return sin(x) + cos(Float64(3) * x)
end
"""
fderivative(x::Float64)
fderivative(x::Float64)::Float64
Calculate the derivative of `sin(x) + cos(3x)`, `cos(x) - 3sin(3x)`.
# Arguments
- `x::Float64`: function argument
"""
function fderivative(x::Float64)
function fderivative(x::Float64)::Float64
return cos(x) - Float64(3) * sin(Float64(3) * x)
end
"""
derivative(f::Function, x0::Float64, h::Float64)
derivative(f::Function, x0::Float64, h::Float64)::Float64
Calculate an approximation of the derivative of `f` at `x0`,
using `h` in the following formula: `(f(x0 + h) - f(x0)) / h`.
@ -37,16 +37,16 @@ using `h` in the following formula: `(f(x0 + h) - f(x0)) / h`.
- `x0::Float64`: function argument
- `h::Float64`: value used in the approximation formula
"""
function derivative(f::Function, x0::Float64, h::Float64)
function derivative(f::Function, x0::Float64, h::Float64)::Float64
return (f(x0 + h) - f(x0)) / h
end
x = one(Float64)
x::Float64 = one(Float64)
for n in 0:54
h = 2.0^(-n)
d = derivative(f, x, h)
e = abs(fderivative(x) - d)
for n::Int in 0:54
h::Float64 = 2.0^(-n)
d::Float64 = derivative(f, x, h)
e::Float64 = abs(fderivative(x) - d)
println("n: $n; h: $h; h + 1: $(h + 1); derivative: $d; error: $e")
end