Debian Codegraph @

docs/numbers.rst

raw
1 :doc:`/index`
2
3 Numbers
4 =======
5
6 When dealing with numbers, DeepDiff provides the following functionalities:
7
8 .. _significant_digits_label:
9
10 Significant Digits
11 ------------------
12
13 significant_digits : int >= 0, default=None
14
15 significant_digits defines the number of digits AFTER the decimal point to be used in the comparison. However you can override that by setting the number_format_notation="e" which will make it mean the digits in scientific notation.
16
17 .. note::
18 Setting significant_digits will affect ANY number comparison.
19
20 If ignore_numeric_type_changes is set to True and you have left significant_digits to the default of None, it gets automatically set to 55. The reason is that normally when numbers from 2 different types are compared, instead of comparing the values, we only report the type change. However when ignore_numeric_type_changes=True, in order compare numbers from different types to each other, we need to convert them all into strings. The significant_digits will be used to make sure we accurately convert all the numbers into strings in order to report the changes between them.
21
22 .. note::
23 significant_digits by default uses "{:.Xf}".format(Your Number) behind the scene to compare numbers where X=significant_digits when the number_format_notation is left as the default of "f" meaning fixed point.
24
25 As a side note, please pay attention that adding digits to your floating point can result in small differences in the results. For example:
26 "{:.3f}".format(1.1135) = 1.113, but "{:.3f}".format(1.11351) = 1.114
27
28 For Decimals, Python's format rounds 2.5 to 2 and 3.5 to 4 (to the closest even number)
29
30 .. note::
31 To override what significant digits mean and switch it to scientific notation, use number_format_notation="e"
32 Behind the scene that switches DeepDiff to use "{:.Xe}".format(Your Number) where X=significant_digits.
33
34 **Examples:**
35
36 Approximate decimals comparison (Significant digits after the point):
37 >>> t1 = Decimal('1.52')
38 >>> t2 = Decimal('1.57')
39 >>> DeepDiff(t1, t2, significant_digits=0)
40 {}
41 >>> DeepDiff(t1, t2, significant_digits=1)
42 {'values_changed': {'root': {'new_value': Decimal('1.57'), 'old_value': Decimal('1.52')}}}
43
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
53 Approximate float comparison (Significant digits after the point):
54 >>> t1 = [ 1.1129, 1.3359 ]
55 >>> t2 = [ 1.113, 1.3362 ]
56 >>> pprint(DeepDiff(t1, t2, significant_digits=3))
57 {}
58 >>> pprint(DeepDiff(t1, t2))
59 {'values_changed': {'root[0]': {'new_value': 1.113, 'old_value': 1.1129},
60 'root[1]': {'new_value': 1.3362, 'old_value': 1.3359}}}
61 >>> pprint(DeepDiff(1.23*10**20, 1.24*10**20, significant_digits=1))
62 {'values_changed': {'root': {'new_value': 1.24e+20, 'old_value': 1.23e+20}}}
63
64
65 .. _number_format_notation_label:
66
67 Number Format Notation
68 ----------------------
69
70 number_format_notation : string, default="f"
71 number_format_notation is what defines the meaning of significant digits. The default value of "f" means the digits AFTER the decimal point. "f" stands for fixed point. The other option is "e" which stands for exponent notation or scientific notation.
72
73 **Examples:**
74
75 Approximate number comparison (significant_digits after the decimal point in scientific notation)
76 >>> DeepDiff(1024, 1020, significant_digits=2, number_format_notation="f") # default is "f"
77 {'values_changed': {'root': {'new_value': 1020, 'old_value': 1024}}}
78 >>> DeepDiff(1024, 1020, significant_digits=2, number_format_notation="e")
79 {}
80
81 .. _number_to_string_func_label:
82
83 Number To String Function
84 -------------------------
85
86 number_to_string_func : function, default=None
87 In many cases DeepDiff converts numbers to strings in order to compare them. For example when ignore_order=True, when significant digits parameter is defined or when the ignore_numeric_type_changes=True.
88 In its simplest form, the number_to_string_func is "{:.Xf}".format(Your Number) where X is the significant digits and the number_format_notation is left as the default of "f" meaning fixed point.
89 The number_to_string_func parameter gives the user the full control into overriding how numbers are converted to strings for comparison. The default function is defined in https://github.com/seperman/deepdiff/blob/master/deepdiff/helper.py and is called number_to_string. You can define your own custom function instead of the default one in the helper module.
90
91 Defining your own number_to_string_func
92 Lets say you want the numbers comparison happen only for numbers above 100 for some reason.
93
94 >>> from deepdiff import DeepDiff
95 >>> from deepdiff.helper import number_to_string
96 >>> def custom_number_to_string(number, *args, **kwargs):
97 ... number = 100 if number < 100 else number
98 ... return number_to_string(number, *args, **kwargs)
99 ...
100 >>> t1 = [10, 12, 100000]
101 >>> t2 = [50, 63, 100021]
102 >>> DeepDiff(t1, t2, significant_digits=3, number_format_notation="e")
103 {'values_changed': {'root[0]': {'new_value': 50, 'old_value': 10}, 'root[1]': {'new_value': 63, 'old_value': 12}}}
104 >>>
105 >>> DeepDiff(t1, t2, significant_digits=3, number_format_notation="e",
106 ... number_to_string_func=custom_number_to_string)
107 {}
108
109
110 Ignore Numeric Type Changes
111 ---------------------------
112
113 ignore_numeric_type_changes: Boolean, default = False
114 read more at :ref:`ignore_numeric_type_changes_label`
115
116 .. _ignore_nan_inequality_label:
117
118 Ignore Nan Inequality
119 ---------------------
120
121 ignore_nan_inequality: Boolean, default = False
122 Whether to ignore float('nan') inequality in Python. Note that this is a cPython "feature". Some versions of Pypy3 have nan==nan where in cPython nan!=nan
123
124 >>> float('nan') == float('nan')
125 False
126 >>> DeepDiff(float('nan'), float('nan'))
127 {'values_changed': {'root': {'new_value': nan, 'old_value': nan}}}
128 >>> DeepDiff(float('nan'), float('nan'), ignore_nan_inequality=True)
129 {}
130
131 .. _math_epsilon_label:
132
133 Math Epsilon
134 ------------
135
136 math_epsilon: Decimal, default = None
137 math_epsilon uses Python's built in Math.isclose. It defines a tolerance value which is passed to math.isclose(). Any numbers that are within the tolerance will not report as being different. Any numbers outside of that tolerance will show up as different.
138
139 For example for some sensor data derived and computed values must lie in a certain range. It does not matter that they are off by e.g. 1e-5.
140
141 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.
142
143 Example with Decimal:
144 >>> from decimal import Decimal
145 >>> d1 = {"a": Decimal("7.175")}
146 >>> d2 = {"a": Decimal("7.174")}
147 >>> DeepDiff(d1, d2, math_epsilon=0.01)
148 {}
149
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
157 .. note::
158 math_epsilon cannot currently handle the hashing of values, which is done when :ref:`ignore_order_label` is True.
159
160
161 .. _use_log_scale_label:
162
163 Use Log Scale
164 -------------
165
166 use_log_scale: Boolean, default=False
167 use_log_scale along with :ref:`log_scale_similarity_threshold_label` can be used to ignore small changes in numbers by comparing their differences in logarithmic space. This is different than ignoring the difference based on significant digits.
168
169
170 >>> from deepdiff import DeepDiff
171
172 >>> t1 = {'foo': 110, 'bar': 306}
173 >>> t2 = {'foo': 140, 'bar': 298}
174 >>>
175 >>> DeepDiff(t1, t2)
176 {'values_changed': {"root['foo']": {'new_value': 140, 'old_value': 110}, "root['bar']": {'new_value': 298, 'old_value': 306}}}
177
178 >>> DeepDiff(t1, t2, use_log_scale=True, log_scale_similarity_threshold=0.01)
179 {'values_changed': {"root['foo']": {'new_value': 140, 'old_value': 110}, "root['bar']": {'new_value': 298, 'old_value': 306}}}
180
181 >>> DeepDiff(t1, t2, use_log_scale=True, log_scale_similarity_threshold=0.1)
182 {'values_changed': {"root['foo']": {'new_value': 140, 'old_value': 110}}}
183
184 >>> DeepDiff(t1, t2, use_log_scale=True, log_scale_similarity_threshold=0.3)
185 {}
186
187
188 .. _log_scale_similarity_threshold_label:
189
190 Log Scale Similarity Threshold
191 ------------------------------
192
193 log_scale_similarity_threshold: float, default = 0.1
194 :ref:`use_log_scale_label` along with log_scale_similarity_threshold can be used to ignore small changes in numbers by comparing their differences in logarithmic space. This is different than ignoring the difference based on significant digits. See the example above.
195
196
197 Performance Improvement of Numbers diffing
198 ------------------------------------------
199
200 Take a look at :ref:`diffing_numbers_optimizations_label`
201
202 Back to :doc:`/index`