Пример #1
0
def round(value, n=0):
    from mitesting.math_objects import math_object

    if isinstance(value,math_object):
        expression=value.return_expression()
        copy_from=value
    else:
        from sympy import sympify,SympifyError
        try:
            expression=sympify(value)
        except SympifyError:
            return value
        copy_from=None

    from mitesting.customized_commands import round_expression
    
    return math_object(round_expression(expression,n),
                       copy_parameters_from=copy_from)
Пример #2
0
    def eval_to_comparison_precision(self, expression, additional_rounding=None, evaluate=None):
        """
        If parameter round_on_compare is set, 
        evaluate all numbers and numbersymbols (like pi) 
        of expression to floats 
        with either round_on_compare digits of precision (if not round_absolute)
        or to round_on_compare decimals (if round_absolute)
        It will convert floats to integers if round_absolute and 
        round_on_compare <= 0.
        If additional rounding is set, round_on_compare is reduced by 
        that amount.
        If round_on_compare is not set, 
        then normalize all floats to 14 digits of precision
        to increase consistency of floats in presence of roundoff errors.
        """

        modified = False
        round_on_compare = self._parameters.get("round_on_compare")
        if round_on_compare is not None:
            if additional_rounding:
                try:
                    round_on_compare -= int(additional_rounding)
                except ValueError:
                    pass

            round_absolute = self._parameters.get("round_absolute", False)

            if round_absolute:
                expression = round_expression(expression, round_on_compare, evaluate=evaluate)
                modified = True
            elif round_on_compare > 0:
                expression = evalf_expression(expression, round_on_compare, evaluate=evaluate)
                modified = True

        if not modified:
            expression = normalize_floats(expression, evaluate=evaluate)

        return expression