Example #1
0
 def test_asin(self):
     start = 0
     diff = 0.00000001
     for i in range(2):
         for e in env:
             set_posit_env(e[0], e[1])
             b = Math.asin(Posit(start))
             Posit(asin(float(Posit(start).get_value())))
         start += diff
Example #2
0
 def test_log(self):
     start = 10
     diff = 0.1
     for i in range(100):
         for e in env:
             set_posit_env(e[0], e[1])
             b = Math.log(Posit(start))
             self.assertEqual(Posit(log(float(Posit(start).get_value()))),
                              b)
         start += diff
Example #3
0
 def test_large_tan(self):
     start = -100
     diff = 10
     for i in range(100):
         for e in env:
             set_posit_env(e[0], e[1])
             b = Math.tan(Posit(start))
             self.assertEqual(Posit(tan(float(Posit(start).get_value()))),
                              b)
         start += diff
Example #4
0
 def test_pow(self):
     i = 10.0
     while i < 10:
         j = -10.0
         while j < 10.0:
             for e in env:
                 set_posit_env(e[0], e[1])
                 b = Posit(i)**Posit(j)
                 self.assertEqual(
                     Posit(
                         float(Posit(i).get_value())**float(
                             Posit(j).get_value())), b)
             j += 0.1
         i += 0.1
Example #5
0
 def test_small_cast(self):
     ctx = shortcuts.get_context()
     ctx.load_libs(['../PySigmoid/posit-javascript/js/decimallookupv2.js'])
     start = 1.5
     diff = 0.0001
     for i in range(10000):
         res = eval(ctx.run_script('convertDToP("{}")'.format(str(start))))
         for j in res:
             nbits = j["ps"]
             es = j["es"]
             set_posit_env(nbits, es)
             a, b = bin(Posit(start).number)[2:], bin(int(j["posit"],
                                                          2))[2:]
             self.assertEqual(a, b)
         start += diff
Example #6
0
from mpmath import mpf, mp
mp.prec = 7

# 500 digits of precision
getcontext().prec = 50
def decacc(x, y):
    if x == y:
        return D('inf')
    else:
        try:
            return -abs((x / y).log10()).log10()
        except:
            return 0

import numpy
p = Posit()
p.set_bit_pattern(p.maxpos)
p = -p
p = p.get_value()

q = Posit()
q.set_bit_pattern(q.maxpos)
q = q.get_value()

xx = []
yy = []
while p <= q:
    t = deepcopy(p)
    x = Posit(float(p)).get_value()
    y = p
    d = decacc(x, y)
Example #7
0
def intpower(x):
    if type(x) != Posit:
        raise Exception("Argument must be posit")
    q = Quire(x)
    q.q = q.q.intpower()
    return Posit(q)
Example #8
0
def pi(nbits, es):
    # uses Bailey–Borwein–Plouffe formula
    q = Quire(0, nbits = nbits, es = es)
    q.q = q.family.pi
    return Posit(q)
Example #9
0
def exp(x):
    if type(x) != Posit:
        raise Exception("Argument must be posit")
    q = Quire(x)
    q.q = q.q.exp()
    return Posit(q)
Example #10
0
        try:
            return -abs((x / y).log10()).log10()
        except:
            return 0

set_posit_env(8, 0)

start = -100
end = 100
xx = []
yy = []
yy2 = []

def sigmoid(x):
  return 1 / (1 + exp(-x))

while start <= end:
    q = Posit(start)
    q = q.sigmoid()
    xx.append(start)
    yy.append(float(q))
    yy2.append(sigmoid(start))
    start += 0.01

# Plot

a = plt.plot(xx, yy, color = 'red', label = "Posit Flip Sign, Shift Right Two Times")
b = plt.plot(xx, yy2, color = 'blue', label = "Floating Point 1/(1 + e^-x)")
plt.legend()

plt.show()