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

View file

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

14
l1/3.jl
View file

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

10
l1/4.jl
View file

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

84
l1/5.jl
View file

@ -3,19 +3,19 @@
# Jacek Poziemski 272389 # 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`, Calculate the scalar sum of `x` and `y`,
going from the first element to the last. going from the first element to the last.
# Arguments # Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors - `x::Vector{T}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors - `y::Vector{T}`: second of the 2 vectors
""" """
function sumvectorsforwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat}) function sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s = 0.0 s::T = zero(T)
for i in 1:(length(x)) for i::Int in 1:(length(x))
s += x[i] * y[i] s += x[i] * y[i]
end end
@ -23,19 +23,19 @@ function sumvectorsforwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFl
end 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`, Calculate the scalar sum of `x` and `y`,
going from the last element to the first. going from the last element to the first.
# Arguments # Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors - `x::Vector{T}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors - `y::Vector{T}`: second of the 2 vectors
""" """
function sumvectorsbackwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat}) function sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s = 0.0 s::T = zero(T)
for i in (length(x)):-1:1 for i::Int in (length(x)):-1:1
s += x[i] * y[i] s += x[i] * y[i]
end end
@ -43,7 +43,7 @@ function sumvectorsbackwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractF
end 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 Calculate the scalar sum of `x` and `y` by calculating
the partial sums of positive and negative numbers, 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. according to their absolute values.
# Arguments # Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors - `x::Vector{T}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors - `y::Vector{T}`: second of the 2 vectors
""" """
function sumvectorsdecreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat}) function sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s = zeros(length(x)) 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] s[i] = x[i] * y[i]
end end
spos = s[s .> 0] spos::Vector{T} = s[s .> 0]
sort!(spos, rev = true) sort!(spos, rev = true)
sneg = s[s .<= 0] sneg::Vector{T} = s[s .<= 0]
sort!(sneg) sort!(sneg)
neg = 0 neg::T = zero(T)
for i in sneg for x::T in sneg
neg += i neg += x
end end
pos = 0 pos::T = zero(T)
for i in spos for x::T in spos
pos += i pos += x
end end
return pos + neg return pos + neg
end 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 Calculate the scalar sum of `x` and `y` by calculating
the partial sums of positive and negative numbers, 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. according to their absolute values.
# Arguments # Arguments
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors - `x::Vector{T}`: first of the 2 vectors
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors - `y::Vector{T}`: second of the 2 vectors
""" """
function sumvectorsincreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat}) function sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s = zeros(length(x)) 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] s[i] = x[i] * y[i]
end end
spos = s[s .> 0] spos::Vector{T} = s[s .> 0]
sort!(spos) sort!(spos)
sneg = s[s .<= 0] sneg::Vector{T} = s[s .<= 0]
sort!(sneg, rev = true) sort!(sneg, rev = true)
neg = 0 neg::T = zero(T)
for i in sneg for x::T in sneg
neg += i neg += x
end end
pos = 0 pos::T = zero(T)
for i in spos for x::T in spos
pos += i pos += x
end end
return pos + neg 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] 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] 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)") println("summing x32 and y32 forwards: $res; correct: $(res == expected)")
res = sumvectorsbackwards(x32, y32) res = sumvectorsbackwards(x32, y32)

10
l1/6.jl
View file

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

22
l1/7.jl
View file

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