# Posit numbers/encoding

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.

## 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`

## 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 .

# 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 \$y ≥ 1 { # 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 \$y ≥ useed && \$i ≤ nbits;
\$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 && \$i ≤ nbits;
if \$i ≥ nbits {
\$p = 2; \$i = nbits + 1;
} else { \$p = 1; \$i++; }
}
# Extract exponent bits:
while \$e > 1/2 and \$i ≤ nbits {
\$p *= 2;
if \$y ≥ 2**\$e { \$y /= 2**\$e; \$p++ }
\$e /= 2;
\$i++;
}
\$y--;
# Fraction bits; substract the hidden bit
while \$y > 0 and \$i ≤ nbits {
\$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`

## 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
```