def test_implicit_convert_xor(self):
     x = Symbol('x')
     self.assertEqual(parse_expr("3x^2/5", local_dict={}), 
                       x**2*sympify("3/5"))
     self.assertEqual(
         normalize_floats(parse_expr("3x^2/5.", local_dict={})), 
         normalize_floats(0.6*x**2))
Example #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
 def test_rationals_floats(self):
     x = Symbol('x')
     self.assertEqual(parse_expr("3x^2/5"), x**2*sympify("3/5"))
     self.assertEqual(
         normalize_floats(parse_expr("3x^2/5.")), 
         normalize_floats(0.6*x**2))