Skip to content

Commit

Permalink
Merge pull request #10 from milankl/ambiguity
Browse files Browse the repository at this point in the history
Ambiguity
  • Loading branch information
Milan K authored Feb 28, 2020
2 parents 1dea6c1 + 23b2067 commit 57bbf58
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- julia_version: 1
- julia_version: 1.2
- julia_version: 1.3
- julia_version: nightly

platform:
Expand Down
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ task:
env:
matrix:
- JULIA_VERSION: 1.0
- JULIA_VERSION: 1.2
- JULIA_VERSION: 1.3
- JULIA_VERSION: nightly
install_script:
- sh -c "$(fetch https://raw.githubusercontent.com/ararslan/CirrusCI.jl/master/bin/install.sh -o -)"
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ os:
- osx
julia:
- 1.0
- 1.2
- 1.3
- nightly
notifications:
email: false
Expand Down
88 changes: 88 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
# This file is machine-generated - editing it directly is not advised

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[DataAPI]]
git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.1.0"

[[DataStructures]]
deps = ["InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "5a431d46abf2ef2a4d5d00bd0ae61f651cf854c8"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.17.10"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Missings]]
deps = ["DataAPI"]
git-tree-sha1 = "de0a5ce9e5289f27df672ffabef4d1e5861247d5"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "0.4.3"

[[OrderedCollections]]
deps = ["Random", "Serialization", "Test"]
git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.1.0"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SortingAlgorithms]]
deps = ["DataStructures", "Random", "Test"]
git-tree-sha1 = "03f5898c9959f8115e30bc7226ada7d0df554ddd"
uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
version = "0.3.1"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[StatsBase]]
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"]
git-tree-sha1 = "be5c7d45daa449d12868f4466dbf5882242cf2d9"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
version = "0.32.1"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ uuid = "d4954a42-a18b-4f81-bb79-24f1192c93d8"
authors = ["Milan Kloewer"]
version = "0.1.0"

[deps]
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
julia = "1"
StatsBase = "^0.32"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
84 changes: 54 additions & 30 deletions src/Sherlog16.jl
Original file line number Diff line number Diff line change
@@ -1,52 +1,76 @@
struct Sherlog16{T<:AbstractFloat} <: AbstractSherlog
struct Sherlog16{T<:AbstractFloat,i} <: AbstractSherlog # T is the bitpattern type for logging, i is the logbook #
val::T
end

Base.UInt16(x::Sherlog16) = reinterpret(UInt16,x.val)
# conversions back from Sherlog16
Base.Float64(x::Sherlog16) = Float64(x.val)
Base.Float32(x::Sherlog16) = Float32(x.val)
Base.Float16(x::Sherlog16) = Float16(x.val)
Base.Int64(x::Sherlog16) = Int64(x.val)
Base.Int32(x::Sherlog16) = Int32(x.val)
Base.Int16(x::Sherlog16) = Int16(x.val)

Sherlog16(x::T) where {T<:AbstractFloat} = Sherlog16{Float16}(x)
Sherlog16(x::Integer) = Sherlog16{Float16}(Float16(x))
# otherwise conversion to itself is ambigious
Sherlog16{T,i}(x::Sherlog16) where {T<:AbstractFloat,i} = Sherlog16{T,i}(x.val)
Sherlog16(x::Sherlog16{T,i}) where {T<:AbstractFloat,i} = Sherlog16{T,i}(x.val)

Base.oneunit(::Type{Sherlog16{T}}) where {T<:AbstractFloat} = Sherlog16{T}(1)
# generator functions from Float, Int and Bool
Sherlog16(x::AbstractFloat) = Sherlog16{Float16,1}(x) # use Float16 and logbook #1 as standard
Sherlog16{T}(x::AbstractFloat) where T = Sherlog16{T,1}(x) # use logbook #1 if no logbook provided
Sherlog16(x::Integer) = Sherlog16{Float16,1}(x) # same for integers
Sherlog16{T}(x::Integer) where T = Sherlog16{T,1}(x)
Sherlog16{T}(x::Bool) where T = if x Sherlog16{T,1}(1) else Sherlog16{T,1}(0) end

Base.promote_rule(::Type{Int64},::Type{Sherlog16{T}}) where T = Sherlog16
Base.promote_rule(::Type{Int32},::Type{Sherlog16{T}}) where T = Sherlog16
Base.promote_rule(::Type{Int16},::Type{Sherlog16{T}}) where T = Sherlog16
Base.promote_rule(::Type{Int64},::Type{Sherlog16{T,i}}) where {T,i} = Sherlog16{T,i}
Base.promote_rule(::Type{Int32},::Type{Sherlog16{T,i}}) where {T,i} = Sherlog16{T,i}
Base.promote_rule(::Type{Int16},::Type{Sherlog16{T,i}}) where {T,i} = Sherlog16{T,i}

Base.promote_rule(::Type{Float64},::Type{Sherlog16{T}}) where T = Sherlog16
Base.promote_rule(::Type{Float32},::Type{Sherlog16{T}}) where T = Sherlog16
Base.promote_rule(::Type{Float16},::Type{Sherlog16{T}}) where T = Sherlog16
Base.promote_rule(::Type{Float64},::Type{Sherlog16{T,i}}) where {T,i} = Sherlog16{T,i}
Base.promote_rule(::Type{Float32},::Type{Sherlog16{T,i}}) where {T,i} = Sherlog16{T,i}
Base.promote_rule(::Type{Float16},::Type{Sherlog16{T,i}}) where {T,i} = Sherlog16{T,i}

bitstring(x::Sherlog16) = bitstring(x.val)
Base.show(io::IO,x::Sherlog16) = print(io,string(x.val))
Base.bitstring(x::Sherlog16) = bitstring(x.val)
Base.show(io::IO,x::Sherlog16) = print(io,"Sherlog16(",string(x.val),")")

Base.eps(::Type{Sherlog16{T}}) where {T<:AbstractFloat} = eps(T)
Base.eps(::Type{Sherlog16{T,i}}) where {T,i} = eps(Float64)
Base.eps(x::Sherlog16) = eps(x.val)

function +(x::Sherlog16{T},y::Sherlog16{T}) where T
Base.typemin(::Sherlog16{T,i}) where {T,i} = Sherlog16{T,i}(typemin(T))
Base.typemax(::Sherlog16{T,i}) where {T,i} = Sherlog16{T,i}(typemax(T))
Base.floatmin(::Sherlog16{T,i}) where {T,i} = Sherlog16{T,i}(floatmin(T))
Base.floatmax(::Sherlog16{T,i}) where {T,i} = Sherlog16{T,i}(floatmax(T))
Base.precision(::Sherlog16{T,i}) where {T,i} = Sherlog16{T,i}(precision(T))

-(x::Sherlog16{T,i}) where {T,i} = Sherlog16{T,i}(-x.val)

function +(x::Sherlog16{T,i},y::Sherlog16{T,i}) where {T,i}
r = x.val + y.val
logit(T,r)
return Sherlog16{T}(r)
log_it(T,r,i)
return Sherlog16{T,i}(r)
end

function -(x::Sherlog16{T},y::Sherlog16{T}) where T
function -(x::Sherlog16{T,i},y::Sherlog16{T,i}) where {T,i}
r = x.val - y.val
logit(T,r)
return Sherlog16{T}(r)
log_it(T,r,i)
return Sherlog16{T,i}(r)
end

function *(x::Sherlog16{T},y::Sherlog16{T}) where T
function *(x::Sherlog16{T,i},y::Sherlog16{T,i}) where {T,i}
r = x.val * y.val
logit(T,r)
return Sherlog16{T}(r)
log_it(T,r,i)
return Sherlog16{T,i}(r)
end

function /(x::Sherlog16{T},y::Sherlog16{T}) where T
function /(x::Sherlog16{T,i},y::Sherlog16{T,i}) where {T,i}
r = x.val / y.val
logit(T,r)
return Sherlog16{T}(r)
log_it(T,r,i)
return Sherlog16{T,i}(r)
end

function ^(x::Sherlog16{T,i},y::Sherlog16{T,i}) where {T,i}
r = x.val ^ y.val
log_it(T,r,i)
return Sherlog16{T,i}(r)
end

for O in ( :(-), :(+),
Expand All @@ -66,16 +90,16 @@ for O in ( :(-), :(+),
:asind, :acosd, :atand, :acscd, :asecd, :acotd
)
@eval begin
function Base.$O(x::Sherlog16{T}) where T
function Base.$O(x::Sherlog16{T,i}) where {T,i}
r = $O(x.val)
logit(T,r)
return Sherlog16{T}(r)
log_it(T,r,i)
return Sherlog16{T,i}(r)
end
end
end

for O in ( :(<), :(<=))
@eval begin
Base.$O(x::Sherlog16{T}, y::Sherlog16{T}) where T = $O(x.val, y.val)
Base.$O(x::Sherlog16, y::Sherlog16) = $O(x.val, y.val)
end
end
88 changes: 56 additions & 32 deletions src/Sherlog32.jl
Original file line number Diff line number Diff line change
@@ -1,52 +1,76 @@
struct Sherlog32{T<:AbstractFloat} <: AbstractSherlog # T is the bitpattern type for logging
val::Float32 # the value is always Float32
struct Sherlog32{T<:AbstractFloat,i} <: AbstractSherlog # T is the bitpattern type for logging, i is the logbook #
val::Float32 # the value is always Float32
end

Base.UInt32(x::Sherlog32) = reinterpret(UInt32,x.val)
Base.Float32(x::Sherlog32) = x.val
# conversions back from Sherlog32
Base.Float64(x::Sherlog32) = Float64(x.val)
Base.Float32(x::Sherlog32) = x.val
Base.Float16(x::Sherlog32) = Float16(x.val)
Base.Int64(x::Sherlog32) = Int64(x.val)
Base.Int32(x::Sherlog32) = Int32(x.val)
Base.Int16(x::Sherlog32) = Int16(x.val)

# otherwise conversion to itself is ambigious
Sherlog32{T,i}(x::Sherlog32) where {T<:AbstractFloat,i} = Sherlog32{T,i}(x.val)
Sherlog32(x::Sherlog32{T,i}) where {T<:AbstractFloat,i} = Sherlog32{T,i}(x.val)

Sherlog32(x::AbstractFloat) = Sherlog32{Float16}(x)
Sherlog32(x::Integer) = Sherlog32{Float16}(Float32(x))
# generator functions from Float, Int and Bool
Sherlog32(x::AbstractFloat) = Sherlog32{Float16,1}(x) # use Float16 and logbook #1 as standard
Sherlog32{T}(x::AbstractFloat) where T = Sherlog32{T,1}(x) # use logbook #1 if no logbook provided
Sherlog32(x::Integer) = Sherlog32{Float16,1}(x) # same for integers
Sherlog32{T}(x::Integer) where T = Sherlog32{T,1}(x)
Sherlog32{T}(x::Bool) where T = if x Sherlog32{T,1}(1) else Sherlog32{T,1}(0) end

Base.oneunit(::Type{Sherlog32{T}}) where {T<:AbstractFloat} = Sherlog32{T}(1)
Base.promote_rule(::Type{Int64},::Type{Sherlog32{T,i}}) where {T,i} = Sherlog32{T,i}
Base.promote_rule(::Type{Int32},::Type{Sherlog32{T,i}}) where {T,i} = Sherlog32{T,i}
Base.promote_rule(::Type{Int16},::Type{Sherlog32{T,i}}) where {T,i} = Sherlog32{T,i}

Base.promote_rule(::Type{Int64},::Type{Sherlog32{T}}) where T = Sherlog32
Base.promote_rule(::Type{Int32},::Type{Sherlog32{T}}) where T = Sherlog32
Base.promote_rule(::Type{Int16},::Type{Sherlog32{T}}) where T = Sherlog32
Base.promote_rule(::Type{Float64},::Type{Sherlog32{T,i}}) where {T,i} = Sherlog32{T,i}
Base.promote_rule(::Type{Float32},::Type{Sherlog32{T,i}}) where {T,i} = Sherlog32{T,i}
Base.promote_rule(::Type{Float16},::Type{Sherlog32{T,i}}) where {T,i} = Sherlog32{T,i}

Base.promote_rule(::Type{Float64},::Type{Sherlog32{T}}) where T = Sherlog32
Base.promote_rule(::Type{Float32},::Type{Sherlog32{T}}) where T = Sherlog32
Base.promote_rule(::Type{Float16},::Type{Sherlog32{T}}) where T = Sherlog32
Base.bitstring(x::Sherlog32) = bitstring(x.val)
Base.show(io::IO,x::Sherlog32) = print(io,"Sherlog32(",string(x.val),")")

bitstring(x::Sherlog32) = bitstring(x.val)
Base.show(io::IO,x::Sherlog32) = print(io,string(x.val))
Base.eps(::Type{Sherlog32{T,i}}) where {T,i} = eps(Float32)
Base.eps(x::Sherlog32) = eps(x.val)

Base.eps(::Type{Sherlog32{T}}) where {T<:AbstractFloat} = eps(Float32)
Base.typemin(::Sherlog32{T,i}) where {T,i} = Sherlog32{T,i}(typemin(Float32))
Base.typemax(::Sherlog32{T,i}) where {T,i} = Sherlog32{T,i}(typemax(Float32))
Base.floatmin(::Sherlog32{T,i}) where {T,i} = Sherlog32{T,i}(floatmin(Float32))
Base.floatmax(::Sherlog32{T,i}) where {T,i} = Sherlog32{T,i}(floatmax(Float32))
Base.precision(::Sherlog32{T,i}) where {T,i} = Sherlog32{T,i}(precision(Float32))

function +(x::Sherlog32{T},y::Sherlog32{T}) where T
-(x::Sherlog32{T,i}) where {T,i} = Sherlog32{T,i}(-x.val)

function +(x::Sherlog32{T,i},y::Sherlog32{T,i}) where {T,i}
r = x.val + y.val
logit(T,r)
return Sherlog32{T}(r)
log_it(T,r,i)
return Sherlog32{T,i}(r)
end

function -(x::Sherlog32{T},y::Sherlog32{T}) where T
function -(x::Sherlog32{T,i},y::Sherlog32{T,i}) where {T,i}
r = x.val - y.val
logit(T,r)
return Sherlog32{T}(r)
log_it(T,r,i)
return Sherlog32{T,i}(r)
end

function *(x::Sherlog32{T},y::Sherlog32{T}) where T
function *(x::Sherlog32{T,i},y::Sherlog32{T,i}) where {T,i}
r = x.val * y.val
logit(T,r)
return Sherlog32{T}(r)
log_it(T,r,i)
return Sherlog32{T,i}(r)
end

function /(x::Sherlog32{T},y::Sherlog32{T}) where T
function /(x::Sherlog32{T,i},y::Sherlog32{T,i}) where {T,i}
r = x.val / y.val
logit(T,r)
return Sherlog32{T}(r)
log_it(T,r,i)
return Sherlog32{T,i}(r)
end

function ^(x::Sherlog32{T,i},y::Sherlog32{T,i}) where {T,i}
r = x.val ^ y.val
log_it(T,r,i)
return Sherlog32{T,i}(r)
end

for O in ( :(-), :(+),
Expand All @@ -66,16 +90,16 @@ for O in ( :(-), :(+),
:asind, :acosd, :atand, :acscd, :asecd, :acotd
)
@eval begin
function Base.$O(x::Sherlog32{T}) where T
function Base.$O(x::Sherlog32{T,i}) where {T,i}
r = $O(x.val)
logit(T,r)
return Sherlog32{T}(r)
log_it(T,r,i)
return Sherlog32{T,i}(r)
end
end
end

for O in ( :(<), :(<=))
@eval begin
Base.$O(x::Sherlog32{T}, y::Sherlog32{T}) where T = $O(x.val, y.val)
Base.$O(x::Sherlog32, y::Sherlog32) = $O(x.val, y.val)
end
end
Loading

0 comments on commit 57bbf58

Please sign in to comment.