Skip to content

Commit 833a8d0

Browse files
authored
Update numbers.rst
1 parent 72568d2 commit 833a8d0

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

docs/numbers.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ Approximate decimals comparison (Significant digits after the point):
4141
>>> DeepDiff(t1, t2, significant_digits=1)
4242
{'values_changed': {'root': {'new_value': Decimal('1.57'), 'old_value': Decimal('1.52')}}}
4343

44+
Approximate fractions comparison (Significant digits after the point):
45+
>>> from fractions import Fraction
46+
>>> t1 = Fraction(22, 7) # 3.142857...
47+
>>> t2 = Fraction(355, 113) # 3.141592...
48+
>>> DeepDiff(t1, t2, significant_digits=2)
49+
{}
50+
>>> DeepDiff(t1, t2, significant_digits=3)
51+
{'values_changed': {'root': {'new_value': Fraction(355, 113), 'old_value': Fraction(22, 7)}}}
52+
4453
Approximate float comparison (Significant digits after the point):
4554
>>> t1 = [ 1.1129, 1.3359 ]
4655
>>> t2 = [ 1.113, 1.3362 ]
@@ -131,13 +140,20 @@ math_epsilon: Decimal, default = None
131140

132141
To check against that the math core module provides the valuable isclose() function. It evaluates the being close of two numbers to each other, with reference to an epsilon (abs_tol). This is superior to the format function, as it evaluates the mathematical representation and not the string representation.
133142

134-
Example:
143+
Example with Decimal:
135144
>>> from decimal import Decimal
136145
>>> d1 = {"a": Decimal("7.175")}
137146
>>> d2 = {"a": Decimal("7.174")}
138147
>>> DeepDiff(d1, d2, math_epsilon=0.01)
139148
{}
140149

150+
Example with Fraction:
151+
>>> from fractions import Fraction
152+
>>> d1 = {"a": Fraction(7175, 1000)}
153+
>>> d2 = {"a": Fraction(7174, 1000)}
154+
>>> DeepDiff(d1, d2, math_epsilon=0.01)
155+
{}
156+
141157
.. note::
142158
math_epsilon cannot currently handle the hashing of values, which is done when :ref:`ignore_order_label` is True.
143159

0 commit comments

Comments
 (0)