Skip to content

Commit

Permalink
Merge pull request #72 from milankl/mk/comparisons
Browse files Browse the repository at this point in the history
>,<,>=,<= via signed(::Posit)
  • Loading branch information
milankl committed Sep 30, 2022
2 parents fd3f61b + 87e3886 commit 73d44d7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
27 changes: 26 additions & 1 deletion .gitignore
@@ -1 +1,26 @@
/deps/*
.DS_Store

# Files generated by invoking Julia with --code-coverage
*.jl.cov
*.jl.*.cov

# Files generated by invoking Julia with --track-allocation
*.jl.mem

# System-specific files and directories generated by the BinaryProvider and BinDeps packages
# They contain absolute paths specific to the host computer, and so should not be committed
deps/deps.jl
deps/build.log
deps/downloads/
deps/usr/
deps/src/

# Build artifacts for creating documentation generated by the Documenter package
docs/build/
docs/site/

# File generated by Pkg, the package manager, based on a corresponding Project.toml
# It records a fixed state of all packages used by the project. As such, it should not be
# committed for packages, but should be committed for applications that require a static
# environment.
Manifest.toml
8 changes: 2 additions & 6 deletions Project.toml
@@ -1,13 +1,9 @@
name = "SoftPosit"
uuid = "0775deef-a35f-56d7-82da-cfc52f91364d"
version = "0.5.0"

[deps]
SoftPosit_jll = "f9aa12f2-fb2a-5e38-99be-91dba0a1f972"
version = "0.5.1"

[compat]
SoftPosit_jll = "0.4.1, 0.4.2"
julia = "1.6, 1.7"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
5 changes: 0 additions & 5 deletions src/SoftPosit.jl
@@ -1,10 +1,5 @@
module SoftPosit

# import SoftPosit_jll
# # For compatibility with previous versions of SofPosit.jl which used the name
# # `SoftPositPath`.
# const SoftPositPath = SoftPosit_jll.softposit

export AbstractPosit, Posit8, Posit16, Posit32, Posit16_1,
notareal

Expand Down
4 changes: 2 additions & 2 deletions src/comparisons.jl
Expand Up @@ -5,5 +5,5 @@ Base.isfinite(x::AbstractPosit) = ~isnan(x) # finite if not NaR
Base.iszero(x::AbstractPosit) = x == zero(x)

# COMPARISONS via two's complement (- of uints)
Base.:(<)(x::T,y::T) where {T<:AbstractPosit} = -unsigned(x) > -unsigned(y)
Base.:(<=)(x::T,y::T) where {T<:AbstractPosit} = -unsigned(x) >= -unsigned(y)
Base.:(<)(x::T,y::T) where {T<:AbstractPosit} = signed(x) < signed(y)
Base.:(<=)(x::T,y::T) where {T<:AbstractPosit} = signed(x) <= signed(y)
40 changes: 35 additions & 5 deletions test/comparisons.jl
@@ -1,10 +1,40 @@
@testset "Comparison" begin
f0,f1,f2,f3 = -0.9,-1.0,0.0,1.0
n = 100
fs = randn(n)
sort!(fs)

@testset for T in (Posit8, Posit16, Posit16_1, Posit32)
@test (f0 > f1) == (T(f0) > T(f1))
@test (f2 >= f2) == (T(f2) >= T(f2))
@test (f3 < f1) == (T(f3) < T(f3))
@test T(f0) === T(f0)
for i in 1:n-1
f1 = T(fs[i])
f2 = T(fs[i+1])

f1 == f2 || @test f2 > f1 # only test when not equal
@test f2 >= f1

f1 == f2 || @test f1 < f2 # only test when not equal
@test f1 <= f1
end
end

fs = 10*rand(n)
@testset for T in (Posit8, Posit16, Posit16_1, Posit32)
for f in fs
p = T(f)
@test p > 0
@test p >= 0
@test -p < 0
@test -p <= 0
end
end

# NaR is the smaller than -maxpos for comparisons
@testset for T in (Posit8, Posit16, Posit16_1, Posit32)
@test floatmax(T) > notareal(T)
@test one(T) > notareal(T)
@test floatmin(T) > notareal(T)
@test zero(T) > notareal(T)
@test -floatmin(T) > notareal(T)
@test -one(T) > notareal(T)
@test -floatmax(T) > notareal(T)
end
end

2 comments on commit 73d44d7

@milankl
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/69291

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" 73d44d7bc497b85bb8a604fa321e8e12eef6e7b0
git push origin v0.5.1

Please sign in to comment.