47 lines
1.2 KiB
Julia
Executable file
47 lines
1.2 KiB
Julia
Executable file
#!/usr/bin/env julia
|
|
|
|
# Jacek Poziemski 272389
|
|
|
|
"""
|
|
exponentsequal(x::Float64, y::Float64)
|
|
|
|
Check if the exponents of 2 64-bit IEEE 754
|
|
numbers are equal.
|
|
|
|
# Arguments
|
|
- `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)
|
|
|
|
return xexp == yexp
|
|
end
|
|
|
|
"""
|
|
numberdistribution(start::Float64, finish::Float64)
|
|
|
|
Calculate the step between 2 neighbouring
|
|
64-bit IEEE 754 numbers in a given range.
|
|
|
|
# Arguments
|
|
- `start::Float64`: bottom bound of the range
|
|
- `finish::Float64`: upper bound of the range
|
|
"""
|
|
function numberdistribution(start::Float64, finish::Float64)
|
|
if !exponentsequal(nextfloat(start), prevfloat(finish))
|
|
return missing
|
|
end
|
|
|
|
exp = parse(Int, SubString(bitstring(start), 2:12), base = 2)
|
|
|
|
return 2.0^(exp - 1023) * 2.0^(-52)
|
|
end
|
|
|
|
println("[1.0, 2.0]: $(numberdistribution(1.0, 2.0))")
|
|
println("2^(-52): $(2.0^(-52))")
|
|
println("[0.5, 1.0]: $(numberdistribution(0.5, 1.0))")
|
|
println("2^(-53): $(2.0^(-53))")
|
|
println("[2.0, 4.0]: $(numberdistribution(2.0, 4.0))")
|
|
println("2^(-51): $(2.0^(-51))")
|