Skip to content

Commit

Permalink
Merge pull request #70 from milankl/mk/eps
Browse files Browse the repository at this point in the history
eps, ^, sinpi, cospi, sincospi,
  • Loading branch information
milankl committed Jun 13, 2022
2 parents 7f32549 + 07eb824 commit fd3f61b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -72,5 +72,5 @@ julia> floatmin(Posit16)*floatmin(Posit16)
Posit16(1.3877788e-17)
```
and similar for other posit formats.
So in Posit16 arithmetic we have `1e-17*1e-17 = 1e-17` and `1e17*1e17 = 1e17` (no overflow).
So in Posit16 arithmetic we have `1e-17*1e-17 = 1e-17` (no underflow) and `1e17*1e17 = 1e17` (no overflow).

16 changes: 9 additions & 7 deletions src/arithmetics.jl
Expand Up @@ -12,21 +12,22 @@ function Base.sign(x::T) where {T<:AbstractPosit}
end

# TWO ARGUMENT ARITHMETIC, addition, subtraction, multiplication, division via conversion
for op in (:(+), :(-), :(*), :(/))
for op in (:(+), :(-), :(*), :(/), :(^))
@eval begin
Base.$op(x::T,y::T) where {T<:AbstractPosit} = convert(T,$op(float(x),float(y)))
end
end

# ONE ARGUMENT ARITHMETIC, sqrt, exp, log, etc. via conversion
for op in (:sqrt, :exp, :exp2, :exp10, :expm1, :log, :log2, :log10, :log1p,
:sin, :cos, :tan)
:sin, :cos, :tan, :sinpi, :cospi)
@eval begin
Base.$op(x::T) where {T<:AbstractPosit} = convert(T,$op(float(x)))
end
end

Base.sincos(x::AbstractPosit) = sin(x),cos(x) # not in eval loop because of convert
Base.sincospi(x::AbstractPosit) = sinpi(x),cospi(x) # not in eval loop because of convert

# complex trigonometric functions
for P in (:Posit8, :Posit16, :Posit16_1, :Posit32)
Expand All @@ -38,12 +39,13 @@ for P in (:Posit8, :Posit16, :Posit16_1, :Posit32)
end

# nextfloat, prevfloat have a wrap-around behaviour nextfloat(maxpos) = NaR, nextfloat(NaR) = -maxpos
# which closes the posit circle and follow the 2022 standard
Base.nextfloat(x::T) where {T<:AbstractPosit} = reinterpret(T,reinterpret(Base.uinttype(T),x)+one(Base.uinttype(T)))
Base.prevfloat(x::T) where {T<:AbstractPosit} = reinterpret(T,reinterpret(Base.uinttype(T),x)-one(Base.uinttype(T)))

# precision (taken from lookup table)
eps(::Type{Posit8}) = reinterpret(Posit8,0x28)
eps(::Type{Posit16}) = reinterpret(Posit16,0x0a00)
eps(::Type{Posit16_1}) = reinterpret(Posit16_1,0x0100)
eps(::Type{Posit32}) = reinterpret(Posit32,0x00a0_0000)
eps(x::AbstractPosit) = max(x-prevfloat(x),nextfloat(x)-x)
Base.eps(::Type{Posit8}) = reinterpret(Posit8,0x28)
Base.eps(::Type{Posit16}) = reinterpret(Posit16,0x0a00)
Base.eps(::Type{Posit16_1}) = reinterpret(Posit16_1,0x0100)
Base.eps(::Type{Posit32}) = reinterpret(Posit32,0x00a0_0000)
Base.eps(x::AbstractPosit) = max(x-prevfloat(x),nextfloat(x)-x)
9 changes: 6 additions & 3 deletions src/conversions.jl
Expand Up @@ -10,9 +10,6 @@ Base.inttype(::Type{Posit16}) = Int16
Base.inttype(::Type{Posit16_1}) = Int16
Base.inttype(::Type{Posit32}) = Int32

positype(::Type{Float16}) = Posit16
positype

# generic conversion to UInt/Int
Base.unsigned(x::AbstractPosit) = reinterpret(Base.uinttype(typeof(x)),x)
Base.signed(x::AbstractPosit) = reinterpret(Base.inttype(typeof(x)),x)
Expand Down Expand Up @@ -42,6 +39,12 @@ Posit8(x::Posit16) = posit(Posit8,x)
Posit8(x::Posit32) = posit(Posit8,x)
Posit16(x::Posit32) = posit(Posit16,x)

# conversion to and from Posit16_1 via floats as number of exponent bits changes
Posit16_1(x::AbstractPosit) = Posit16_1(float(x))
Posit8(x::Posit16_1) = Posit8(float(x))
Posit16(x::Posit16_1) = Posit16(float(x))
Posit32(x::Posit16_1) = Posit32(float(x))

function posit(::Type{PositN1},x::PositN2) where {PositN1<:AbstractPosit,PositN2<:AbstractPosit}
return reinterpret(PositN1,bitround(Base.uinttype(PositN1),unsigned(x)))
end
Expand Down

2 comments on commit fd3f61b

@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/62354

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.0 -m "<description of version>" fd3f61b673fea5bcf628a0dee00961d43be95d3b
git push origin v0.5.0

Please sign in to comment.