Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of number to Posit8 returns NaR #73

Open
OreganeD opened this issue Oct 5, 2022 · 4 comments · May be fixed by #74
Open

Conversion of number to Posit8 returns NaR #73

OreganeD opened this issue Oct 5, 2022 · 4 comments · May be fixed by #74
Labels
bug Something isn't working rounding

Comments

@OreganeD
Copy link

OreganeD commented Oct 5, 2022

Hello,
I found unexpected results when converting a normal floating point number to a Posit8, I believe a large number should return max_posit instead of NaR

julia> using SoftPosit

julia> Posit8(1e9)
NaR

Have a nice day

@milankl
Copy link
Owner

milankl commented Oct 5, 2022

julia> Posit8(1e8)
Posit8(1.6777216e7)

julia> Posit8(1e9)
NaR

julia> Posit8(1e10)
Posit8(1.6777216e7)

So there's a "bad spot" where the conversion of x>maxpos is to NaR instead of to maxpos. Beyond that conversion returns maxpos correctly. Testing this more thoroughly

julia> for T in (Posit8,Posit16,Posit16_1,Posit32)
           for x in 5:0.1:100
               ~isfinite(T(10.0 ^ x)) && println((T,x,:overflow))
               iszero(T(10.0 ^-x)) && println((T,x,:underflow))
           end
       end
(Posit8, 8.5, :overflow)
(Posit8, 8.6, :overflow)
(Posit8, 8.7, :overflow)
(Posit8, 8.8, :overflow)
(Posit8, 8.9, :overflow)
(Posit8, 9.0, :overflow)
(Posit8, 9.1, :overflow)
(Posit8, 9.2, :overflow)
(Posit8, 9.3, :overflow)
(Posit8, 9.4, :overflow)
(Posit8, 9.5, :overflow)
(Posit8, 9.6, :overflow)
(Posit16, 18.1, :overflow)
(Posit16, 18.2, :overflow)
(Posit16, 18.3, :overflow)
(Posit16, 18.4, :overflow)
(Posit16, 18.5, :overflow)
(Posit16, 18.6, :overflow)
(Posit16, 18.7, :overflow)
(Posit16, 18.8, :overflow)
(Posit16, 18.9, :overflow)
(Posit16, 19.0, :overflow)
(Posit16, 19.1, :overflow)
(Posit16, 19.2, :overflow)
(Posit16_1, 9.1, :overflow)
(Posit16_1, 9.2, :overflow)
(Posit16_1, 9.3, :overflow)
(Posit16_1, 9.4, :overflow)
(Posit16_1, 9.5, :overflow)
(Posit16_1, 9.6, :overflow)
(Posit32, 37.4, :overflow)
(Posit32, 37.5, :overflow)
(Posit32, 37.6, :overflow)
(Posit32, 37.7, :overflow)
(Posit32, 37.8, :overflow)
(Posit32, 37.9, :overflow)
(Posit32, 38.0, :overflow)
(Posit32, 38.1, :overflow)
(Posit32, 38.2, :overflow)
(Posit32, 38.3, :overflow)
(Posit32, 38.4, :overflow)
(Posit32, 38.5, :overflow)

So each posit format with 2 exponent bits has about 1 decade of overflow, Posit16,1 has half a decade of overflow. By refining the range, it seems that overflow occurs between

(useed*maxpos,useed^2*maxpos)

With $u_{seed} = 4,16$ for $1,2$ exponent bits.
I believe this has something to do with shifting in the regime bits here

# combine regime, exponent, mantissa and arithmetic bitshift for 11..110em or 00..001em
regime_exponent_mantissa = regime_bits | exponent_bits | mantissa
regime_exponent_mantissa >>= (abs(k+1) + signbit_e) # arithmetic bitshift

Line 111 here

@milankl milankl linked a pull request Oct 9, 2022 that will close this issue
@milankl
Copy link
Owner

milankl commented Oct 9, 2022

Turns out only a small change was needed to correct for this. The only test that's currently not passing is

julia> Posit32(Inf32)
Posit32(0.0)

julia> Posit32(-Inf32)
Posit32(0.0)

julia> Posit32(NaN32)
Posit32(0.0)

julia> Posit32(-NaN32)
Posit32(0.0)

So only Float32 -> Posit32 the non-finite numbers do something odd. I need to look into this.

@milankl
Copy link
Owner

milankl commented Oct 10, 2022

Then there's apparently also this issue remaining (similar for other formats)

julia> Posit8(1f-8)
Posit8(9.536743e-7)

julia> floatmin(Posit8)
Posit8(5.9604645e-8)

Meaning that a float-value that should be converted to floatmin/minpos of posit, it actually gets converted to the next larger posit.

@milankl
Copy link
Owner

milankl commented Oct 10, 2022

And the issue as already descriped in the issue remains

julia> Posit8(floatmin(Float32))
Posit8(0.0)

julia> Posit8(10*floatmin(Float32))
Posit8(5.9604645e-8)

floats within a multiple of floatmin underflow instead of being converted to floatmin/minpos of the posit format.

@milankl milankl added bug Something isn't working rounding labels Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rounding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants