Jump to content

Posit numbers/encoding

From Rosetta Code
Posit numbers/encoding is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Encode pi as a 8-bit posit with a 2-bit exponent.

As an unsigned integer, the result should be 77.

BASIC

BASIC256

Translation of: FreeBASIC
print positEncode(4 * atan(1), 8, 2)
end

function positEncode(x, nbits, es)
	npat = 2 ^ nbits
	useed = 2 ^ (2 ^ es)
	e = 2 ^ (es - 1)
	y = abs(x)

	if y = 0 then return 0
	if y > 1e308 then return 2 ^ (nbits - 1)

	if y >= 1 then
		p = 1
		i = 2
		while y >= useed and i < nbits
			p = 2 * p + 1
			y /= useed
			i += 1
		end while
		p *= 2
		i += 1
	else
		p = 0
		i = 1
		while y < 1 and i <= nbits
			y *= useed
			i += 1
		end while
		if i >= nbits then
			p = 2
			i = nbits + 1
		else
			p = 1
			i += 1
		end if
	end if

	while e > 0.5 and i <= nbits
		p *= 2
		if y >= 2 * e then
			y /= 2 ^ e
			p += 1
		end if
		e /= 2
		i += 1
	end while
	y -= 1

	while y > 0 and i <= nbits
		y *= 2
		p = 2 * p + int(y)
		y -= int(y)
		i += 1
	end while
	p *= 2 ^ (nbits + 1 - i)
	i += 1
	i = p and 1
	p = int(p / 2)
	if i <> 0 then
		if y = 1 or y = 0 then
			p += (p and 1)
		else
			p += 1
		end if
	end if

	if x < 0 then return ((npat - p) mod npat) else return (p mod npat)
end function

Chipmunk Basic

Translation of: FreeBASIC
Works with: Chipmunk Basic version 3.6.4
100 cls
110 sub positencode(x)
120  if nbits = 0 then nbits = 8
130  if es = 0 then es = 2
140  npat = 2^nbits
150  useed = 2^(2^es)
160  e = 2^(es-1)
170  y = abs(x)
180  if y = 0 then positencode = 0
190  if y > 1.000000E+308 then positencode = 2^(nbits-1)
200  if y >= 1 then
210   p = 1
220   i = 2
230   while y >= useed and i < nbits
240    p = 2*p+1
250    y = y/useed
260    i = i+1
270   wend
280   p = p*2
290   i = i+1
300  else
310   p = 0
320   i = 1
330   while y < 1 and i <= nbits
340    y = y*useed
350    i = i+1
360   wend
370   if i >= nbits then
380    p = 2
390    i = nbits+1
400   else
410    p = 1
420    i = i+1
430   endif
440  endif
450  while e > 0.5 and i <= nbits
460   p = p*2
470   if y >= 2*e then
480    y = y/(2^e)
490    p = p+1
500   endif
510   e = e/2
520   i = i+1
530  wend
540  y = y-1
550  while y > 0 and i <= nbits
560   y = y*2
570   p = 2*p+int(y)
580   y = y-int(y)
590   i = i+1
600  wend
610  p = p*(2^(nbits+1-i))
620  i = i+1
630  i = p and 1
640  p = int(p/2)
650  if i <> 0 then
660   if y = 1 or y = 0 then
670    p = p+(p and 1)
680   else
690    p = p+1
700   endif
710  endif
720  if x < 0 then
730   positencode = ((npat-p) mod npat)
740  else
750   positencode = (p mod npat)
760  endif
770 end sub
780 print positencode(4*atan(1))
790 end

FreeBASIC

Translation of: Phix
#define IsInf(x)  Abs(x) > 1E308

Function positEncode(x As Double, nbits As Uinteger = 8, es As Uinteger = 2) As Uinteger
    Dim As Double npat = 2 ^ nbits
    Dim As Double useed = 2 ^ (2 ^ es)
    Dim As Double e = 2 ^ (es - 1)
    Dim As Double y = Abs(x)
    Dim As Uinteger i, p
    
    If y = 0 Then Return 0
    If IsInf(y) Then Return 2 ^ (nbits - 1)
    
    If y >= 1 Then
        p = 1
        i = 2
        While y >= useed And i < nbits
            p = 2 * p + 1
            y /= useed
            i += 1
        Wend
        p *= 2
        i += 1
    Else
        p = 0
        i = 1
        While y < 1 And i <= nbits
            y *= useed
            i += 1
        Wend
        If i >= nbits Then
            p = 2
            i = nbits + 1
        Else
            p = 1
            i += 1
        End If
    End If
    
    While e > 0.5 And i <= nbits
        p *= 2
        If y >= 2 * e Then
            y /= 2 ^ e
            p += 1
        End If
        e /= 2
        i += 1
    Wend
    y -= 1
    
    While y > 0 And i <= nbits
        y *= 2
        p = 2 * p + Int(y)
        y -= Int(y)
        i += 1
    Wend
    p *= 2 ^ (nbits + 1 - i)
    i += 1
    i = p And 1
    p = Int(p / 2)
    If i <> 0 Then p += Iif((y = 1 Or y = 0), p And 1, 1)
    
    Return Iif(x < 0, npat - p, p) Mod npat
End Function

Print positEncode(4 * Atn(1))

Sleep
Output:
77

Gambas

Translation of: FreeBASIC
Function positEncode(x As Float, nbits As Integer, es As Integer) As Integer 

  Dim npat As Integer = 2 ^ nbits 
  Dim useed As Float = 2 ^ (2 ^ es) 
  Dim e As Float = 2 ^ (es - 1) 
  Dim y As Float = Abs(x) 
  Dim i As Integer, p As Integer
  
  If y = 0 Then Return 0 
  If IsInf(y) Then Return 2 ^ (nbits - 1) 
  
  If y >= 1 Then 
    p = 1 
    i = 2 
    While y >= useed And i < nbits 
      p = 2 * p + 1 
      y /= useed 
      i += 1 
    Wend 
    p *= 2 
    i += 1 
  Else 
    p = 0 
    i = 1 
    While y < 1 And i <= nbits 
      y *= useed 
      i += 1 
    Wend 
    If i >= nbits Then 
      p = 2 
      i = nbits + 1 
    Else 
      p = 1 
      i += 1 
    End If 
  End If 
  
  While e > 0.5 And i <= nbits 
    p *= 2 
    If y >= 2 * e Then 
      y /= 2 ^ e 
      p += 1 
    End If 
    e /= 2 
    i += 1 
  Wend 
  y -= 1 
  
  While y > 0 And i <= nbits 
    y *= 2 
    p = 2 * p + Int(y) 
    y -= Int(y) 
    i += 1 
  Wend 
  p *= 2 ^ (nbits + 1 - i) 
  i += 1 
  i = p And 1 
  p = Int(p / 2) 
  If i <> 0 Then p += IIf((y = 1 Or y = 0), p And 1, 1) 
  
  Return IIf(x < 0, npat - p, p) Mod npat

End Function

Public Sub Main()  
  
  Print positEncode(4 * Atn(1), 8, 2)
  
End

PureBasic

Procedure.i positEncode(x.d, nbits.i = 8, es.i = 2)
  Protected npat.i, useed.d, e.d, y.d
  Protected i.i, p.i
  
  npat = Pow(2, nbits)
  useed = Pow(2, Pow(2, es))
  e = Pow(2, es - 1)
  y = Abs(x)
  
  If y = 0
    ProcedureReturn 0
  EndIf
  
  If y > 1E308
    ProcedureReturn Pow(2, nbits - 1)
  EndIf
  
  If y >= 1
    p = 1
    i = 2
    While y >= useed And i < nbits
      p = 2 * p + 1
      y / useed
      i + 1
    Wend
    p * 2
    i + 1
  Else
    p = 0
    i = 1
    While y < 1 And i <= nbits
      y * useed
      i + 1
    Wend
    If i >= nbits
      p = 2
      i = nbits + 1
    Else
      p = 1
      i + 1
    EndIf
  EndIf
  
  While e > 0.5 And i <= nbits
    p * 2
    If y >= 2 * e
      y / Pow(2, e)
      p + 1
    EndIf
    e / 2
    i + 1
  Wend
  y - 1
  
  While y > 0 And i <= nbits
    y * 2
    p = 2 * p + Int(y)
    y - Int(y)
    i + 1
  Wend
  p * Pow(2, nbits + 1 - i)
  i + 1
  i = p & 1
  p = Int(p / 2)
  If i <> 0
    If y = 1 Or y = 0
      p + (p & 1)
    Else
      p + 1
    EndIf
  EndIf
  
  If x < 0
    ProcedureReturn (npat - p) % npat
  Else
    ProcedureReturn p % npat
  EndIf
EndProcedure

OpenConsole()
PrintN(Str(positEncode(4 * ATan(1))))
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()

QBasic

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5

The QB64 solution works without any changes.

QB64

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5
Print positEncode(4 * Atn(1), 8, 2)

Function positEncode (x, nbits, es)
    Dim npat As Double, useed As Double
    Dim e As Double, y As Double
    Dim i As Integer, p As Integer

    npat = 2 ^ nbits
    useed = 2 ^ (2 ^ es)
    e = 2 ^ (es - 1)
    y = Abs(x)
    If y = 0 Then positEncode = 0: Exit Function
    If y > 1E+38 Then positEncode = 2 ^ (nbits - 1): Exit Function

    If y >= 1 Then
        p = 1
        i = 2
        While y >= useed And i < nbits
            p = 2 * p + 1
            y = y / useed
            i = i + 1
        Wend
        p = p * 2
        i = i + 1
    Else
        p = 0
        i = 1
        While y < 1 And i <= nbits
            y = y * useed
            i = i + 1
        Wend
        If i >= nbits Then
            p = 2
            i = nbits + 1
        Else
            p = 1
            i = i + 1
        End If
    End If

    While e > 0.5 And i <= nbits
        p = p * 2
        If y >= 2 * e Then
            y = y / (2 ^ e)
            p = p + 1
        End If
        e = e / 2
        i = i + 1
    Wend
    y = y - 1

    While y > 0 And i <= nbits
        y = y * 2
        p = 2 * p + Int(y)
        y = y - Int(y)
        i = i + 1
    Wend
    p = p * (2 ^ (nbits + 1 - i))
    i = i + 1
    i = p And 1
    p = Int(p / 2)
    If i <> 0 Then
        If y = 1 Or y = 0 Then
            p = p + (p And 1)
        Else
            p = p + 1
        End If
    End If
    If x < 0 Then positEncode = ((npat - p) Mod npat) Else positEncode = (p Mod npat)
End Function

True BASIC

Translation of: QBasic
FUNCTION positencode(x, nbits, es)
    LET npat = 2^nbits
    LET useed = 2^(2^es)
    LET e = 2^(es-1)
    LET y = ABS(x)
    IF y = 0 THEN
       LET positencode = 0
       EXIT FUNCTION
    END IF
    IF y > 1e308 THEN
       LET positencode = 2^(nbits-1)
       EXIT FUNCTION
    END IF
    IF y >= 1 THEN
       LET p = 1
       LET i = 2
       DO WHILE y >= useed AND i < nbits
          LET p = 2*p+1
          LET y = y/useed
          LET i = i+1
       LOOP
       LET p = p*2
       LET i = i+1
    ELSE
       LET p = 0
       LET i = 1
       DO WHILE y < 1 AND i <= nbits
          LET y = y*useed
          LET i = i+1
       LOOP
       IF i >= nbits THEN
          LET p = 2
          LET i = nbits+1
       ELSE
          LET p = 1
          LET i = i+1
       END IF
    END IF
    DO WHILE e > 0.5 AND i <= nbits
       LET p = p*2
       IF y >= 2*e THEN
          LET y = y/(2^e)
          LET p = p+1
       END IF
       LET e = e/2
       LET i = i+1
    LOOP
    LET y = y-1
    DO WHILE y > 0 AND i <= nbits
       LET y = y*2
       LET p = 2*p+INT(y)
       LET y = y-INT(y)
       LET i = i+1
    LOOP
    LET p = p*(2^(nbits+1-i))
    LET i = i+1
    !LET i = p AND 1
    IF REMAINDER(p, 2) = 1 THEN
       LET i = 1
    ELSE
       LET i = 0
    END IF
    LET p = INT(p/2)
    IF i <> 0 THEN
       IF y = 1 OR y = 0 THEN
          !LET p = p + (p AND 1)
          IF REMAINDER(p, 2) = 1 THEN
             LET p = p + 1
          END IF
       ELSE
          LET p = p+1
       END IF
    END IF
    IF x < 0 THEN LET positencode = REMAINDER((npat-p),npat) ELSE LET positencode = REMAINDER(p,npat)
END FUNCTION

PRINT positencode(4*ATN(1), 8, 2)
END

Yabasic

Translation of: FreeBASIC
print posit_encode(4 * atan(1))
end

sub posit_encode(x, nbits, es)
    if not nbits  nbits = 8
    if not es  es = 2
	
    npat = 2 ^ nbits
    useed = 2 ^ (2 ^ es)
    e = 2 ^ (es - 1)
    y = abs(x)
	
    if y = 0  return 0
    if y > 1e308  return 2 ^ (nbits - 1)
	
    if y >= 1 then
        p = 1
        i = 2
        while y >= useed and i < nbits
            p = 2 * p + 1
            y = y / useed
            i = i + 1
        wend
        p = p * 2
        i = i + 1
    else
        p = 0
        i = 1
        while y < 1 and i <= nbits
            y = y * useed
            i = i + 1
        wend
        if i >= nbits then
            p = 2
            i = nbits + 1
        else
            p = 1
            i = i + 1
        fi
    fi

    while e > 0.5 and i <= nbits
        p = p * 2
        if y >= 2 * e then
            y = y / (2 ^ e)
            p = p + 1
        fi
        e = e / 2
        i = i + 1
    wend
    y = y - 1

    while y > 0 and i <= nbits
        y = y * 2
        p = 2 * p + int(y)
        y = y - int(y)
        i = i + 1
    wend
    p = p * (2 ^ (nbits + 1 - i))
    i = i + 1
    i = p and 1
    p = int(p / 2)
    if i <> 0  if y = 1 or y = 0 then p = p + (p and 1) else p = p + 1 : fi
	
	if x < 0 then return mod((npat - p), npat) else return mod(p, npat) : fi
end sub

JavaScript

Translation of: Wren
/* Copyright © 2017 John L . Gustafson
 * 
 * Permission is hereby granted, free of charge to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction including without limitation the rights to use
 * copy, modify, merge, publish, distribute, sub - license, and/or sell copies of
 * the Software and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions :
 *    
 * This copyright and permission notice shall be included in all copies or
 * substantial portions of the software .
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OR CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE .
 */

const nbits = 8
const es = 2
const npat = 1 << nbits
const useed = 1 << (1 << es)

function x2p(x) {
    "use strict";
    let i, p,
        e = 1 << (es - 1),
        y = Math.abs(x);
    if (y == 0) return 0
    if (y == Math.Infinity) return 1 << (nbits - 1)
    if (y >= 1) {
        p = 1
        i = 2
        while (y >= useed && i < nbits) {
            p = 2 * p + 1
            y = y / useed
            i = i + 1
        }
        p = 2 * p
        i = i + 1
    } else {
        p = 0
        i = 1
        while (y < 1 && i <= nbits) {
            y = y * useed
            i = i + 1
        }
        if (i >= nbits) {
            p = 2
            i = nbits + 1
        } else {
            p = 1
            i = i + 1
        }
    }

    while (e > 0.5 && i <= nbits) {
        p = 2 * p
        if (y >= 2 * e) {
            y = y / (1 << e)
            p = p + 1
        }
        e = e / 2
        i = i + 1
    }
    y = y - 1

    while (y > 0 && i <= nbits) {
        y = 2 * y
        p  = 2 * p + Math.floor(y)
        y = y - Math.floor(y)
        i = i + 1
    }
    p = p * (1 << (nbits + 1 - i))
    i = i + 1
    i = p & 1
    p = Math.floor((p/2))
    if (i != 0) {
        if (y == 1 || y == 0) {
            p = p + (p & 1)
        } else {
            p = p + 1
        }
    }
    return (x < 0 ? npat - p : p) % npat
}

console.log(x2p(Math.PI));
Output:
77

Julia

""" Posit floating point numbers """
struct PositType3{T<:Integer}
    numbits::UInt16
    es::UInt16
    bits::T
    PositType3(nb, ne, i) = new{typeof(i)}(UInt16(nb), UInt16(ne), i)
end

""" Convert PositType3 to Rational. See also posithub.org/docs/Posits4.pdf """
function Base.Rational(p::PositType3)
    s = signbit(signed(p.bits))              # s for S signbit, is 1 if negative
    pabs = p.bits << 1                       # Shift off signbit (adds a 0 to F at LSB)
    pabs == 0 && return s ? 1 // 0 : 0 // 1  # If p is 0, return 0 or if s 1 error
    s && (pabs = (-p.bits) << 1)             # If p is negative, flip to 2's complement
    expsign = signbit(signed(pabs))          # Exponent sign from 2nd bit now MSB 
    r = expsign == 1 ? leading_ones(pabs) : leading_zeros(pabs) # r regime R size
    k = expsign ? r - 1 : -r                 # k for the exponent calculation
    pabs <<= (r + 1)                         # Shift off unwanted R bits
    pabs >>= (r + 2)                         # Shift back for E, F
    fsize = p.numbits - 1 - r - 1 - p.es     # Check how many F bits explicit
    e = fsize < 1 ? pabs : pabs >> fsize     # Get E value, then F value next line
    f = fsize < 1 ? 1 // 1 : big"1" + (pabs & (2^fsize - 1)) // big"2"^fsize
    pw = 2^p.es * k + e                      # pw multiplier, power of 2 exponent
    return pw >= 0 ? (-1)^s * f * big"2"^pw // 1 : (-1)^s * f // big"2"^(-pw)
end

""" Get bits representation of a posit of size numbits and from a real number """
function positbits(x::Real, numbits, es)
    tindex = Int(round(log2(numbits / 8))) + 1 # choice of output type
    1 <= tindex <= 5 || error("Cannot create posit of bit size $numbits")
    T = [UInt8, UInt16, UInt32, UInt64, UInt128][tindex]
    x == 0 && return zero(T)                 # bits for 0 if 0, Inf if Inf, etc
    x in [-Inf, Inf, NaN] && return typemax(T) - typemax((signed(typemax(T))))
    s = x < 0                                # sign bit, 1 if negative
    xabs = abs(x)                            # work with abs(x)
    useed = 2^es                             # the useed
    pw = Int(floor(log2(xabs)))              # xabs =  1.bits.. * 2^pw
    k, e = divrem(pw, useed)                 # from pw = 2^p.es * k + e
    if e < 0
        k, e = k - 1, e + useed              # e must be unsigned
    end
    r = k < 0 ? -k : k + 1                   # r is number of R repetitions
    rbits = pw >= 0 ? (2^(r+1)-1)  1 : 01   # bit pattern of R portion
    fsize = numbits - 1 - r - 1 - es         # size of F portion
    f = round((xabs / (2^pw) - 1) * 2^fsize) # f (mantissa - 1 as binary digits)
    pabs = T(f) | T(e << fsize) | T(BigInt(rbits) << (fsize + es)) # rbits | e | f
    return s ? -pabs : pabs                  # S and two's complement if negative
end

""" Construct various bit sizes of Posit """
posit8(x, es = 2) = PositType3(8, 2, positbits(x, 8, es))
posit16(x, es = 2) = PositType3(16, 2, positbits(x, 16, es))
posit32(x, es = 2) = PositType3(32, 2, positbits(x, 32, es))
posit64(x, es = 2) = PositType3(64, 2, positbits(x, 64, es))

const tests = [0, Inf, 1, -1, π, -π, 10π, -10π]

for t in tests, posit in (posit8, posit16, posit32, posit64)
    p = posit(t)
    i = signed(p.bits)
    ending = BigFloat(Rational(p))
    err = Float64(abs(t - ending))
    println("\n$t to $(p.numbits)-bit posit is $p.")
    println("This posit reinterpreted as integer is $i.")
    println("This posit as float is $ending,\n  with error $err.")
end
Output:
0.0 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0x00).
This posit reinterpreted as integer is 0.
This posit as float is 0.0,
  with error 0.0.

0.0 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0x0000).
This posit reinterpreted as integer is 0.
This posit as float is 0.0,
  with error 0.0.

0.0 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0x00000000).
This posit reinterpreted as integer is 0.
This posit as float is 0.0,
  with error 0.0.

0.0 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0x0000000000000000).
This posit reinterpreted as integer is 0.
This posit as float is 0.0,
  with error 0.0.

Inf to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0x80).
This posit reinterpreted as integer is -128.
This posit as float is Inf,
  with error NaN.

Inf to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0x8000).
This posit reinterpreted as integer is -32768.
This posit as float is Inf,
  with error NaN.

Inf to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0x80000000).
This posit reinterpreted as integer is -2147483648.
This posit as float is Inf,
  with error NaN.

Inf to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0x8000000000000000).
This posit reinterpreted as integer is -9223372036854775808.
This posit as float is Inf,
  with error NaN.

1.0 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0x40).
This posit reinterpreted as integer is 64.
This posit as float is 1.0,
  with error 0.0.

1.0 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0x4000).
This posit reinterpreted as integer is 16384.
This posit as float is 1.0,
  with error 0.0.

1.0 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0x40000000).
This posit reinterpreted as integer is 1073741824.
This posit as float is 1.0,
  with error 0.0.

1.0 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0x4000000000000000).
This posit reinterpreted as integer is 4611686018427387904.
This posit as float is 1.0,
  with error 0.0.

-1.0 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0xc0).
This posit reinterpreted as integer is -64.
This posit as float is -1.0,
  with error 0.0.

-1.0 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0xc000).
This posit reinterpreted as integer is -16384.
This posit as float is -1.0,
  with error 0.0.

-1.0 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0xc0000000).
This posit reinterpreted as integer is -1073741824.
This posit as float is -1.0,
  with error 0.0.

-1.0 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0xc000000000000000).
This posit reinterpreted as integer is -4611686018427387904.
This posit as float is -1.0,
  with error 0.0.

3.141592653589793 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0x4d).
This posit reinterpreted as integer is 77.
This posit as float is 3.25,
  with error 0.10840734641020688.

3.141592653589793 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0x4c91).
This posit reinterpreted as integer is 19601.
This posit as float is 3.1416015625,
  with error 8.908910206884002e-6.

3.141592653589793 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0x4c90fdaa).
This posit reinterpreted as integer is 1284570538.
This posit as float is 3.1415926516056060791015625,
  with error 1.984187036896401e-9.

3.141592653589793 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0x4c90fdaa22168c00).
This posit reinterpreted as integer is 5517188450687028224.
This posit as float is 3.141592653589793115997963468544185161590576171875,
  with error 0.0.

-3.141592653589793 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0xb3).
This posit reinterpreted as integer is -77.
This posit as float is -3.25,
  with error 0.10840734641020688.

-3.141592653589793 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0xb36f).
This posit reinterpreted as integer is -19601.
This posit as float is -3.1416015625,
  with error 8.908910206884002e-6.

-3.141592653589793 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0xb36f0256).
This posit reinterpreted as integer is -1284570538.
This posit as float is -3.1415926516056060791015625,
  with error 1.984187036896401e-9.

-3.141592653589793 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0xb36f0255dde97400).
This posit reinterpreted as integer is -5517188450687028224.
This posit as float is -3.141592653589793115997963468544185161590576171875,
  with error 0.0.

31.41592653589793 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0x64).
This posit reinterpreted as integer is 100.
This posit as float is 32.0,
  with error 0.5840734641020688.

31.41592653589793 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0x63db).
This posit reinterpreted as integer is 25563.
This posit as float is 31.421875,
  with error 0.00594846410206884.

31.41592653589793 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0x63da9e8a).
This posit reinterpreted as integer is 1675271818.
This posit as float is 31.415926456451416015625,
  with error 7.944651514435463e-8.

31.41592653589793 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0x63da9e8a554e1780).
This posit reinterpreted as integer is 7195237671651645312.
This posit as float is 31.41592653589793115997963468544185161590576171875,
  with error 0.0.

-31.41592653589793 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0x9c).
This posit reinterpreted as integer is -100.
This posit as float is -32.0,
  with error 0.5840734641020688.

-31.41592653589793 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0x9c25).
This posit reinterpreted as integer is -25563.
This posit as float is -31.421875,
  with error 0.00594846410206884.

-31.41592653589793 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0x9c256176).
This posit reinterpreted as integer is -1675271818.
This posit as float is -31.415926456451416015625,
  with error 7.944651514435463e-8.

-31.41592653589793 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0x9c256175aab1e880).
This posit reinterpreted as integer is -7195237671651645312.
This posit as float is -31.41592653589793115997963468544185161590576171875,
  with error 0.0.

Mathematica

John Gustafson's code.

(*
 * Copyright © 2017 John L . Gustafson
 *
 * Permission is hereby granted, free of charge to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction including without limitation the
 * rights to use copy, modify, merge, publish, distribute, sub - license,
 * and/or sell copies of the Software and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions :
 *    
 * This copyright and permission notice shall be included in all copies or
 * substantial portions of the software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OR CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *)

setpositenv[{n_Integer /; n >= 2, e_Integer /; e >= 0}] := (
  {nbits, es} = {n, e};
  npat = 2^nbits;
  useed = 2^2^es;
  {minpos, maxpos} = {useed^(-nbits + 2), useed^(nbits - 2)};
  qsize = Power[2, Ceiling[Log[2, (nbits - 2) 2^(es + 2) + 5]]];
  qextra = qsize - (nbits - 2) 2^(es + 2);
  )
positableQ[x_] := (Abs[x] ==  || x  Reals)
x2p[x_ /; positableQ[x]] := Module[
  {i, p, e = 2^(es - 1), y = Abs[x]},
  Which[
   (* First, take care of the two exception values: *)
   y == 0, 0, (* all 0 bits s *)
   y == , BitShiftLeft[1, nbits - 1], (* 1 followed by all 0 bits *)
   True,
   If[
    y >= 1, (* Northeast quadrant: *)
    p = 1; i = 2; (* Shift in 1s from the right and scale down. *)
    
    While[y >= useed && 
      i < nbits, {p, y, i} = {2 p + 1, y/useed, i + 1}]; 
    p = 2 p; i++,
    (* Else, southeast quadrant: *)
    p = 0; i = 1; (* Shift in 0s from the right and scale up. *)
    
    While[y < 1 && i <= nbits, {y, i} = {y useed, i + 1}];
    If[i >= nbits, p = 2; i = nbits + 1, p = 1; i++]
    ];(* Extract exponent bits: *)
   
   While[e > 1/2 && i <= nbits, p = 2 p; 
    If[y >= 2^e, y /= 2^e; p++]; e /= 2; i++];
   y--; (* Fraction bits; subtract the hidden bit *)
   
   While[y > 0 && i <= nbits, y = 2 y; 
    p = 2 p + y; 
    y -= y; i++];
   p *= 2^(nbits + 1 - i); i++;(* Round to nearest; tie goes to even *)

    i = BitAnd[p, 1]; p = p/2;
   p = Which[
     i == 0, p, (* closer to lower value *)
     y == 1 || y == 0, 
     p + BitAnd[p, 1], (* tie goes to nearest even *)
     True, 
     p + 1 (* closer to upper value *)];

   Mod[If[x < 0, npat - p, p], npat (* Simulate 2's complement *)]
   ] 
  ]
setpositenv[{8,2}];
x2p @ Pi
Output:
77

Perl

Translation of: Raku
# 20240928 Perl programming solution

use strict;
use warnings;
use POSIX qw(INFINITY);

my ($npat, $useed) = (1 << (my $nbits = 8), 1 << (1 << (my $es = 2)));

sub x2p {
   my $x = shift;
   my ($e, $y, $i, $p) = (1 << ($es - 1), abs($x));
   
   return 0 if ($y == 0);
   return (1 << ($nbits - 1)) if ($y == INFINITY);

   if ($y >= 1) {
      ($p, $i) = (1, 2);
      while ($y >= $useed && $i < $nbits) {
         $p += $p + 1;
         $y /= $useed;
         $i++;
      }
      $p += $p;
      $i++;
   } else {
      ($p, $i) = (0, 1);
      while ($y < 1 && $i <= $nbits) {
         $y *= $useed;
         $i++;
      }
      if ($i >= $nbits) {
         ($p, $i) = (2, $nbits + 1);
      } else {
         $p = 1;
         $i++;
      }
   }

   while ($e > 0.5 && $i <= $nbits) {
      $p = 2 * $p;
      if ($y >= 2 * $e) {
         $y /= (1 << $e);
         $p++;
      }
      $e /= 2;
      $i++;
   }
   $y -= 1;

   while ($y > 0 && $i <= $nbits) {
      $y *= 2;
      $p = 2 * $p + int($y);
      $y -= int($y);
      $i++;
   }
   $p *= (1 << ($nbits + 1 - $i));
   $i++;
   my $i_tmp = $p & 1;
   $p >>= 1;
   if ($i_tmp != 0) { ($y == 1 || $y == 0) ? $p += ($p & 1) : $p++ }
   return ($x < 0 ? $npat - $p : $p) % $npat;
}

print x2p(3.14159265358979), "\n";

You may Attempt This Online!

Phix

Translation of: JavaScript
with javascript_semantics
function posit_encode(atom x, integer nbits=8, es=2)
    atom npat = power(2,nbits),
        useed = power(2,power(2,es)),
            e = power(2,es-1),
            y = abs(x), i, p
    if y == 0 then return 0 end if
    if is_inf(y) then return power(2,nbits-1) end if
    if y>=1 then
        p = 1
        i = 2
        while y>=useed and i<nbits do
            p = 2 * p + 1
            y /= useed
            i += 1
        end while
        p *= 2
        i += 1
    else
        p = 0
        i = 1
        while y<1 and i<=nbits do
            y *= useed
            i += 1
        end while
        if i>=nbits then
            p = 2
            i = nbits + 1
        else
            p = 1
            i += 1
        end if
    end if

    while e>0.5 and i<=nbits do
        p *= 2
        if y>=2*e then
            y /= power(2,e)
            p += 1
        end if
        e /= 2
        i = i + 1
    end while
    y -= 1

    while y>0 and i<=nbits do
        y *= 2
        p  = 2 * p + floor(y)
        y -= floor(y)
        i += 1
    end while
    p = p * power(2,nbits+1-i)
    i += 1
    i = and_bits(p,1)
    p = floor(p/2)
    if i!=0 then
        if y=1 or y=0 then
            p += and_bits(p,1)
        else
            p += 1
        end if
    end if
    return remainder(iff(x<0 ? npat-p : p),npat)
end function

?posit_encode(PI)
Output:
77

Can also re-encode all the outputs from the decode task perfectly.

Raku

Translation of: Mathematica
=begin LICENSE
Copyright © 2017 John L . Gustafson

Permission is hereby granted, free of charge to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction including without limitation the rights to use copy, modify, merge, publish, distribute, sub - license, and/or sell copies of the Software and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
   
This copyright and permission notice shall be included in all copies or substantial portions of the software .

THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OR CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE .
=end LICENSE

# L<https://posithub.org/docs/Posits4.pdf>
constant nbits = 8;
constant es    = 2;
constant npat  = 2**nbits;
constant useed = 2**2**es;
constant minpos = useed**(-nbits + 2);
constant maxpos = useed**(+nbits - 2);
constant qsize  = 2**((nbits-2)*2**(es+2)+5).log2.ceiling;
constant qextra = qsize - (nbits - 2)*2**(es+2);

constant posit-range = ^npat;
subset posit of UInt where posit-range;

sub x2p(Real $x --> posit) {

  # first, take care of the two exceptional values
  return 0 if $x == 0;
  return npat div 2 if $x == Inf;

  # working variables
  my ($i, $p, $e, $y) = $, $, 2**(es - 1), $x.abs;


  if $y1 { # north-east quadrant
    ($p, $i) = 1, 2;
    # Shift in 1s from the right and scale down.
    ($p, $y, $i) = 2*$p+1, $y/useed, $i+1 while $yuseed && $inbits; 
    $p *= 2; $i++;
  } else { # south-east quadrant
    ($p, $i) = 0, 1;
    # Shift in 0 s from the right and scale up.
    ($y, $i) = $y*useed, $i+1 while $y < 1 && $inbits;
    if $inbits {
      $p = 2; $i = nbits + 1;
    } else { $p = 1; $i++; }
  }
  # Extract exponent bits:
  while $e > 1/2 and $inbits {
    $p *= 2;
    if $y2**$e { $y /= 2**$e; $p++ }
    $e /= 2;
    $i++;
  }
  $y--;
  # Fraction bits; substract the hidden bit
  while $y > 0 and $inbits {
    $y *= 2;
    $p = 2*$p + $y.floor;
    $y -= $y.floor;
    $i++
  }
  $p *= 2**(nbits+1-$i);
  $i++;

  # Round to nearest; tie goes to even
  $i = $p +& 1;
  $p = ($p/2).floor;
  $p = $i == 0 ?? $p !!
       $y == 0|1 ?? $p + $p+&1 !!
       $p + 1;

  # Simulate 2's complement
  ($x < 0 ?? npat - $p !! $p) mod npat;

}

say x2p pi;
Output:
77

You may Attempt This Online!

Wren

Translation of: Mathematica
/* See original Mathematica example for copyright notice and comments. */

var nbits = 8
var es = 2
var npat = 1 << nbits
var useed = 1 << (1 << es)

var x2p = Fn.new { |x|
    var i
    var p
    var e = 1 << (es - 1)
    var y = x.abs
    if (y == 0) return 0
    if (y.isInfinity) return 1 << (nbits - 1)
    if (y >= 1) {
        p = 1
        i = 2
        while (y >= useed && i < nbits) {
            p = 2 * p + 1
            y = y / useed
            i = i + 1
        }
        p = 2 * p
        i = i + 1
    } else {
        p = 0
        i = 1
        while (y < 1 && i <= nbits) {
            y = y * useed
            i = i + 1
        }
        if (i >= nbits) {
            p = 2
            i = nbits + 1
        } else {
            p = 1
            i = i + 1
        }
    }

    while (e > 0.5 && i <= nbits) {
        p = 2 * p
        if (y >= 2 * e) {
            y = y / (1 << e)
            p = p + 1
        }
        e = e / 2
        i = i + 1
    }
    y = y - 1

    while (y > 0 && i <= nbits) {
        y = 2 * y
        p  = 2 * p + y.floor
        y = y - y.floor
        i = i + 1
    }
    p = p * (1 << (nbits + 1 - i))
    i = i + 1
    i = p & 1
    p = (p/2).floor
    if (i != 0) {
        if (y == 1 || y == 0) {
            p = p + (p & 1)
        } else {
            p = p + 1
        }
    }
    return (x < 0 ? npat - p : p) % npat
}

System.print(x2p.call(Num.pi))
Output:
77
Cookies help us deliver our services. By using our services, you agree to our use of cookies.