(cls, scalar, n=None, max_int=None, precision=None, max_exponent=None)
return cls(int_fixpoint % n, exponent, n, max_int)
return an encoding of an int or float.
return an encoding of an int or float.
return an encoding of an int or float .
def encode(cls, scalar, n=None, max_int=None, precision=None, max_exponent=None):
        """return an encoding of an int or float.
        """
        # Calculate the maximum exponent for desired precision
        exponent = None

        #  Too low value preprocess;
        #  avoid "OverflowError: int too large to convert to float"

        if np.abs(scalar) < 1e-200:
            scalar = 0

        if n is None:
            n = cls.Q
            max_int = n // 2

        if precision is None:
            if isinstance(scalar, int) or isinstance(scalar, np.int16) or \
                    isinstance(scalar, np.int32) or isinstance(scalar, np.int64):
                exponent = 0
            elif isinstance(scalar, float) or isinstance(scalar, np.float16) \
                    or isinstance(scalar, np.float32) or isinstance(scalar, np.float64):
                flt_exponent = math.frexp(scalar)[1]
                lsb_exponent = cls.FLOAT_MANTISSA_BITS - flt_exponent
                exponent = math.floor(lsb_exponent / cls.LOG2_BASE)
            else:
                raise TypeError("Don't know the precision of type %s."
                                % type(scalar))
        else:
            exponent = cls.calculate_exponent_from_precision(precision)

        if max_exponent is not None:
            exponent = max(max_exponent, exponent)

        int_fixpoint = int(round(scalar * pow(cls.BASE, exponent)))

        if abs(int_fixpoint) > max_int:
            raise ValueError(f"Integer needs to be within +/- {max_int},but got {int_fixpoint},"
                             f"basic info, scalar={scalar}, base={cls.BASE}, exponent={exponent}"
                             )

        return cls(int_fixpoint % n, exponent, n, max_int)
def encode ( cls , scalar , n = None , max_int = None , precision = None , max_exponent = None ) : # Calculate the maximum exponent for desired precision exponent = None #  Too low value preprocess; #  avoid "OverflowError: int too large to convert to float" if np . abs ( scalar ) < 1e-200 : scalar = 0 if n is None : n = cls . Q max_int = n // 2 if precision is None : if isinstance ( scalar , int ) or isinstance ( scalar , np . int16 ) or isinstance ( scalar , np . int32 ) or isinstance ( scalar , np . int64 ) : exponent = 0 elif isinstance ( scalar , float ) or isinstance ( scalar , np . float16 ) or isinstance ( scalar , np . float32 ) or isinstance ( scalar , np . float64 ) : flt_exponent = math . frexp ( scalar ) [ 1 ] lsb_exponent = cls . FLOAT_MANTISSA_BITS - flt_exponent exponent = math . floor ( lsb_exponent / cls . LOG2_BASE ) else : raise TypeError ( "Don't know the precision of type %s." % type ( scalar ) ) else : exponent = cls . calculate_exponent_from_precision ( precision ) if max_exponent is not None : exponent = max ( max_exponent , exponent ) int_fixpoint = int ( round ( scalar * pow ( cls . BASE , exponent ) ) ) if abs ( int_fixpoint ) > max_int : raise ValueError ( f"Integer needs to be within +/- {max_int},but got {int_fixpoint}," f"basic info, scalar={scalar}, base={cls.BASE}, exponent={exponent}" ) return cls ( int_fixpoint % n , exponent , n , max_int )