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 = cls.Q // 3 - 1    
                
        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 = math.floor(math.log(precision, cls.BASE))        
        
        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('Integer needs to be within +/- %d but got %d'
                             % (max_int, int_fixpoint))

        return cls(int_fixpoint % n, exponent, n, max_int)
return an encoding of an int or float.
        
fedlearner/model/crypto/fixed_point_number.py