Posit numbers/encoding

From Rosetta Code
Revision as of 10:49, 22 September 2023 by PureFox (talk | contribs) (Converted this to a draft task and added a Wren example.)
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.

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] == \[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 == \[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. *)
    
    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. *)
    
    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 -= \[LeftFloor]y\[RightFloor]; i++];
   p *= 2^(nbits + 1 - i); i++;(* Round to nearest; tie goes to even *)

      i = BitAnd[p, 1]; p = \[LeftFloor]p/2\[RightFloor];
   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 *)]
   ] 
  ]
setpositenv[{8,2}];
x2p @ Pi
Output:
77

Wren

Translation of: Mathematica
/* See original Mathemetica 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