Posit numbers/encoding: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Undo revision 356089 by Wherrera (talk))
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 4 users not shown)
Line 100:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">struct""" PositType3{T<:Integer}Posit floating point numbers """
struct PositType3{T<:Integer}
numbits::UInt16
es::UInt16
Line 119 ⟶ 120:
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 # Get F value
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")
typT = [UInt8, UInt16, UInt32, UInt64, UInt128][tindex]
x == 0 && return zero(typT) # bits for 0 if 0, Inf if Inf, etc
x in [-Inf, Inf, NaN] && return typemax(typT) - typemax((signed(typemax(typT))))
s = x < 0 # sign bit, 1 if negative
s = x < 0
xabs = abs(x) # work with abs(x)
useed = 2^es # the useed
expopw = Int(floor(log2(xabs))) # x can be written as # xabs = 1.bits.. * 2^expopw
k, e = divrem(pw, useed) # from pw = 2^p.es * k + e
expneg = expo - 1 <= 0
if e < 0
k = expneg ? -(expo - useed + 1) ÷ useed : expo ÷ useed
k, e = expnegk ?- k1, *e + useed + expo : expo - k * useed # e must be unsigned
end
r = expneg ? k + 1 : -k
rbitsr = expnegk < 0 ? (2^(r-k : k + 1) - 1) 1 : 01 # r is number of R repetitions
fsizerbits = numbitspw ->= 10 -? (2^(r +1)-1) ⊻ 1 -: 01 # bit pattern of R esportion
fsize = numbits - 1 - r - 1 - es # size of F portion
f = UInt(round((x/(2^expo) - 1) * 2^fsize))
pabsf = typround(f)(xabs |/ typ(e2^pw) <<- fsize1) |* typ(rbits2^fsize) <<# f (fsizemantissa - 1 as +binary esdigits)
#@showpabs s= expoT(f) expneg k| T(e r<< fsize) | T(BigInt(rbits) << (fsize f+ pabses)) # rbits | e | f
return s ? -pabs : pabs # S and two's complement if negative
end
 
""" Construct various bit sizes of Posit """
posit8(x, es) = PositType3(8, 2, positbits(x, 8, es))
posit16posit8(x, es = 2) = PositType3(168, 2, positbits(x, 168, es))
posit32posit16(x, es = 2) = PositType3(3216, 2, positbits(x, 3216, es))
posit64posit32(x, es = 2) = PositType3(6432, 2, positbits(x, 6432, es))
posit64(x, es = 2) = PositType3(64, 2, positbits(x, 64, es))
 
const tests = [0, Inf, 1, -1, π, -π, 10π, -10π]
for constructor in (posit8, posit16, posit32, posit64)
 
p = constructor(π, 2)
for t in tests, posit in (posit8, posit16, posit32, posit64)
p = posit(t)
i = signed(p.bits)
endingπending = floatBigFloat(Rational(p))
err = Float64(abs(πt - endingπending))
println("\n$t to $(p.numbits)-bit posit is $p.")
println("This posit with bits reinterpreted as integer is $i.")
println("This posit as float is $endingπending,\n with error $err.")
end
</syntaxhighlight>{{out}}
<div id="" style="overflow:scroll; height:120px;">
<pre>
π0.0 to 8-bit posit is PositType3{UInt8}(0x0008, 0x0002, 0x4d0x00).
This posit with bits reinterpreted as integer is 770.
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.1084073464102067610840734641020688.
 
π3.141592653589793 to 16-bit posit is PositType3{UInt16}(0x0010, 0x0002, 0x4c91).
This posit with bits reinterpreted as integer is 19601.
This posit as float is 3.1416015625,
with error 8.908910206761538e908910206884002e-6.
 
π3.141592653589793 to 32-bit posit is PositType3{UInt32}(0x0020, 0x0002, 0x4c90fdaa).
This posit with bits reinterpreted as integer is 1284570538.
This posit as float is 3.1415926516056060791015625,
with error 1.984187159361081e984187036896401e-9.
 
π3.141592653589793 to 64-bit posit is PositType3{UInt64}(0x0040, 0x0002, 0x4c90fdaa22168c00).
This posit with bits reinterpreted as integer is 5517188450687028224.
This posit as float is 3.141592653589793115997963468544185161590576171875,
with error 10.2246467991473532e-160.
 
</pre>
-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.
</pre></div>
 
=={{header|Mathematica}}==
Line 191 ⟶ 334:
 
<syntaxhighlight lang="Mathematica">(*
* 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.
*)
 
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};
Line 207 ⟶ 363:
qextra = qsize - (nbits - 2) 2^(es + 2);
)
positableQ[x_] := (Abs[x] == \[Infinity] \[Or]|| x \[Element] 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 s *)
y == \[Infinity], 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. *)
Shift in 1s from the right and scale down. *)
While[y >= useed \[And]&&
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. *)
Shift in 0s from the right and scale up. *)
While[y < 1 \[And]&& 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 \[And]&& 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 \[And]&& i <= nbits, y = 2 y;
p = 2 p + \[LeftFloor]y\[RightFloor]⌊y⌋;
y -= \[LeftFloor]y\[RightFloor]⌊y⌋; i++];
p *= 2^(nbits + 1 - i); i++;(* Round to nearest; tie goes to even *)
 
i = BitAnd[p, 1]; p = \[LeftFloor]p⌊p/2\[RightFloor]2⌋;
p = Which[
i == 0, p, (* closer to lower value *)
y == 1 \[Or]|| 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 *)]
]
Line 259 ⟶ 412:
<pre>77</pre>
 
=={{header|rakuPhix}}==
{{trans|JavaScript}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">posit_encode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">nbits</span><span style="color: #0000FF;">=</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">es</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">npat</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nbits</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">useed</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">es</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">es</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">is_inf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nbits</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">useed</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #000000;">nbits</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">useed</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">y</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">nbits</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">useed</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">nbits</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nbits</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">></span><span style="color: #000000;">0.5</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">nbits</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">e</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">/=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">nbits</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nbits</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">or</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">npat</span><span style="color: #0000FF;">-</span><span style="color: #000000;">p</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">),</span><span style="color: #000000;">npat</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">posit_encode</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
77
</pre>
Can also re-encode all the outputs from the decode task perfectly.
 
=={{header|Raku}}==
{{trans|Mathematica}}
<syntaxhighlight lang=raku>
Line 345 ⟶ 574:
=={{header|Wren}}==
{{trans|Mathematica}}
<syntaxhighlight lang="ecmascriptwren">/* See original Mathematica example for copyright notice and comments. */
 
var nbits = 8
9,482

edits