Example #1
0
    def create(self):
        ctx = gmpy2.ieee(self.length * 8)  # Emulate IEEE for requested format size
        ctx.round = self.rmodeo  # Set the requested gmpy2 rounding mode
        gmpy2.set_context(ctx)  # Make this the active context.

        # Convert the floating point string to an mpfr object
        return gmpy2.mpfr(self.fpstr)
Example #2
0
File: bfp.py Project: mstram/SATK
 def create(self,string,round=0):
     ctx=gmpy2.ieee(self.format)
     ctx.round=MPFR_Data.rounding[round]
     gmpy2.set_context(ctx)
     fpo=gmpy2.mpfr(string)
     ctx=gmpy2.get_context()     # Get the results
     self.ctx=ctx.copy()         # Copy the context for later status
     return fpo
Example #3
0
File: bfp.py Project: mstram/SATK
 def create(self, string, round=0):
     ctx = gmpy2.ieee(self.format)
     ctx.round = MPFR_Data.rounding[round]
     gmpy2.set_context(ctx)
     fpo = gmpy2.mpfr(string)
     ctx = gmpy2.get_context()  # Get the results
     self.ctx = ctx.copy()  # Copy the context for later status
     return fpo
Example #4
0
 def precision_hook(self, value):
     fp_format = {'single': 32, 'double': 64}.get(value, None)
     if fp_format is not None:
         value = gmpy2.ieee(fp_format).precision - 1
     if isinstance(value, str):
         value = int(value)
     gmpy2.get_context().precision = value + 1
     return value
Example #5
0
    def to_bytes(self, byteorder="big"):

        self.digits()

        print(format(self.fpo, "A"))
        ctx = gmpy2.ieee(self.length * 8)
        gmpy2.set_context(ctx)
        b = gmpy2.to_binary(self.fpo)
        return b
Example #6
0
    def __init__(self, src, ic=None, format=32, round=0):
        self.ic = ic  # Interchange format hex data string
        self.fpo = None  # gnoy2.mpfr object
        self.format = format  # interchange format being created

        # These values are supplied by the gmpy2.mpfr.digits() method
        self.digits = None  # the binary digits of the signigicand
        self.dexp = None  # the signed exponent
        self.dprec = None  # the precision of the object

        # These attributes are produced below and are destined for the interchange
        # format
        self.isign = None  # The value's sign
        self.ibits = None  # The actual bits destined for the significand
        self.iexp = None  # The signed exponent destined for the int

        if isinstance(src, gmpy2.mpfr):
            self.fpo = src
        elif isinstance(src, str):
            ctx = gmpy2.ieee(format)
            ctx.round = gmpy2.round = round
            gmpy2.set_context(ctx)
            self.fpo = gmpy2.mpfr(src)
        else:
            raise ValueError(
                "%s 'byts' argument unrecognized: %s" % (fp.eloc(self, "__init__", module=this_module), byts)
            )

        self.digits, self.dexp, self.dprec = self.fpo.digits(2)

        if self.digits[0] == "-":
            self.isign = 1
            self.ibits = self.digits[2:]  # Remove the sign and implied first 1
        else:
            self.isign = 0
            self.ibits = self.digits[1:]  # Remove the implied first 1

        # The exponent assumes the leading one is part of the significand, so the
        # exponent is one larger than is required for the interchange format.
        self.iexp = self.dexp - 1
def mpfr_context(a, rm=None):
    assert isinstance(a, MPF)

    # mpfr_precision = a.p
    # mpfr_emax = a.emax + 1
    mpfr_emin = a.emin - a.p + 2

    if mpfr_emin == 0:
        raise Unsupported("mpfr emin would be zero")

    ctx = gmpy2.ieee(32)
    # This is a good place to start from, but we'll overwrite most of
    # the fun stuff.

    ctx.precision = a.p
    ctx.emax      = a.emax + 1
    ctx.emin      = a.emin - a.p + 2

    if rm is not None:
        ctx.round = MPF_TO_MPFR_RM[rm]

    return ctx
Example #8
0
    # Not passing tests!
    import decimal
    Real = decimal.Decimal
    decimal.getcontext().prec = 80
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'numpy':
    import numpy
    Real = numpy.float64
    del numpy
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'gmpy2':
    # Not passing tests!
    import gmpy2
    gmpy2.set_context(gmpy2.ieee(128))
    Real = gmpy2.mpz
    NUM_EPS = Real(float("1e-10"))
    NUM_INF = gmpy2.get_emax_max()
    del gmpy2
else:
    raise Exception("Type not found")

NUM_EPS_SQ = NUM_EPS * NUM_EPS
NUM_ZERO = Real(0.0)
NUM_ONE = Real(1.0)


class Event:
    __slots__ = (
        "type",
    # Not passing tests!
    import decimal
    Real = decimal.Decimal
    decimal.getcontext().prec = 80
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'numpy':
    import numpy
    Real = numpy.float64
    del numpy
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'gmpy2':
    # Not passing tests!
    import gmpy2
    gmpy2.set_context(gmpy2.ieee(128))
    Real = gmpy2.mpz
    NUM_EPS = Real(float("1e-10"))
    NUM_INF = gmpy2.get_emax_max()
    del gmpy2
else:
    raise Exception("Type not found")

NUM_EPS_SQ = NUM_EPS * NUM_EPS
NUM_ZERO = Real(0.0)
NUM_ONE = Real(1.0)


class Event:
    __slots__ = (
        "type",
Example #10
0
import builtins
import contextlib
import inspect
import os

import gmpy2
gmpy2.set_context(gmpy2.ieee(64))

import soap
from soap.context.base import _Context, ConfigError

_repr = builtins.repr
_str = builtins.str
_soap_classes = [c for c in dir(soap) if inspect.isclass(c)]


def _run_line_magic(magic, value):
    from soap.shell import shell
    with open(os.devnull, 'w') as null:
        with contextlib.redirect_stdout(null):
            shell.run_line_magic(magic, value)


class SoapContext(_Context):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for c in _soap_classes:
            c._repr = c.__repr__

    def precision_hook(self, value):
        fp_format = {'single': 32, 'double': 64}.get(value, None)